-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksort.java
More file actions
68 lines (67 loc) · 2.03 KB
/
Quicksort.java
File metadata and controls
68 lines (67 loc) · 2.03 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
import java.util.Random;
import java.util.Scanner;
import java.util.Collections;
import java.util.ArrayList;
public class Quicksort{
public static void sort(Comparable[] a){
int V=a.length;int lo=0;int hi=V-1;
quickSort(a,lo,hi);
}
public static void quickSort(Comparable[] a,int lo,int hi){
if (lo>=hi) return;
int j=quicksort(a, lo, hi);
quickSort(a, lo, j-1);
quickSort(a, j+1, hi);
}
public static int quicksort(Comparable[] a,int lo,int hi){
int i=lo;int j=hi+1;
Random r = new Random();
int x=r.nextInt(hi-lo)+lo;
//exch(a,lo,x);
Comparable pivot=a[x];
exch(a,lo,x);
while (true){
while (less(a[++i],pivot)) {if (i>=hi) break;}//i++;}
while (less(pivot,a[--j])) {if (j<=lo) break;}//j--;}
if (i>=j) break;
exch(a,i,j);
}
exch (a,lo,j);
return j;
}
public static boolean less(Comparable a, Comparable b){
return a.compareTo(b)<0;
}
public static void exch(Comparable[] a, int i,int j){
Comparable t=a[i];a[i]=a[j];a[j]=t;
}
public static void show(Comparable[] a){
for (int i=0;i<a.length;i++){System.out.println(a[i]+"");}
}
public static boolean isSorted(Comparable[] a){
for (int i=1;i<a.length;i++){
if (less(a[i],a[i-1])) {return false;}
}
return true;
}
public static void main(String[] args){
int N=10;
Random r = new Random();
Integer[] a=new Integer [N];
for(int i=0 ; i<N ; i++){
int ran1 = r.nextInt(100);
a[i]=ran1;
}
Scanner scanner=new Scanner(System.in);
Integer[] num=new Integer[N];
for (int i=0;i<N;i++){
num[i]=scanner.nextInt();
}
scanner.close();
// sort(a);
// assert isSorted(a):"not sorted";
// show(a);
sort(num);
show(num);
}
}