-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuick_Sort.java
More file actions
49 lines (41 loc) · 1.19 KB
/
Quick_Sort.java
File metadata and controls
49 lines (41 loc) · 1.19 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
package Sorting_Algo;
public class Quick_Sort {
int simulation(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low-1);
for (int j=low; j<high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
void sort(int arr[], int low, int high) {
if (low < high) {
int sm = simulation(arr, low, high);
sort(arr, low, sm-1);
sort(arr, sm+1, high);
}
}
public static void main(String args[]) {
System.out.println("Given Unsorted Array");
int arr[] = { 10, 5, 20, 7, 18};
for (int i = 0; i <arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
int n=arr.length-1;
Quick_Sort ob = new Quick_Sort();
ob.sort(arr, 0,n);
System.out.println("Ascending-sorted array");
for (int i = 0; i <arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}