Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int _tmain(int argc, _TCHAR* argv[])
- {
- int array[9] = {40, 26, 48, 1, 37, 38, 5, 42, 13};
- QuickSort(array, 0,8);
- bool isSorted = IsSorted(array,9);
- for (int i = 0; i < 9; i++)
- {
- cout<<array[i]<< " ";
- }
- if (isSorted)
- {
- cout<<"It is sorted!";
- }
- else
- {
- cout<<"It is not sorted!";
- }
- return 0;
- void QuickSort( int *array, int i, int j )
- {
- if ( i < j )
- {
- int m = Partition( array, i, j );
- QuickSort( array, i, m - 1 );
- QuickSort( array, m + 1, j );
- }
- int Partition(int array[], int i, int j)
- {
- int middle = (i + j)/2;
- int pivot = array[middle];
- int max = j;
- swap(array[middle], array[j]);
- j--;
- while (i < j)
- {
- while (array[i] < pivot)
- {
- i++;
- cout<<"It was here, and the numbers were, array[i]: "<< array[i] << " i: " << i<< " pivot: " << pivot <<endl;
- }
- while (array[j] > pivot && j > 0)
- {
- j--;
- cout<<"It was here, and the numbers were, array[j]: "<< array[j] << " j: " << j<< " pivot: " << pivot <<endl;
- }
- //if (array[i] > pivot && array[j] <= pivot)
- {
- cout<<"The numbers " << array[i] << " and " << array[j] << " are going to be swapped!"<<endl;
- swap(array[i], array[j]);
- //i++;
- //j--;
- }
- }
- //if (i >= j)
- {
- swap(array[j], array[i]);
- swap(array[max], array[i]);
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement