fork download
  1. //*******************************************************************
  2. // NOTE: please read the 'More Info' tab to the right for shortcuts.
  3. //*******************************************************************
  4.  
  5. import java.lang.Math; // headers MUST be above the first class
  6.  
  7. // one class needs to have a main() method
  8. class Ideone
  9. {
  10. static int counter;
  11. // arguments are passed using the text field below this editor
  12. public static void main(String[] args)
  13. {
  14. int a[] = {10,9,8,7,6,5,4,3,2,1};
  15. quickSort(a,0,10);
  16. for (int i=0;i<10;i++) {
  17. System.out.print(a[i]);
  18. }
  19. }
  20.  
  21. public static void quickSort(int[] toSort, int l, int r){
  22. if(r - l <= 1)return;
  23. counter += r - l - 1;
  24. int p = choosePivot(l, r);
  25. int pivot = toSort[p];
  26. int oldP = toSort[p];
  27. toSort[p] = toSort[l];
  28. toSort[l] = oldP;
  29.  
  30. int i = l + 1;
  31. for(int j = l + 1; j < r; j++){
  32. if(toSort[j] < pivot){
  33. int swap = toSort[j];
  34. toSort[j] = toSort[i];
  35. toSort[i] = swap;
  36. i++;
  37. }
  38. }
  39.  
  40.  
  41. oldP = toSort[i - 1];
  42. toSort[i - 1] = toSort[l];
  43. toSort[l] = oldP;
  44.  
  45. quickSort(toSort, l, i-1);
  46. quickSort(toSort, i, r);
  47. }
  48.  
  49. public static int choosePivot(int m, int n){
  50. return n - 1;
  51. //return m;
  52. }
  53. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
12345678910