-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort.java
78 lines (73 loc) · 2.68 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
73
74
75
76
77
78
public class Quicksort {
// Write formal pre- and post-conditions for this method.
/*@ requires a != null && a.length > 0;
@ requires \typeof(ulimit) <: \type(int) && \typeof(llimit) <: \type(int);
@ requires ulimit > 0 && ulimit < a.length;
@ requires llimit >= 0 && llimit < ulimit;
@ ensures (\forall int i; 0 < i && i < a.length ==> a[i-1] <= a[i]);
*/
public static void sort(int[] a, int ulimit, int llimit)
{
quicksort(a, 0, a.length, ulimit, llimit);
}
// Write pre-conditions for this method.
//@ modifies a[*];
//@ requires start >= 0 && start <= stop;
//@ requires stop >= start && stop <= a.length;
// Write post-conditions for this method.
private static void quicksort(int[] a, int start, int stop, int ulimit, int llimit)
{
if (stop - start > 1) {
int p = pivot(a, start, stop, ulimit, llimit);
quicksort(a, start, p, a[p], llimit);
quicksort(a, p+1, stop, ulimit, a[p]);
}
}
// Write pre-conditions for this method.
//@ modifies a[*];
//@ requires start >= 0 && start < stop;
//@ requires stop >= start && stop <= a.length;
// Write post-conditions for this method.
//@ ensures \result >= start && \result <= stop;
private static int pivot(int[] a, int start, int stop, int ulimit, int llimit)
{
int p = partition(a, a[start], start+1, stop, ulimit, llimit);
if (start < p)
swap(a, start, p);
return p;
}
// Write pre-conditions for this method.
//@ modifies a[*];
//@ requires a != null && a.length > 0;
//@ requires \typeof(pivot) <: \type(int);
//@ requires start >= 0;
//@ requires stop <= a.length;
// Write post-conditions for this method.
// ensures \result >= 0 && \result < a.length;
private static int partition(int[] a, int pivot, int start, int stop, int ulimit, int llimit)
{
if (start >= stop)
return start - 1;
if (a[start] < pivot)
return partition(a, pivot, start + 1, stop, ulimit, llimit);
if (a[--stop] > pivot)
return partition(a, pivot, start, stop, ulimit, llimit);
if (start < stop) {
swap(a, start, stop);
return partition(a, pivot, start+1, stop, ulimit, llimit);
} else
return start;
}
/*@ requires a != null;
@ requires 0 <= i && i < a.length;
@ requires 0 <= j && j < a.length;
@ modifies a[i], a[j];
@ ensures a[i] == \old(a[j]) && a[j] == \old(a[i]);
*/
public static void swap(int[] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}