forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
60 lines (48 loc) · 1.42 KB
/
QuickSort.java
File metadata and controls
60 lines (48 loc) · 1.42 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
// Program that implements the quick sort algorithm
// Best Case : O(n log n)
// Average Case : O(n log n)
// Worst Case : O(n^2)
import java.util.Arrays;
public class QuickSortMain {
private static int array[];
public static void sort(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
array = arr;
quickSort(0, array.length-1);
}
private static void quickSort(int left, int right) {
int i = left;
int j = right;
int pivot = array[left+(right-left)/2];
while (i <= j) {
while (array[i] < pivot) { i++; } while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchange(i, j);
i++;
j--;
}
}
if (left < j)
quickSort(left, j);
if (i < right)
quickSort(i, right);
}
private static void exchange(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
int[] input = {33,21,45,64,55,34,11,8,3,5,1};
System.out.println("Before Sorting : ");
System.out.println(Arrays.toString(input));
sort(input);
System.out.println("==================");
System.out.println("After Sorting : ");
System.out.println(Arrays.toString(array));
}
}