-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_QuickSort.java
63 lines (59 loc) · 2 KB
/
sorting_QuickSort.java
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
package sorting;
import java.io.*;
public class QuickSort {
public static void main(String[] args) throws java.lang.Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
System.out.println("enter the number of elements");
int n = Integer.parseInt(br.readLine());
System.out.println("enter the numbers with spaces");
String[] z = br.readLine().split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(z[i]);
}
for (int i = 0; i < n; i++) {
pw.printf("%d ", arr[i]);
}
pw.println();
//create a object of that class mergesort given below
Quicksort sort = new Quicksort();
sort.sort(arr);
for (int i = 0; i < n; i++) {
pw.printf("%d ", arr[i]);
}
pw.close();
}
static class Quicksort{
public static void sort(int[] arr){
sort(arr,0,arr.length-1);
}
public static void sort(int[] arr,int lo,int hi){
if(hi<=lo) return;
int j = partition(arr,lo,hi);
sort(arr,lo,j-1);
sort(arr,j+1,hi);
}
//this is to create a partition of the array so elements before the pivot are less and above are high than pivot
public static int partition(int[] arr,int lo,int hi){
int i = lo ;int j = hi+1;
while(true){
while(arr[++i]<arr[lo]){
if(i==hi) break;
}
while(arr[--j]>arr[lo]){
if(j==lo) break;
}
if(j<=i) break;
swap(arr,i,j);
}
swap(arr,lo,j);
return j;
}
public static void swap(int[] arr,int a,int b){
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
}