-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
58 lines (54 loc) · 1.69 KB
/
QuickSort.java
File metadata and controls
58 lines (54 loc) · 1.69 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
import java.util.Scanner;
/**
* @author Shaon Bhatta Shuvo
*/
public class QuickSort {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int n;
System.out.println("Enter the size of the array:");
n= input.nextInt();
int a[] = new int[n];
//taking array input
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
//calling quickSort method where begin=0 and end = a.length-1
quickSort(a,0,a.length-1);
//printing the array after sorting
for (int k = 0; k < a.length; k++) {
System.out.print(a[k]+" ");
}
}
//partition method
public static int partition(int a[], int begin1, int end1){
int i=begin1-1;
int j=begin1;
int x = a[end1];
for (; j < end1; j++) {
if(x>a[j]){
i = i+1;
int temp = a[i];
a[i] = a[j];
a[j]=temp;
}
}
i = i+1;
int temp = a[i];
a[i] = a[j];
a[j]=temp;
//we can print the array and the value of i to check whether the partition method working properly or not
// for (int k = 0; k < a.length; k++) {
// System.out.print(a[k]+" ");
// }
// System.out.println("i="+i);
return i;
}
public static void quickSort(int a[], int begin, int end){
if(begin<end){
int pivot= partition(a, begin, end);
quickSort(a, begin, pivot-1);
quickSort(a, pivot+1, end);
}
}
}