fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int arr[] = {10, 7, 8, 9, 1, 5};
  14. int p=0;
  15. int r=5;
  16. quickSort(arr,p,r);
  17. printArray(arr);
  18. }
  19. public static void quickSort(int arr[], int p, int r) {
  20.  
  21. if (p < r) {
  22. // System.out.println(p+" "+r);
  23. int q = partition(arr, p, r);
  24. quickSort(arr, p, q - 1);
  25. quickSort(arr, q + 1, r);
  26. }
  27. }
  28.  
  29. public static int partition(int arr[], int p, int r) {
  30. int pivot = arr[r];
  31. int i = p - 1;
  32. for (int j = p; j <= r - 1; j++) {
  33. // System.out.println("j");
  34. if (arr[j] <= pivot) {
  35. i = i + 1;
  36. int temp = arr[i];
  37. arr[i] = arr[j];
  38. arr[j] = temp;
  39. }
  40. }
  41. int temp = arr[i + 1];
  42. arr[i + 1] = arr[r];
  43. arr[r] = temp;
  44. return i + 1;
  45. }
  46.  
  47. static void printArray(int arr[]) {
  48. int n = arr.length;
  49. for (int i = 0; i < n; ++i)
  50. System.out.print(arr[i] + " ");
  51. System.out.println();
  52. }
  53. }
Success #stdin #stdout 0.12s 320576KB
stdin
Standard input is empty
stdout
1 5 7 8 9 10