Advertisement
marwanpro

isn quicksort

Dec 17th, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. int sizearray = 11;
  2. int array[] = new int[sizearray];
  3. int reverse[] = new int[sizearray];
  4.  
  5. void setup() {
  6.   noLoop();
  7. }
  8.  
  9. void draw() {
  10.   array = FillArrayRandom(0,20);
  11.   ArrayDisplayer(array);
  12.   quickSort(0, array.length-1);
  13.   ArrayDisplayer(array);
  14.   reverse = reverseSort(array);
  15.   ArrayDisplayer(reverse);
  16. }
  17.  
  18. public int[] FillArrayRandom(int minRandom, int MaxRandom) {
  19.   for (int i = 0; i < sizearray; i++) {
  20.     array[i] = (int(random(minRandom, MaxRandom)));
  21.   }
  22.   return array;
  23. }  
  24.  
  25. void ArrayDisplayer(int[] array) {
  26.   print("Array size=" + array.length + " [ ");
  27.   for (int i = 0; i < array.length; i++) {
  28.     if (array.length-1 == i) { print(i+": " + array[i] + " "); }
  29.     else { print(i+": " + array[i] + ", "); }
  30.   }
  31.   println("]");
  32. }
  33.  
  34. /*
  35. public int MinValue(int[] array) {
  36.   int MinV = array[0];
  37.   for (int i = 1; i <= array.length-1; i++) {
  38.     if (array[i] < MinV) { MinV = array[i]; }
  39.   }
  40.   return MinV;
  41. }
  42.  
  43.  
  44. public int MaxValue(int[] array) {
  45.   int MaxV = array[0];
  46.   for (int i = 1; i <= array.length-1; i++) {
  47.     if (array[i] > MaxV) { MaxV = array[i]; }
  48.   }
  49.   return MaxV;
  50. }
  51. */
  52.  
  53.  
  54. /*public int[] quickSort(int[] array, int minIndex, int maxIndex) {
  55.   int switcher = array[0+(array.length-1-0)/2];
  56.   while (minIndex <= maxIndex) {
  57.     while (array[minIndex] < switcher) {
  58.       minIndex++;
  59.     }
  60.     while (array[maxIndex] > switcher) {
  61.       maxIndex--;
  62.     }
  63.     if (minIndex <= maxIndex) {
  64.       SwapNumber(
  65.   }
  66.   if (minIndex
  67.   return array;
  68. }  
  69.  
  70. void SwapNumber(int a, int b) {
  71.   int temp = array[a];
  72.   array[a] = array[b];
  73.   array[b] = temp;
  74. }*/
  75.  
  76. void quickSort(int lowerIndex, int higherIndex) {
  77.     int i = lowerIndex;
  78.     int j = higherIndex;
  79.     int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
  80.     while (i <= j) {
  81.         while (array[i] < pivot) {
  82.             i++;
  83.         }
  84.         while (array[j] > pivot) {
  85.             j--;
  86.         }
  87.         if (i <= j) {
  88.             exchangeNumbers(i, j);
  89.             i++;
  90.             j--;
  91.         }
  92.     }
  93.     if (lowerIndex < j)
  94.         quickSort(lowerIndex, j);
  95.     if (i < higherIndex)
  96.         quickSort(i, higherIndex);
  97. }
  98.  
  99. void exchangeNumbers(int i, int j) {
  100.     int temp = array[i];
  101.     array[i] = array[j];
  102.     array[j] = temp;
  103. }
  104.  
  105. public int[] reverseSort(int[] array) {
  106.   int reversed[] = new int[array.length];
  107.   int count = 0;
  108.   int baselength = array.length-1;
  109.   for (int i = baselength; i >= 0; i--) {
  110.     reversed[count] = array[i];
  111.     count++;
  112.   }
  113.   return reversed;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement