Posted By

jsmith on 01/10/12


Tagged

sort java algorithm sort-algorithm Quick-sort


Versions (?)

Quick Sort


 / Published in: Java
 

  1. int partition(int arr[], int left, int right)
  2. {
  3. int i = left, j = right;
  4. int tmp;
  5. int pivot = arr[(left + right) / 2];
  6. while (i <= j) {
  7. while (arr[i] < pivot)
  8. i++;
  9. while (arr[j] > pivot)
  10. j--;
  11. if (i <= j) {
  12. tmp = arr[i];
  13. arr[i] = arr[j];
  14. arr[j] = tmp;
  15. i++;
  16. j--;
  17. }
  18. };
  19. return i;
  20. }
  21.  
  22. void quickSort(int arr[], int left, int right) {
  23. int index = partition(arr, left, right);
  24. if (left < index - 1)
  25. quickSort(arr, left, index - 1);
  26. if (index < right)
  27. quickSort(arr, index, right);
  28. }

Report this snippet  

You need to login to post a comment.