public void BucketSort() { auxCollection = new Droid[highestValue]; for (int i = 0; i < highestValue; i++) { auxCollection[i] = DCollection[i]; } // push droids onto the appropriate stack foreach (Droid droid in auxCollection) { if (droid.Model == "Astromech") { astromechStack.Push(droid); } if (droid.Model == "Janitor") { janitorialStack.Push(droid); } if (droid.Model == "Utility") { utilityStack.Push(droid); } if (droid.Model == "Protocol") { protocolStack.Push(droid); } } while (astromechStack.size() != 0) { genericQueue.Endqueue(astromechStack.Pop()); } while (janitorialStack.size() != 0) { genericQueue.Endqueue(janitorialStack.Pop()); } while (utilityStack.size() != 0) { genericQueue.Endqueue(utilityStack.Pop()); } while (protocolStack.size() != 0) { genericQueue.Endqueue(protocolStack.Pop()); } BSI = 0; // dequeue all the droids back into the original DCollection array while (genericQueue.size() != 0) { DCollection[BSI] = genericQueue.queue(); BSI++; } }
} //without the highestIndex, the sort could break due to possible null values in the array. //******************END METHOD WHICH WILL MERGE SORT DROID COLLECTION BY TOTAL COST******************// //*****************METHOD WHICH WILL USE BUCKET SORT TO SORT DROIDS BY MODEL******************************// public void BucketSort() { //Sort order should be: Astromech, Janitor, Utility, Protocol auxCollection = new Droid[highestIndex]; for (int i = 0; i < highestIndex; i++) //for loop that copys the droidcollection array to the auxcollection array, removing any empty spots in { //the droidcollection array auxCollection[i] = droidCollection[i]; } foreach (Droid droid in auxCollection) //these statements will push droids onto the appropriate stack { if (droid.Model == "Astromech") { astromechStack.Push(droid); } if (droid.Model == "Janitor") { janitorialStack.Push(droid); } if (droid.Model == "Utility") { utilityStack.Push(droid); } if (droid.Model == "Protocol") { protocolStack.Push(droid); } } //A group of while loops that will Pop off the droid from each stack and enqueue them into the genericqueue while (astromechStack.size() != 0) { genericQueue.Enqueue(astromechStack.Pop()); } while (janitorialStack.size() != 0) { genericQueue.Enqueue(janitorialStack.Pop()); } while (utilityStack.size() != 0) { genericQueue.Enqueue(utilityStack.Pop()); } while (protocolStack.size() != 0) { genericQueue.Enqueue(protocolStack.Pop()); } bucketCounter = 0; //set bucketcounter to 0, the following while loop will increment the counter by 1 each time //This while loop will dequeue all the droids back into the original droidcollection array in the correct order while (genericQueue.size() != 0) { droidCollection[bucketCounter] = genericQueue.Dequeue(); bucketCounter++; } }