diff --git a/PA03-0920/PA03_Main b/PA03-0920/PA03_Main index 75757e8..66e7355 100755 Binary files a/PA03-0920/PA03_Main and b/PA03-0920/PA03_Main differ diff --git a/PA03-0920/StateUtilities_LogN.c b/PA03-0920/StateUtilities_LogN.c index 54a18ee..5cc57c7 100644 --- a/PA03-0920/StateUtilities_LogN.c +++ b/PA03-0920/StateUtilities_LogN.c @@ -362,8 +362,20 @@ Dependencies: as needed */ void reverseArray( StateArrayType *stateArray ) { + int arrayIterator; + StateDataType tempState; // reverse in place? could use swap elements to make this happen. // use a for loop? + for(arrayIterator = 0; arrayIterator < stateArray->size / 2; arrayIterator++) + { + // store the state data + tempState = stateArray->array[arrayIterator]; + // swap the element from the 'outer edges' and swap them + // takes from the 'last' element + stateArray->array[arrayIterator] = stateArray->array[stateArray->size - arrayIterator - 1]; + // takes from the temp, which came from 'first' element + stateArray->array[stateArray->size - arrayIterator - 1] = tempState; + } // no return, void } @@ -442,8 +454,41 @@ Dependencies: as needed */ int runPartition( StateArrayType *stateArray, int lowIndex, int highIndex ) { + int partitionIndex, workingLowIndex, workingHighIndex, partitionIterator, originalPivot; + // Identify the partition value at the beginning + // of the array segment (at lowIndex) + workingLowIndex = lowIndex; + workingHighIndex = highIndex; + partitionIndex = lowIndex; + originalPivot = lowIndex; + + // set the working index and the pivot index + // to the low index parameter + + for(partitionIterator = workingLowIndex; partitionIterator < highIndex - 1; partitionIterator++) + { + // Start a loop across the the array segment + // from the low index to the high index, inclusive + // This will use the working index + if(stateArray->array[partitionIterator].population < stateArray->array[partitionIndex].population) + { + // check if the value at the current working index + // is less than the original pivot value + + // increment the pivot index + partitionIndex = partitionIndex + 1; + + // swap the value at the working index + // with the value at the pivot index + swapElements(stateArray->array, partitionIterator, partitionIndex); + } + // end working loop + } + // Swap the original pivot value (at the low index) + // with the value at the current pivot index + swapElements(stateArray->array, originalPivot, partitionIterator); // return zero (temporarily) - return 0; + return partitionIndex; } /* @@ -460,6 +505,7 @@ Dependencies: as needed */ void runQuickSortByPopulation( StateArrayType *stateArray ) { + runQuickSortHelper(stateArray, 0, stateArray->size); // no return, void } @@ -480,6 +526,18 @@ Dependencies: as needed void runQuickSortHelper( StateArrayType *stateArray, int lowIndex, int highIndex ) { + int currentPartition; + // check to see if the lowIndex is less than the highIndex + // (meaning more than one item in list) + if(lowIndex < highIndex) + { + // use runPartition to get pivot and index of pivot + currentPartition = runPartition(stateArray, lowIndex, highIndex); + // use runQuickSortHelper and use lowIndex and pivot - 1 to get left list + runQuickSortHelper(stateArray, lowIndex, currentPartition - 1); + // use runQuickSortHelper and use pivot + 1 and highIndex to get right list + runQuickSortHelper(stateArray, currentPartition + 1, highIndex); + } // no return, void } diff --git a/PA03-0920/build/Debug/outDebug b/PA03-0920/build/Debug/outDebug index fc5b29a..2d1fcd4 100755 Binary files a/PA03-0920/build/Debug/outDebug and b/PA03-0920/build/Debug/outDebug differ