Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3. int array[9] = {40, 26, 48, 1, 37, 38, 5, 42, 13};
  4. QuickSort(array, 0,8);
  5.  
  6. bool isSorted = IsSorted(array,9);
  7.  
  8. for (int i = 0; i < 9; i++)
  9. {
  10. cout<<array[i]<< " ";
  11. }
  12.  
  13. if (isSorted)
  14. {
  15. cout<<"It is sorted!";
  16. }
  17. else
  18. {
  19. cout<<"It is not sorted!";
  20. }
  21.  
  22. return 0;
  23.  
  24. void QuickSort( int *array, int i, int j )
  25. {
  26. if ( i < j )
  27. {
  28. int m = Partition( array, i, j );
  29. QuickSort( array, i, m - 1 );
  30. QuickSort( array, m + 1, j );
  31. }
  32.  
  33. int Partition(int array[], int i, int j)
  34. {
  35.  
  36. int middle = (i + j)/2;
  37. int pivot = array[middle];
  38. int max = j;
  39.  
  40.  
  41. swap(array[middle], array[j]);
  42. j--;
  43. while (i < j)
  44. {
  45. while (array[i] < pivot)
  46. {
  47. i++;
  48. cout<<"It was here, and the numbers were, array[i]: "<< array[i] << " i: " << i<< " pivot: " << pivot <<endl;
  49. }
  50. while (array[j] > pivot && j > 0)
  51. {
  52. j--;
  53. cout<<"It was here, and the numbers were, array[j]: "<< array[j] << " j: " << j<< " pivot: " << pivot <<endl;
  54. }
  55. //if (array[i] > pivot && array[j] <= pivot)
  56. {
  57. cout<<"The numbers " << array[i] << " and " << array[j] << " are going to be swapped!"<<endl;
  58. swap(array[i], array[j]);
  59. //i++;
  60. //j--;
  61. }
  62. }
  63. //if (i >= j)
  64. {
  65. swap(array[j], array[i]);
  66. swap(array[max], array[i]);
  67. return i;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement