-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
64 lines (46 loc) · 1.36 KB
/
QuickSort.java
File metadata and controls
64 lines (46 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
class QuickSort{
public void swap(int numArray[],int i,int j){
int temp;
temp=numArray[i];
numArray[i]=numArray[j];
numArray[j]=temp;
}
public int partitioning(int numArray[], int start,int end){
QuickSort p = new QuickSort();
int pivot = numArray[end];
int i=start-1;
for(int j=start;j<=end-1;j++){
if(numArray[j]<pivot){
i++;
p.swap(numArray,i,j);
}
}
swap(numArray,i+1,end);
return i+1;
}
public void quickSort(int numArray[], int start, int end){
if(start >= end){
return;
}
int pivot;
pivot=partitioning(numArray,start,end);
quickSort(numArray,start,pivot-1);
quickSort(numArray,pivot+1,end);
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int [] numArray = new int[size];
for(int i =0; i<size ; i++){
numArray[i] = sc.nextInt();
}
QuickSort p = new QuickSort();
p.quickSort(numArray,0,size-1);
System.out.println("Array : ");
for(int i =0; i<size ; i++){
System.out.println(numArray[i]);
}
sc.close();
}
}