Last active
August 29, 2015 14:03
-
-
Save metallurgix/6fb31ad9ac73b4b5e27a to your computer and use it in GitHub Desktop.
QuickSort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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