-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquicksort.java
72 lines (60 loc) · 1.35 KB
/
quicksort.java
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
import java.util.Scanner;
public class quicksort {
String ar[];int l;
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
quicksort s=new quicksort();
int size,i;
String name[]=new String[20];
System.out.println("Enter the number of elements");
size=in.nextInt();
for(i=0;i<size;i++) {
System.out.println("Enter name");
name[i]=in.next();
}
s.sort(name,size);
System.out.println("Sorted Array");
for (i=0;i<size;i++) {
System.out.print(name[i]);
System.out.print(" ");
}
}
void sort(String array[],int size) {
if (array == null || array.length == 0) {
return ;
}
else{
this.ar = array;
this.l = size;
quick(0, l - 1);
}
}
void quick(int f,int l) {
int i=f,j=l;
String pivot=this.ar[f+(l-f)/2];
while (i <= j) {
while (this.ar[i].compareToIgnoreCase(pivot) <0){
i++;
}
while (this.ar[j].compareToIgnoreCase(pivot) > 0) {
j--;
}
if (i <= j) {
exchange(i, j);
i++;
j--;
}
}
if (f < j) {
quick(f, j);
}
if (i < l) {
quick(i, l);
}
}
void exchange(int i,int j) {
String temp = this.ar[i];
this.ar[i] = this.ar[j];
this.ar[j] = temp;
}
}