Friday, July 20, 2012

Quick Sort Using Java

         Do you want to publish source codes in your blog or web site as follows.
                              Visit Source Code Formatter

 import java.util.*;  
   
 class quick{  
   public void quickSort(int array[]){  
 // the array is sorted in ascending order  
     quickSort(array, 0, array.length - 1);       // quicksort all the elements in the array  
   }  
   
   //Start counting time.  
   long start = System.nanoTime();  
   public void quickSort(int array[], int start, int end){  
       int i = start;             // index of left-to-right scan  
       int k = end;              // index of right-to-left scan  
   
       if (end - start >= 1){          // check that there are at least two elements to sort  
           int pivot = array[start];    // set the pivot as the first element in the partition  
       System.out.println("\n"+"pivot value : "+pivot);  
           while (k > i){          // while the scan indices from left and right have not met,  
               while (array[i] <= pivot && i <= end && k > i) // from the left, look for the first  
                   i++;                  // element greater than the pivot  
                 while (array[k] > pivot && k >= start && k >= i) // from the right, look for the first  
                   k--;                    // element not greater than the pivot  
               if (k > i)                    // if the left seekindex is still smaller than  
                   swap(array, i, k);           // the right index, swap the corresponding elements  
           }  
           swap(array, start, k);     // after the indices have crossed, swap the last element in  
           for(int j=0;j<array.length;j++){  
         System.out.print(array[j]+",");  
       }  
       System.out.println("");     
           quickSort(array, start, k - 1); // quicksort the left partition  
           quickSort(array, k + 1, end);  // quicksort the right partition  
       }  
         else{  // if there is only one element in the partition, do not do any sorting  
           return;           // the array is sorted, so exit  
         }  
   }  
   
   public void swap(int array[], int index1, int index2){  
   // the values at indices 1 and 2 have been swapped  
     int temp = array[index1];      // store the first value in a temp  
     array[index1] = array[index2];   // copy the value of the second into the first  
     array[index2] = temp;        // copy the value of the temp into the second  
   }  
   long end = System.nanoTime();  
   //end counting time.  
 }  
   
 public class quicksort{  
   public static void quicksort(){  
     System.out.println("");  
     Scanner scan=new Scanner(System.in);  
     try{  
       System.out.print("Enter number of elements you want to sort :");  
       int length=scan.nextInt();  
       int[] arr=new int[length];  
       //System.out.println("Enter integer values you want to sort :");  
       for(int i=0;i<arr.length;i++){  
         System.out.print("Enter "+ (i+1)+ " number: ");  
         arr[i]=scan.nextInt();  
       }  
       quick q=new quick();  
       q.quickSort(arr);  
       System.out.println("\n"+"Entered values after Sort :");  
       for(int j=0;j<arr.length;j++){  
         System.out.print(+arr[j]+",");  
       }  
       long elapsedTime = q.end - q.start;  
       System.out.println("");  
       System.out.println("\n"+"Running time of heap sort ="+elapsedTime + " nano seconds"+"\n");  
     }catch(Exception e){  
       System.out.println("Error in input");  
     }  
   }  
 }  

0 comments:

Post a Comment