-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
51 lines (41 loc) · 965 Bytes
/
sort.c
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
/* sort.c
* Test program to sort a large number of integers.
*
* Intention is to stress virtual memory system. To increase the memory
* usage of this program, simply increase SORTSHIFT. The size of the array
* is (SORTSIZE)(2^(SORTSHIFT+2)).
*/
#include "syscall.h"
/* size of physical memory; with code, we'll run out of space! */
#define SORTSIZE 256
#define SORTSHIFT 0
int array[SORTSIZE<<SORTSHIFT];
#define A(i) (array[(i)<<SORTSHIFT])
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int
main()
{
int i, j;
/* first initialize the array, in reverse sorted order */
for (i=0; i<SORTSIZE; i++)
A(i) = (SORTSIZE-1)-i;
/* then sort! */
for (i=0; i<SORTSIZE-1; i++) {
for (j=i; j<SORTSIZE; j++) {
if (A(i) > A(j))
swap(&A(i), &A(j));
}
}
/* and last, verify */
for (i=0; i<SORTSIZE; i++) {
if (A(i) != i)
return 1;
}
/* if successful, return 0 */
return 0;
}