Advertisement
FahimFaisal

QuickSortAlgo1

Jan 28th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package cse221;
  7.  
  8. /**
  9.  *
  10.  * @author acer
  11.  */
  12. public class QuickAlgo1 {
  13.  
  14. static int [] A={6,10,13,5,8,3,2,11};
  15. /**
  16.  *
  17.  * @author acer
  18.  */
  19.  
  20.     /**
  21.      * @param args the command line arguments
  22.      */
  23.     public static void main(String[] args) {
  24.         // TODO code application logic here
  25.        
  26.         int p=0;
  27.         int r=A.length-1;
  28.         quickSort(A,p,r);
  29.        
  30.         for(int i:A){
  31.             System.out.print(i+",");
  32.         }
  33.        
  34.     }
  35.     public static void quickSort(int [] A,int p,int r){
  36.         if(p<r){
  37.             int q=Partition(A,p,r);
  38.             System.out.println("Pivot:"+A[p]);
  39.             QuickAlgo1.quickSort(A,p,q-1);
  40.             QuickAlgo1.quickSort(A,q+1,r);
  41.         }
  42.         /*for(int i:A){
  43.             System.out.print(i+" ");
  44.         }
  45.         System.out.println("");*/
  46.     }
  47.    
  48.     public static int Partition(int []A,int p,int q){
  49.         int x=A[p];
  50.         int i=p;
  51.         int j=0;
  52.         for( j=p+1;j<=q;j++){
  53.             if(A[j]<x){
  54.                 i++;
  55.                //swap(A[i],A[j]);
  56.                // swap arr[i] and arr[j]
  57.                 int temp = A[i];
  58.                 A[i] = A[j];
  59.                 A[j] = temp;
  60.             }
  61.         }
  62.        
  63.         //swap(A[p],A[j]);
  64.         int temp = A[p];
  65.         A[p] = A[i];
  66.         A[i] = temp;
  67.        
  68.         return (i);
  69.     }
  70.    
  71.     public static void swap(int a, int b){
  72.         int temp=a;
  73.         a=b;
  74.         b=temp;
  75.     }
  76.  
  77.    
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement