Quick sort is quite efficient algorithm in sorting techniques. In this post i am just sharing my piece of code of quick sort program in java. In the next post i am going to go in detailed explanation regarding quick sort algorithm. In this post i have also included some video links which will help you to understand quick sort algorithm in a better way.

[java]

import java.io.*;

class QuickSortRecursive

{

public static void main ( String args [] )throws IOException

{

int i, n=0;

int x[] = new int[25];

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Enter number of elements to be sorted: ");

n = Integer.parseInt(in.readLine());

System.out.println("n Enter the elements: ");

for(i=0; i<n; i++)

{

System.out.print(" Element at x["+(i+1)+"]=");

x[i] = Integer.parseInt(in.readLine());

}

quicksort(x,0,n-1,n); // calling my recursive sort method

System.out.println("n Sorted elements are: n");

display(x,n);

}

// my quick sort method

static void quicksort(int x[], int lb, int ub, int n)

{

int j=0;

if(lb>=ub)

return;

j=partition(x,lb,ub,j);

System.out.println("n After partitioning from "+(lb+1)+" to "+(ub+1)+" : n " );

display(x,n);

quicksort(x,lb,j-1,n); // sorting left partition

quicksort(x,j+1,ub,n); // sorting right partition

}

// my partition method

static int partition (int x[], int lb, int ub, int pj)

{

int a, down, temp, up; // a is a pivot element

a = x[lb];

up = ub;

down = lb;

while (down < up)

{

while ( x[down] <= a && down<up )

down=down+1;

while ( x[up] > a )

up=up-1;

if ( down < up )

{

temp=x[down];

x[down]=x[up];

x[up]=temp;

}

}

x[lb] = x[up];

x[up] = a;

return up;

}

// my display function

static void display(int x[], int n)

{

int i;

//System.out.println(" ");

for(i=0; i<n; i++)

System.out.print(" " + x[i]);

System.out.println(" ");

}

}

[/java]

codings me lill mistake hai…

check it again… and if i am wrong then sorry : /

Yeah sorry, i guess the code got messed up during the paste operation. Fixed it now. Check again, it should be fine.