-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.java
106 lines (88 loc) · 1.93 KB
/
Sort.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
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
100
101
102
103
104
105
106
package general;
//Shay McCarthy and Aparajita Rana
public class Sort {
private static int[] merge(int[] a, int[] b) {
int[] c = new int[a.length+b.length];
int ia=0;
int ib=0;
int ic=0;
while (ia<a.length && ib<b.length) {
if (a[ia]<b[ib]) {
c[ic] = a[ia];
ia++;
} else {
c[ic] = b[ib];
ib++;
}
ic++;
}
while (ia<a.length) {
c[ic]=a[ia];
ia++;
ic++;
}
while (ib<b.length) {
c[ic]=b[ib];
ib++;
ic++;
}
return c;
}
public static <E extends Comparable<E>> void qs(E[] a) {
qs(a, 0, a.length - 1);
}
private static <E extends Comparable<E>> void qs(E[] a, int first, int last) {
if (first < last) { // There is data to be sorted.
int pivIndex = partition(a, first, last);
qs(a, first, pivIndex - 1);
qs(a, pivIndex + 1, last);
}
}
public static <E extends Comparable<E>> int partition(E[] a, int first, int last) {
/* E temp;
int i=1;
for(int x=1;x<a.length;x++)
{
if((int) a[x]<=first)
{
temp=a[x];
a[x]=a[i];
a[i]=temp;
i++;
}
}
temp=a[i-1];
a[i-1]=a[0];
a[0]=temp;
return i;*/
int pivot = first;
while (first < last)
{
if (first== pivot || last == pivot)
{
return first;
}
while (first < pivot)
{
first++;
}
while (last > pivot)
{
last--;
}
swap (a,first,last);
}
return first;
}
public static <E extends Comparable<E>> void swap (E[]a, int x, int y)
{
E temp = a[x];
a[x] = a[y];
a[y] = temp;
}
public static void main(String[] args) {
Integer[] a = {10,7,6,5};
int x=partition(a,10,5);
System.out.println(x);
}
}