Wednesday 4 July 2012

program to performed quick sort using java.

import java.io.*;
class QuickSort
{
int lb,ub,a[];
QuickSort(int n)
{
    a=new int[n];   
}

   
void quick(int lb,int ub)
{    int i;
    int j;
    boolean flag;
    int key;
    if(lb<ub)
        {   
             i=lb;
             j=ub+1;
             flag=true;
             key=a[lb];
       
    while(flag)
            {
                i=i+1;
                //while( key>a[i] )
                for(int z=i;i<ub;i++)
                {
                    if(key<a[i])
                    break;
                   
                }
           
                j=j-1;
                //while(key<a[j] )
                for(int z=j;i>=lb;i--)
                {
                    if(key>a[i])
                    break;
                   
                }
                if(i<j )
                {
                    int t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                       
                }   
                else
                {
                    flag=false;
                }
   
            }        //putting  lb to I th position
                    int t=a[i];
                    a[i]=a[lb];
                    a[lb]=t;   
                quick(lb,j-1);
                quick(j+1,ub);
        }
}
void input() throws IOException
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           
        for(int i=0;i<a.length;i++)
        {
            a[i]=Integer.parseInt(br.readLine());
        }   
}   

void display()
{
    System.out.println("output is:");

    for(int i=0;i<a.length;i++)
        {
                System.out.println(a[i]);
        }   

   
}

}
class QuickSortMain
{
public static void main(String args[]) throws IOException
    {
       
       
        QuickSort q=new QuickSort(7);
            q.input();
            q.quick(0,7);
            q.display();
           
           
               
    }
}

No comments:

Post a Comment