-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
86 lines (77 loc) · 1.79 KB
/
QuickSort.java
File metadata and controls
86 lines (77 loc) · 1.79 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.interview.sort;
public class QuickSort {
private void swap(int A[],int i,int j)
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
private int split(int A[],int low,int high)
{
int pivot = low;
int i = low +1;
int j = high;
while(i<j)
{
while(i<=j && A[pivot]>=A[i])
{
i++;
}
while(j>=i && A[pivot]<A[j])
{
j--;
}
if(i < j && A[i]>A[j])
{
swap(A,i++,j--);
}
}
if(A[pivot] > A[j]){
swap(A,j,pivot);
}
return j;
}
private int split1(int A[],int low,int high){
int pivot = low;
int i = low+1;
int j = high;
while(i <= j){
if(A[i] <= A[pivot]){
i++;
continue;
}
if(A[j] > A[pivot]){
j--;
continue;
}
swap(A,i++,j--);
}
if(A[pivot] > A[j]){
swap(A,pivot,j);
return j;
}
return pivot;
}
public void sort(int A[],int low,int high)
{
if(low>=high)
{
return;
}
int pos = split1(A,low,high);
sort(A,low,pos-1);
sort(A,pos+1,high);
}
private void printArray(int arr[]){
for(int a : arr){
System.out.println(a);
}
}
public static void main(String args[]){
QuickSort qs = new QuickSort();
int A[] = {11,19,0,-1,5,6,16,-3,6,0,14,18,7,21,18,-6,-8};
// int A[] = {11,9,0,4,6,-1,13};
qs.sort(A, 0, A.length-1);
qs.printArray(A);
}
}