fork download
  1. import java.util.Random;
  2.  
  3. public class Main
  4. {
  5. public static void main(String[] args)
  6. {
  7. int[] arr = new int[10];
  8. Random r = new Random();
  9. for (int i = 0; i < arr.length; i++)
  10. {
  11. arr[i] = r.nextInt(1000);
  12. }
  13. quickSort(arr, 0, arr.length-1);
  14. for (int i:arr)
  15. {
  16. System.out.print(i+" ");
  17. }
  18. System.out.println();
  19. }
  20.  
  21. public static void quickSort(int[] a, int left, int right) {
  22.  
  23. int index = partition(a, left, right);
  24. if (left < index - 1)
  25. quickSort(a, left, index);
  26. if (index < right)
  27. quickSort(a, index + 1, right);
  28. }
  29.  
  30. private static int partition (int[] a, int left, int right) {
  31. int i = left - 1;
  32. int j = right + 1;
  33. int pivot = a[left];
  34.  
  35. while (i < j) {
  36.  
  37. i++;
  38.  
  39. while (a[i] < pivot)
  40. i++;
  41.  
  42. j--;
  43.  
  44. while (a[j] > pivot)
  45. j--;
  46.  
  47. if (i < j)
  48. swap (a, i--, j++);
  49. }
  50.  
  51. return i;
  52. }
  53.  
  54. private static void swap (int[] a, int i, int j) {
  55. int temp = a[i];
  56. a[i] = a[j];
  57. a[j] = temp;
  58. }
  59. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
324 329 492 586 800 818 839 862 914 943