Created
February 18, 2013 06:18
-
-
Save apremalal/4975409 to your computer and use it in GitHub Desktop.
Quick sort implementation for integers in java.
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
import java.util.*; | |
public class QuickSort | |
{ | |
int a[]; | |
QuickSort(int a[]) | |
{ | |
this.a=a; | |
} | |
int[] sort() | |
{ | |
quickSort(0,a.length-1); | |
return a; | |
} | |
void quickSort(int p,int r) | |
{ | |
if(p<r) | |
{ | |
int q=partition(p,r); | |
quickSort(p,q-1); | |
quickSort(q+1,r); | |
} | |
} | |
int partition(int p,int r) | |
{ | |
int x=a[r]; | |
int i=p-1; | |
for(int j=p;j<r;j++) | |
{ | |
if(a[j]<=x) | |
{ | |
i=i+1; | |
} | |
} | |
int temp=a[r]; | |
a[r]=a[i+1]; | |
a[i+1]=temp; | |
return i+1; | |
} | |
} | |
class Test | |
{ | |
public static void main(String args[]) | |
{ | |
int a[]={5,4,3,2,1}; | |
QuickSort qs=new QuickSort(a); | |
System.out.println(Arrays.toString(qs.sort())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment