-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort2.java
More file actions
49 lines (43 loc) · 886 Bytes
/
QuickSort2.java
File metadata and controls
49 lines (43 loc) · 886 Bytes
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
package com.sort;
public class QuickSort2 {
public static void main(String[] args) {
int[] list = new int[5];
list[0] = 4;
list[1] = 9;
list[2] = 1;
list[3] = 5;
list[4] = 7;
for (int i=0; i<list.length;i++)
System.out.print(list[i]+" ");
System.out.println("");
System.out.println("---------------sort");
quickSort(list,0,list.length-1);
for (int i=0; i<list.length;i++)
System.out.print(list[i]+" ");
}
public static void quickSort(int[] s, int l,int r){
if (l<r){
int i =l, j =r, x=s[l];
while(i<j){
while(i<j && s[j]>=x){
j--;
}
if(i<j){
s[i++] = s[j];
}
while (i<j && s[i]<x){
i++;
}
if (i<j){
s[j--] =s[i];
}
}
s[i] = x;
quickSort(s,l,i-1);
quickSort(s,i+1,r);
/*for (int k=0; k<s.length;k++)
System.out.print(s[k]+" ");
System.out.println();*/
}
}
}