Guest User

Untitled

a guest
Jun 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package sort;
  6.  
  7. import java.util.Random;
  8.  
  9. /**
  10. *
  11. * @author Martijn
  12. */
  13. public class Sort {
  14.  
  15. /**
  16. * @param args the command line arguments
  17. */
  18. public static void main(String[] args) {
  19. Sort s = new Sort();
  20. int array[] = randomIntArray(69000, 10);
  21. System.out.println("Started");
  22. //long Start = System.currentTimeMillis();
  23. s.sort(array);
  24. //long End = System.currentTimeMillis();
  25. for(int i = 0; i< array.length; i++){
  26. System.out.println(array[i]);
  27. }
  28. //long Finish = End - Start;
  29. //System.out.println("Finished in:" +Finish);
  30. }
  31. public static int[] randomIntArray(int length, int n)
  32. {
  33. int[] a = new int[length];
  34. Random generator = new Random();
  35. // for each item in the list
  36. for (int i = 0; i < a.length; i++)
  37. {
  38. // create a new random number and populate the
  39. // current location in the list with it
  40. a[i] = generator.nextInt(n);
  41. }
  42. return a;
  43. }
  44. public void sort(int[] A)
  45. {
  46. quickSort(A,0,A.length-1);
  47. }
  48.  
  49. public void quickSort(int[] A,int left,int right)
  50. {
  51. if(left < right)
  52. {
  53. int pi = partition(A,left,right);
  54. quickSort(A,left,pi-1);
  55. quickSort(A,pi+1,right);
  56. }
  57. }
  58. public int partition(int[] A,int left,int right)
  59. {
  60. int i = left-1;
  61. int j = left-1;
  62. do {
  63. j++;
  64. if (A[j] <= A[right])
  65. {
  66. i++;
  67. swap(A,i,j);
  68. }
  69. }
  70. while (j < right - 1);
  71. swap(A,i+1, right);
  72. return i + 1;
  73. }
  74.  
  75. public void swap(int[] A, int x, int y)
  76. {
  77. int z = A[x];
  78. A[x] = A[y];
  79. A[y] = z;
  80. }
  81. }
Add Comment
Please, Sign In to add comment