QuickSort

The Code Vault

Java Code To Illustrate QuickSort:

package quicksort;

import edu.princeton.cs.introcs.*;

public class QuickSort
{

public static void display(int a[])
{
for (int k=0;k<a.length;k++) System.out.print(a[k] + ” “);
System.out.println();
}

public static int Partition(int A[], int p, int r)
{
int pivot = A[r];
int i = p-1;
int temp;
for (int j=p;j<r;j++)
{
if (A[j]<=pivot)
{
i++;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[r];
A[r] = A[i+1];
A[i+1] = temp;
return i+1;
}

public static int HoarePartition(int A[], int p, int r)
{
int i = p;
int j = r;
int x = A[p];
int temp;
while (i<j)
{
while(A[j]>=x && j>p) –j;
while(A[i]<=x && i<r) ++i;
if(i<j)
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[p];
A[p] = A[j];
A[j] = temp;
return j;
}

 

public static void quicksort(int A[],int p, int r)
{
if…

View original post 55 more words

Advertisements

About Khuram Ali

Programming... Programming and Programming...!!!

Posted on June 27, 2013, in Algorithms, Java and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: