Skip to content

Instantly share code, notes, and snippets.

@metallurgix
Last active August 29, 2015 14:03
Show Gist options
  • Save metallurgix/6fb31ad9ac73b4b5e27a to your computer and use it in GitHub Desktop.
Save metallurgix/6fb31ad9ac73b4b5e27a to your computer and use it in GitHub Desktop.
QuickSort
public class QuickSort
{
private int[] a;
private int n;
public void sort(int[] b)
{
this.a=b;
n=a.length;
qs(0, n-1);
}
private void qs(int lo, int hi)
{
if (lo>=hi)
return;
int x=a[lo+hi/2];
int i=lo, j=hi;
while (i<=j)
{
while (a[i]<x) i++;
while (a[j]>x) j--;
if (i<=j)
swap(i++, j--);
}
qs(lo, j);
qs(i, hi);
}
private void swap(int i, int j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment