Merge branch 'main' of https://git.cdezselfhosted.pw/cdez/CS249-Projects
This commit is contained in:
commit
bc7d3529c2
3 changed files with 59 additions and 1 deletions
Binary file not shown.
|
|
@ -362,8 +362,20 @@ Dependencies: as needed
|
||||||
*/
|
*/
|
||||||
void reverseArray( StateArrayType *stateArray )
|
void reverseArray( StateArrayType *stateArray )
|
||||||
{
|
{
|
||||||
|
int arrayIterator;
|
||||||
|
StateDataType tempState;
|
||||||
// reverse in place? could use swap elements to make this happen.
|
// reverse in place? could use swap elements to make this happen.
|
||||||
// use a for loop?
|
// 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
|
// no return, void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -442,8 +454,41 @@ Dependencies: as needed
|
||||||
*/
|
*/
|
||||||
int runPartition( StateArrayType *stateArray, int lowIndex, int highIndex )
|
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 zero (temporarily)
|
||||||
return 0;
|
return partitionIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -460,6 +505,7 @@ Dependencies: as needed
|
||||||
*/
|
*/
|
||||||
void runQuickSortByPopulation( StateArrayType *stateArray )
|
void runQuickSortByPopulation( StateArrayType *stateArray )
|
||||||
{
|
{
|
||||||
|
runQuickSortHelper(stateArray, 0, stateArray->size);
|
||||||
// no return, void
|
// no return, void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -480,6 +526,18 @@ Dependencies: as needed
|
||||||
void runQuickSortHelper( StateArrayType *stateArray,
|
void runQuickSortHelper( StateArrayType *stateArray,
|
||||||
int lowIndex, int highIndex )
|
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
|
// no return, void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue