Sunday 19 October 2014

Quick Sort in Java

Structure of the Problem Requirements 

In this Program we will learn about Quick Sorting Algorithm. Quick Sort is a very efficient sorting algorithm. In this algorithm we divided our problem into two Parts. One of the Part is the partition of the phases and other is sorting the Phases.In first phase we divide our problems in further two array and in both arrays Sorting is done in first phase, then in second phase both array combines again and then sort them and resultant array display in sorting order

SOURCE CODE



import java.io.*;
import java.util.*;
public class QuickSort
{
public static voidmain(String args[]) throws IOException
{
InputStreamReaderisr=new InputStreamReader(System.in);
  BufferedReader br=new BufferedReader(isr);
  int x[]=new int[40];
  int size,i;
  char st[]=newchar[40];
  Operate op=new Operate();
 System.out.print("Enter no.of characters:");
 size=Integer.parseInt(br.readLine());


  System.out.println("\nEnter "+size+" characters :");

 for (i=0;i<size;i++)
 {
 Scanner reader = new Scanner(System.in);
char c = reader.next().charAt(0);
 st[i]=c;
x[i]=(int)st[i];

}

op.quicksort(x,0,size-1,size);
System.out.println("Sorted elements: ");
  for(i=0;i<size;i++)
  {
  char c=(char)x[i];
    System.out.println(" "+c+"\n");
 }

}
}
class Operate
{
void quicksort(int x[],int first,int last,int size){
    int pivot,j,temp,i;

     if(first<last){
         pivot=first;
         i=first;
         j=last;

         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }
         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1,size);
         quicksort(x,j+1,last,size);
    }
}
}

OUTPUT OF THE PROGRAM


Quick Sort Algorithm
Quick Sort Algorithm



Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

3 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

  1. This is in java. Suppose that your Quick-Sort rule uses a pivot rule that picks the component within the “middle”. That is, for AN array A[0,1,...,n−1] of size n, it uses the component in A[n/2] as pivot if n is even and therefore the component in A[(n − 1)/2] as pivot if n is odd. Illustrate however this rule works employing a quick-sort tree on the input:....!!!!

    ReplyDelete
  2. Note additionally that the rule should not be within the whereas loop. Quicksort is (1) separate the array into 2 teams, one all below your "pivot" middle range and one all on top of, then (2) kind every teams with one decision to quicksort on every.....

    ReplyDelete
  3. In rule, you usually ought to shorten the vary that you simply decision yourself with, alternatively it will be infinite. i have not discovered specifically what you are doing

    ReplyDelete