1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
public class QuickSort {
public QuickSort(int[] targetArr) { recursionMethod(targetArr, 0, targetArr.length - 1); for (int r : targetArr) { System.out.println(r); } }
void recursionMethod(int[] partArr, int start, int end) { if (start >= end) return; int pivot = (start + end) / 2; int pivotValue = partArr[pivot]; int lefP = start; int rigP = end; while (lefP <= rigP) { while (lefP <= rigP && partArr[lefP] < pivotValue) lefP++; while (lefP <= rigP && partArr[rigP] > pivotValue) rigP--;
if (lefP <= rigP) { swapValues(partArr, lefP, rigP); lefP++; rigP--; } }
recursionMethod(partArr, start, rigP); recursionMethod(partArr, lefP, end);
}
void swapValues(int[] partArr, int left, int right) { int temp = partArr[left]; partArr[left] = partArr[right]; partArr[right] = temp; }
public static void main(String args[]) { int target[] = {51, 46, 20, 18, 65, 97, 82, 30, 77, 50, 4, 6, 77, 8, 32, 2, 1, 754, 7347, 2352, 656, 75467, 2534, 6436, 2342, 73, 25, 253, 654, 2542, 34252, 6453, 345, 736, 5623, 2345234, 25, 34534, 456, 7456, 776, 567, 4564, 34, 345, 456, 345, 2, 745, 453, 7643625, 25}; try { new QuickSort(target); } catch (StackOverflowError e) {
} } }
|