Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package cse221;
- /**
- *
- * @author acer
- */
- public class QuickAlgo1 {
- static int [] A={6,10,13,5,8,3,2,11};
- /**
- *
- * @author acer
- */
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- int p=0;
- int r=A.length-1;
- quickSort(A,p,r);
- for(int i:A){
- System.out.print(i+",");
- }
- }
- public static void quickSort(int [] A,int p,int r){
- if(p<r){
- int q=Partition(A,p,r);
- System.out.println("Pivot:"+A[p]);
- QuickAlgo1.quickSort(A,p,q-1);
- QuickAlgo1.quickSort(A,q+1,r);
- }
- /*for(int i:A){
- System.out.print(i+" ");
- }
- System.out.println("");*/
- }
- public static int Partition(int []A,int p,int q){
- int x=A[p];
- int i=p;
- int j=0;
- for( j=p+1;j<=q;j++){
- if(A[j]<x){
- i++;
- //swap(A[i],A[j]);
- // swap arr[i] and arr[j]
- int temp = A[i];
- A[i] = A[j];
- A[j] = temp;
- }
- }
- //swap(A[p],A[j]);
- int temp = A[p];
- A[p] = A[i];
- A[i] = temp;
- return (i);
- }
- public static void swap(int a, int b){
- int temp=a;
- a=b;
- b=temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement