xxxxxxxxxx
141/** Return the max value in A[start..end] */
2public int findMax(int[] a, int start, int end) {
3 currentMax = a[start]
4 for i in start+1..end:
5 if a[i] is bigger, update currentMax
6 return currentMax;
7}
8
9/** Print the min of several subarrays of A
10 * Precondition: A.length >= 50. */
11public static void mins(A) {
12 for i in 0..50:
13 print(findMin(A, 0, i));
14}
xxxxxxxxxx
1public int f(int n) {
2 while (n > 0) {
3 System.out.println(n);
4 n = n/2;
5 }
6}
merge
methodxxxxxxxxxx
131merge(A, start, end):
2 initialize i, j
3 B = deep copy of A
4 while neither uncopied segment is empty:
5 copy the smaller of B[i], B[j]
6 into A[k]
7 increment i or j
8 increment k
9 while one uncopied segment is empty:
10 copy the next element in the nonempty
11 segment into A[k]
12 increment i or j
13 increment k
partition
methodxxxxxxxxxx
91partition(A, start, end):
2 initialize i, j
3 while i < j:
4 if A[i] <= p:
5 swap(A, i, i-1)
6 i++
7 else:
8 swap(A, i, j-1)
9 j--
x
1LSDRadixSort(A):
2 create one bucket (queue) for each digit (0 to 9)
3 for each digit (least- to most-significant):
4 for v in A:
5 put v into the bucket based on digit
6 for each bucket, starting from smallest digit
7 while bucket is not empty:
8 move element to the next position in the array