[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
Held: Friday, March 6, 1998
Work on lab 9 for approximately twenty minutes to improve your understanding of the operation of quicksort.
/**
* Sort an array containing the values 1 ... n.
* pre: The array is of size n.
* pre: Each value between 1 and n appears exactly once in the
* array
* post: The array contains the same elements and they're sorted.
*/
public sillySort(int[] stuff)
{
for (int value = 1; value <= this.size(); ++value)
{
stuff[value-1] = value;
} / for
} // sillySort
public static void radixSort() {
radixSort(v,0);
} // radixSort
/**
* Radix sort the vector, starting with the bth-least significant
* bit.
* pre: 0 <= b < # of places in the sequence representation of
* the data type
* pre: this vector has been radix sorted for all 0 <= i < p
* post: this vector is radix sorted
*/
public static void radixSort(place p) {
// Base case, we've run out of bits
if (p == PLACES_IN_ELEMENT) return;
// Recursive case, partition and move on to the next bit
splitAndJoin(b);
return radixSort(b+1);
} // radixSort
/**
* Split the elements at the pth position
* pre: 0 <= b < # of places in the sequence representation of
* the data type
* post: For all i, the pth place in v[i] <= the pth place in v[i+1]
**/
public static void splitAndJoin(int p) {
// Create the set of buckets
Vector[] buckets = new Vector[POSSIBLE_PLACE_VALUES];
for(int i = 0; i < POSSIBLE_PLACE_VALUES; ++i)
buckets[i] = new Vector();
// Paritition the vector
for(int i = 0; i < size(); ++i) {
buckets[elementAt(i).placeValue(p)].addElement(elementAt(i));
}
// Put 'em back together
setLength(0);
for(int i = 0; i < POSSIBLE_PLACE_VALUES; ++i) {
addElements(buckets[i]);
}
} // splitAndJoin
On to Introduction to Lists
Back to More Sorting Techniques
Outlines:
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
Current position in syllabus
[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/98S/home/rebelsky/public_html/Courses/CS152/98S/Outlines/outline.26.html
Source text last modified Tue Jan 12 11:52:24 1999.
This page generated on Mon Jan 25 09:49:25 1999 by SiteWeaver. Validate this page.
Contact our webmaster at rebelsky@math.grin.edu