-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.java
More file actions
67 lines (56 loc) · 1.21 KB
/
Copy pathquickSort.java
File metadata and controls
67 lines (56 loc) · 1.21 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
package com.epi;
import java.util.*;
public class quickSort{
public static void quick(int[] input,int l,int r){
quickSort(input,l,r);
}
public static void quickSort(int[] input,int l,int r) {
if (r ==-1){
return ;
}
if(l>=r){
return ;
}
int m = l+(r-l)/2;
int pivot = input[m];
int tmp;
int leftIndex=l,midIndex=0,rightIndex=r;
while(leftIndex<rightIndex){
if(input[leftIndex]<pivot){
leftIndex++;
}
else if(input[leftIndex]>pivot){
tmp=input[rightIndex];
input[rightIndex]=input[leftIndex];
input[leftIndex] = tmp;
rightIndex--;
}
else{
input[leftIndex]=input[l+midIndex];
input[l+midIndex]=pivot;
midIndex++;
leftIndex++;
}
}
if(input[leftIndex]<pivot){
rightIndex++;
}else {
leftIndex--;
}
for(int i=0;i<midIndex;i++){
input[l+i]=input[leftIndex-i];
input[leftIndex-i]=pivot;
}
quickSort(input,l,leftIndex);
quickSort(input,leftIndex+1,r);
}
public static void main(String[] args){
int []input={100,1203,0,1,3,56,2,3,2,5,10};
if(input.length!=0){
quick(input,0,input.length-1);
for (int t:input){
System.out.println(t);
}
}
}
}