/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- sort
- initarray
- check
- main
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3 * The President and Fellows of Harvard College.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /* sort.c
31 * Test program to sort a large number of integers.
32 *
33 * Intention is to stress virtual memory system.
34 *
35 * Once the virtual memory assignment is complete, your system
36 * should survive this.
37 */
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <err.h>
42
43 /* Larger than physical memory */
44 #define SIZE (144*1024)
45
46
47 /*
48 * Quicksort.
49 *
50 * This used to be a bubble sort, which was ok but slow in nachos with
51 * 4k of memory and SIZE of 1024. However, with SIZE of 147,456 bubble
52 * sort is completely unacceptable.
53 *
54 * Also, quicksort has somewhat more interesting memory usage patterns.
55 */
56
57 static
58 void
59 sort(int *arr, int size)
60 {
61 static int tmp[SIZE];
62 int pivot, i, j, k;
63
64 if (size<2) {
65 return;
66 }
67
68 pivot = size/2;
69 sort(arr, pivot);
70 sort(&arr[pivot], size-pivot);
71
72 i = 0;
73 j = pivot;
74 k = 0;
75 while (i<pivot && j<size) {
76 if (arr[i] < arr[j]) {
77 tmp[k++] = arr[i++];
78 }
79 else {
80 tmp[k++] = arr[j++];
81 }
82 }
83 while (i<pivot) {
84 tmp[k++] = arr[i++];
85 }
86 while (j<size) {
87 tmp[k++] = arr[j++];
88 }
89
90 memcpy(arr, tmp, size*sizeof(int));
91 }
92
93 ////////////////////////////////////////////////////////////
94
95 static int A[SIZE];
96
97 static
98 void
99 initarray(void)
100 {
101 int i;
102
103 /*
104 * Initialize the array, with pseudo-random but deterministic contents.
105 */
106 srandom(533);
107
108 for (i = 0; i < SIZE; i++) {
109 A[i] = random();
110 }
111 }
112
113 static
114 void
115 check(void)
116 {
117 int i;
118
119 for (i=0; i<SIZE-1; i++) {
120 if (A[i] > A[i+1]) {
121 errx(1, "Failed: A[%d] is %d, A[%d] is %d",
122 i, A[i], i+1, A[i+1]);
123 }
124 }
125 warnx("Passed.");
126 }
127
128 int
129 main(void)
130 {
131 initarray();
132 sort(A, SIZE);
133 check();
134 return 0;
135 }