- Untitled
- Saturday, April 14th, 2007 at 3:26:36pm MDT
- public class quickSort {
- public static void main(String[] args)
- {
- int[] myArray = (3,2,5,4,1};
- quicksort(myArray,1,3);
- for (int i = 0; i < myArray.length; i++)
- {
- system.out.printf("d,",myArray[i]);
- }
- }
- public static void quicksort (int[] a)
- { quicksort(a, 0, a.length-1);
- }
- private static void quicksort (int[] a, int l, int r) {
- if (l>=r) //list is empty
- return;
- //recursion step: make partition and sort recursively
- int m = partition(a, l, r);
- quicksort(a,l,m-1);
- quicksort(a,m+1,r);
- }
- //partition the array from l+1 to r with pivot a[l]
- private static int partition (int[] a, int l, int r) {
- int i=l+1; //pointer on left side
- int j=r; //pointer on right side
- int p = a[l]; //pivot element
- //move pointer to center or swap if on wrong sides
- while (i<=j) {
- if (a[i]<=p) i++;
- else if (a[j]>p) j--;
- else swap(a,i,j); }
- //swap pivot element between partitions
- swap(a,l,j);
- //return position of pivot element
- return j; }
- //swap two elements in the array
- private static void swap (int[] a, int i, int j) {
- int h = a[i];
- a[i] = a[j];
- a[j] = h; }
- }
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.