diff --git a/PA02-0913/PA02_Main b/PA02-0913/PA02_Main index 226f7e2..cf539d2 100755 Binary files a/PA02-0913/PA02_Main and b/PA02-0913/PA02_Main differ diff --git a/PA02-0913/PA02_Main.c b/PA02-0913/PA02_Main.c index 929535b..71e9ed4 100644 --- a/PA02-0913/PA02_Main.c +++ b/PA02-0913/PA02_Main.c @@ -80,7 +80,7 @@ int main() // display bubble-sorted list displayData( sortingArray, sortingNumStates, "Full list sorted by name using bubble sort" ); - /* + // insertion sort // function: runInsertionSort runInsertionSortByPopulation( sortingArray, sortingNumStates ); @@ -88,7 +88,7 @@ int main() // display insertion-sorted list displayData( sortingArray, sortingNumStates, "Full list sorted by population using insertion sort" ); - + // selection sort // function: runSelectionSort runSelectionSortByGrowth( sortingArray, sortingNumStates ); @@ -96,7 +96,7 @@ int main() // display insertion-sorted list displayData( sortingArray, sortingNumStates, "Full list sorted by growth using selection sort" ); - */ + } else diff --git a/PA02-0913/StateUtilities.c b/PA02-0913/StateUtilities.c index 0ed35a7..fcf80a6 100644 --- a/PA02-0913/StateUtilities.c +++ b/PA02-0913/StateUtilities.c @@ -36,7 +36,7 @@ bool addElementFromData( StateDataType *stateArr, int *size, int capacity, double stateGrowth, char politicalMajority ) { // check to see if the size will go over the capacity - if(*size <= capacity) + if(*size < capacity) { // put in the data to the array // array[*size] = parameter @@ -112,7 +112,7 @@ void copyArray( StateDataType *dest, const StateDataType *src, int srcSize ) { // copy each element from the array to the new array. // uses deepCopy to do this. - deepCopy(dest, *src); + deepCopy(&dest[currentSize], src[currentSize]); } // no return, void } @@ -235,7 +235,7 @@ void displayData( const StateDataType *array, int size, const char *subTitle ) printCharacter(PIPE); // print new line printEndline(); - for(countStates = 0; countStates < size - 1; countStates++) + for(countStates = 0; countStates < size; countStates++) { // use the constants to format the table // print PIPE character @@ -362,19 +362,16 @@ Dependencies: as needed bool removeStateByName( StateDataType *removedElement, StateDataType *array, int *size, const StateDataType searchVal ) { - bool stateNameFound; int removeCount; bool stateNameFoundFlag = false; - // use the findStateByName to search - stateNameFound = findStateByName(removedElement, array, *size, searchVal); // iterate through array - if(stateNameFound) + if(findStateByName(removedElement, array, *size, searchVal)) { // for(removeCount = 0; removeCount < *size; removeCount++) { if(strcmp(array[removeCount].name, removedElement->name) == 0 || - stateNameFoundFlag == 1) + stateNameFoundFlag) { // set the flag to signify found value stateNameFoundFlag = true; @@ -406,29 +403,19 @@ void runBubbleSortByName( StateDataType *array, int size ) { int bubbleCount; int arrayCount; - printf("\nArray @ 0: %s\n", array[0].name); - printf("\nArray @ 1: %s\n", array[1].name); - printf("\nArray @ 2: %s\n", array[2].name); - printf("\nArray @ 3: %s\n", array[3].name); - printf("\nArray @ 4: %s\n", array[4].name); - printf("\nArray @ size: %s\n", array[size].name); // use two for loops to iterate through array - // for(bubbleCount = 0; bubbleCount < size - 1; bubbleCount++) - // { - // for(arrayCount = bubbleCount + 1; arrayCount < size; arrayCount++) - // { - // printf("\nValue of bubbleCount: %d\n", bubbleCount); - // printf("\nValue of arrayCount: %d\n", arrayCount); - // //printf("\nValue of array[arrayCount].name: %s", array[0].name); - // //printf("\nValue of array[arrayCount + 1].name: %s\n", array[1].name); - // // check the current index vs the next one. - // if(strcmp(array[arrayCount].name, array[arrayCount + 1].name) > 0) - // { - // // if current > next, swap. AKA: strcmp(current, next) > 0 - // swapElements(array, arrayCount, arrayCount + 1); - // } - // } - // } + for(bubbleCount = 0; bubbleCount < size - 1; bubbleCount++) + { + for(arrayCount = 0; arrayCount < size - bubbleCount - 1; arrayCount++) + { + // check the current index vs the next one. + if(strcmp(array[arrayCount].name, array[arrayCount + 1].name) > 0) + { + // if current > next, swap. AKA: strcmp(current, next) > 0 + swapElements(array, arrayCount, arrayCount + 1); + } + } + } // no return, void } @@ -444,6 +431,28 @@ Dependencies: as needed, swap may not be used */ void runInsertionSortByPopulation( StateDataType *array, int size ) { + // define variables + int insertCount, insertIterator; + StateDataType insertStoreState; + // use a for loop to move through the array + for(insertCount = 0; insertCount < size; insertCount++) + { + // start the iterator at the same place where insertCount starts + insertIterator = insertCount; + // use a while loop to do the insert sort + while(insertIterator > 0 && (array[insertIterator - 1].population >= + array[insertIterator].population)) + { + // store the element + deepCopy(&insertStoreState, array[insertIterator - 1]); + // move the next element to the last location + deepCopy(&array[insertIterator - 1], array[insertIterator]); + // put the element into the array + deepCopy(&array[insertIterator], insertStoreState); + // move the iterator to the previous index + insertIterator = insertIterator - 1; + } + } // no return, void } @@ -459,6 +468,25 @@ Dependencies: as needed */ void runSelectionSortByGrowth( StateDataType *array, int size ) { + int selectCount, selectIterator, smallestGrowth; + // use for loop to run through the whole array + for(selectCount = 0; selectCount < size; selectCount++) + { + // start at the not sorted part of the array + smallestGrowth = selectCount; + // use another for loop to find what the smallest growth is + for(selectIterator = selectCount + 1; selectIterator < size; selectIterator++) + { + // select the smallest growth + if(array[selectIterator].growth < array[smallestGrowth].growth) + { + // find the smallest growth of the array. + smallestGrowth = selectIterator; + } + } + // swap the elements of the array + swapElements(array, selectCount, smallestGrowth); + } // no return, void } diff --git a/PA02-0913/build/Release/outRelease b/PA02-0913/build/Release/outRelease new file mode 100755 index 0000000..2c0c71b Binary files /dev/null and b/PA02-0913/build/Release/outRelease differ