Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 11, 2015 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/bd9ae8467628847ed7ed to your computer and use it in GitHub Desktop.
Save thmain/bd9ae8467628847ed7ed to your computer and use it in GitHub Desktop.
public class QuickSort {
private int[] arrA;
public QuickSort(int[] arrA) {
this.arrA = arrA;
}
public void quickS(int low, int high) {
int mid = (low + high) / 2;
int left = low;
int right = high;
int pivot = arrA[mid]; // select middle element as pivot
while (left <= right) {
while (arrA[left] < pivot)
left++;// find element which is greater than pivot
while (arrA[right] > pivot)
right--;// //find element which is smaller than pivot
// System.out.println(arrA[left] + " " + pivot + " " + arrA[right]
// );
// if we found the element on the left side which is greater than
// pivot
// and element on the right side which is smaller than pivot
// Swap them, and increase the left and right
if (left <= right) {
int temp = arrA[left];
arrA[left] = arrA[right];
arrA[right] = temp;
left++;
right--;
}
}
// Recursion on left and right of the pivot
if (low < right)
quickS(low, right);
if (left < high)
quickS(left, high);
}
public void display() {
for (int i = 0; i < arrA.length; i++) {
System.out.print(" " + arrA[i]);
}
}
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
int a[] = { 6, 2, 4, 12, 10 };
QuickSort i = new QuickSort(a);
System.out.print("UnSorted : ");
i.display();
i.quickS(0, a.length - 1);
System.out.println("");
System.out.print("Quick Sorted : ");
i.display();
}
}
@leezzag24
Copy link

Is there a license for this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment