1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
//PRE: Takes in array and an array index
//POST: pushes all indexes forward and puts the value at index 'index' at index n.
void pushArray(char* pointers[], int index, int n)
{
char tmp = ' ';
copyValues(&tmp, pointers[index]);
for(int x = n; x<=index; x++)
{
copyValues(pointers[x+1], pointers[x]);
}
copyValues(pointers[n], &tmp);
}
//PRE: takes in pointer array and two elements to be swapped.
//POST: swaps two elements.
void swapArray(char* pointers[], int n, int m)
{
char tmp = ' ';
copyValues(&tmp, pointers[n]);
copyValues(pointers[n], pointers[m]);
copyValues(pointers[m], &tmp);
}
void quickSort(char* pointers[], int n, int m)
{
int pivot = midpoint(n, m);
if(m-n>0)
{
swapArray(pointers, pivot, n); //places pivot element where all values to left are less than the pivot.
for(int i = n+1; i <= m; i++)
{
if(strCmp(pointers[pivot], pointers[i])>0)
pushArray(pointers, i, n); //...all values below pivot get moved to index n after pushing the others forward.
}
quickSort(pointers, 0, pivot-1);
quickSort(pointers, pivot+1, m);
}
}
int main ()
{
char* pointers[SIZE];
char yourword[SIZE];
int length = 0;
cout << "String to be sorted: ";
cin >> yourword;
length = getLength(yourword);
for(int x = 0; x<length; x++) //THIS is where I can see every array element. After this loop ends, it all disappears except for the first element.
{
pointers[x] = &(yourword[x]);
}
quickSort(pointers, 0, length-1);
displayPointersArray(pointers, length);
return 0;
}
|