/* * iterative implementation of quicksort sorting algorithm. */ publicstaticvoiditerativeQsort(int numbers){ Stack stack = new Stack(); stack.push(0); stack.push(numbers.length);
while (!stack.isEmpty()) { int end = stack.pop(); int start = stack.pop(); if (end - start < 2) { continue; } int p = start + ((end - start) / 2); p = partition(numbers, p, start, end);
stack.push(p + 1); stack.push(end);
stack.push(start); stack.push(p);
} }
/* * Utility method to partition the array into smaller array, and * comparing numbers to rearrange them as per quicksort algorithm. */ privatestaticintpartition(int input, int position, int start, int end){ int l = start; int h = end - 2; int piv = input[position]; swap(input, position, end - 1);
while (l < h) { if (input[l] < piv) { l++; } elseif (input[h] >= piv) { h--; } else { swap(input, l, h); } } int idx = h; if (input[h] < piv) { idx++; } swap(input, end - 1, idx); return idx; }
/** * Utility method to swap two numbers in given array * * @param arr - array on which swap will happen * @param i * @param j */ privatestaticvoidswap(int arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }