} //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++; } }
//method to organize droids by type public bool Organize() { GenericStack <AstromechDroid> astromechStack = new GenericStack <AstromechDroid>(); GenericStack <JanitorDroid> janitorStack = new GenericStack <JanitorDroid>(); GenericStack <UtilityDroid> utilityStack = new GenericStack <UtilityDroid>(); GenericStack <ProtocolDroid> protocolStack = new GenericStack <ProtocolDroid>(); GenericQueue <IDroid> droidQueue = new GenericQueue <IDroid>(); //Add droids to appropriate stack types for (int i = 0; i < lengthOfCollection; i++) { try { astromechStack.Add((AstromechDroid)droidCollection[i]); } catch { try { janitorStack.Add((JanitorDroid)droidCollection[i]); } catch { try { utilityStack.Add((UtilityDroid)droidCollection[i]); } catch { try { protocolStack.Add((ProtocolDroid)droidCollection[i]); } catch { return(false); } } } } } //Add droids in order to the queue while (astromechStack.Head != null) { droidQueue.Add((IDroid)astromechStack.Pop()); } while (janitorStack.Head != null) { droidQueue.Add((IDroid)janitorStack.Pop()); } while (utilityStack.Head != null) { droidQueue.Add((IDroid)utilityStack.Pop()); } while (protocolStack.Head != null) { droidQueue.Add((IDroid)protocolStack.Pop()); } //Dequeue droids back into the array for (int i = 0; droidQueue.Tail != null; i++) { droidCollection[i] = (IDroid)droidQueue.Dequeue(); } return(true); }