-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathquicksort.java
More file actions
99 lines (47 loc) · 1.88 KB
/
quicksort.java
File metadata and controls
99 lines (47 loc) · 1.88 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
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.ArrayList;
import java.util.Random;
public class Program {
static ArrayList <Integer>list=new ArrayList<Integer>();
static boolean swapped=true;
static ArrayList <Integer> ii=new ArrayList<Integer>();
static ArrayList <Integer> jj=new ArrayList<Integer>();
static int temp;
static long count=0;
public static void main(String[] args) {
int i=0;
while (i<300) {
list.add(new Random().nextInt(500));
i++;
}
System.out.println(list);
int loi=0;
int hii=list.size();
sort(list,0,hii-1);
System.out.println(list);
System.out.println("sort was done "+count+" times");}
public static void sort(ArrayList<Integer> sorte,int lo,int hi) {
int x=sorte.get(lo+(hi-lo)/2);
int i=lo;
int j= hi;
// System.out.println(list+"begin i= "+i+"wert "+list.get(i)+"j= "+j+"wert "+list.get(j)+"x= "+x+" lo="+lo+" hi= "+hi);
count++;
while(i<=j) {
while(sorte.get(i)<x) {i++;if(i>hi)i=lo;}
while(sorte.get(j)>x) {j--; if(j<lo) j=hi; }
// System.out.println(list+"mid i= "+i+"wert "+list.get(i)+"j= "+j+"wert "+list.get(j)+"x= "+x+" lo="+lo+" hi= "+hi);
if(i<=j) {temp=sorte.get(i);
sorte.set(i,sorte.get(j));
sorte.set(j, temp);
//System.out.println(list+"end i= "+i+"wert "+list.get(i)+"j= "+j+"wert "+list.get(j)+"x= "+x+" lo="+lo+" hi= "+hi);
i++;if(i>hi)i=lo;
j--; if(j<lo) j=hi;
}//else System.out.println(list+"end i= "+i+"wert "+list.get(i)+"j= "+j+"wert "+list.get(j)+"x= "+x+" lo="+lo+" hi= "+hi);
}
// System.out.println(list+"end i= "+i+"wert "+list.get(i)+"j= "+j+"wert "+list.get(j)+"x= "+x+" lo="+lo+" hi= "+hi);
if(lo<j) {
sort(sorte,lo,j);}
if(i<hi) {
sort(sorte,i,hi);}
list=sorte;
}
}