Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Untitled
Saturday, April 14th, 2007 at 3:26:36pm MDT 

  1. public class quickSort {
  2.  
  3.  public static void main(String[] args)
  4.     {
  5.         int[] myArray = (3,2,5,4,1};
  6.         quicksort(myArray,1,3);
  7.         for (int i = 0; i < myArray.length; i++)
  8.         {
  9.         system.out.printf("d,",myArray[i]);
  10.         }
  11.         }
  12.  
  13.         public static void quicksort (int[] a)
  14.         { quicksort(a, 0, a.length-1);
  15.         }
  16.  
  17.  private static void quicksort (int[] a, int l, int r) {
  18.         if (l>=r)       //list is empty
  19.         return;
  20.  
  21.  //recursion step: make partition and sort recursively
  22.  int m = partition(a, l, r);
  23.  quicksort(a,l,m-1);
  24.  quicksort(a,m+1,r);
  25.  }
  26.  
  27. //partition the array from l+1 to r with pivot a[l]
  28. private static int partition (int[] a, int l, int r) {
  29. int i=l+1; //pointer on left side
  30. int j=r; //pointer on right side
  31. int p = a[l]; //pivot element
  32.  
  33. //move pointer to center or swap if on wrong sides
  34. while (i<=j) {
  35.         if (a[i]<=p) i++;
  36.         else if (a[j]>p) j--;
  37.         else swap(a,i,j); }
  38.        
  39. //swap pivot element between partitions
  40. swap(a,l,j);
  41. //return position of pivot element
  42. return j; }
  43.  
  44. //swap two elements in the array
  45. private static void swap (int[] a, int i, int j) {
  46. int h = a[i];
  47. a[i] = a[j];
  48. a[j] = h; }
  49. }

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right
worth-right