1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // searchsort.c -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Thu Feb 4 18:17:50 2016 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sat Apr 1 19:10:12 2017 |
---|
13 | // Update Count : 62 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream> |
---|
17 | #include <stdlib> // bsearch, qsort |
---|
18 | |
---|
19 | int main( void ) { |
---|
20 | const unsigned int size = 10; |
---|
21 | int iarr[size]; |
---|
22 | |
---|
23 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
24 | iarr[i] = size - i; |
---|
25 | sout | iarr[i] | ", "; |
---|
26 | } // for |
---|
27 | sout | endl; |
---|
28 | qsort( iarr, size ); |
---|
29 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
30 | sout | iarr[i] | ", "; |
---|
31 | } // for |
---|
32 | sout | endl; |
---|
33 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
34 | int *v = bsearch( size - i, iarr, size ); |
---|
35 | sout | *v | ", "; |
---|
36 | } // for |
---|
37 | sout | endl; |
---|
38 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
39 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
40 | sout | iarr[posn] | ", "; |
---|
41 | } // for |
---|
42 | sout | endl | endl; |
---|
43 | |
---|
44 | // descending sort/search by changing < to > |
---|
45 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
46 | iarr[i] = i + 1; |
---|
47 | sout | iarr[i] | ", "; |
---|
48 | } // for |
---|
49 | sout | endl; |
---|
50 | { |
---|
51 | int ?<?( int x, int y ) { return x > y; } |
---|
52 | qsort( iarr, size ); |
---|
53 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
54 | sout | iarr[i] | ", "; |
---|
55 | } // for |
---|
56 | sout | endl; |
---|
57 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
58 | int *v = bsearch( size - i, iarr, size ); |
---|
59 | sout | *v | ", "; |
---|
60 | } // for |
---|
61 | sout | endl; |
---|
62 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
63 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
64 | sout | iarr[posn] | ", "; |
---|
65 | } // for |
---|
66 | } |
---|
67 | sout | endl | endl; |
---|
68 | |
---|
69 | double darr[size]; |
---|
70 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
71 | darr[i] = size - i + 0.5; |
---|
72 | sout | darr[i] | ", "; |
---|
73 | } // for |
---|
74 | sout | endl; |
---|
75 | qsort( darr, size ); |
---|
76 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
77 | sout | darr[i] | ", "; |
---|
78 | } // for |
---|
79 | sout | endl; |
---|
80 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
81 | double *v = bsearch( size - i + 0.5, darr, size ); |
---|
82 | sout | *v | ", "; |
---|
83 | } // for |
---|
84 | sout | endl; |
---|
85 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
86 | unsigned int posn = bsearch( size - i + 0.5, darr, size ); |
---|
87 | sout | darr[posn] | ", "; |
---|
88 | } // for |
---|
89 | sout | endl | endl; |
---|
90 | |
---|
91 | struct S { int i, j; } sarr[size]; |
---|
92 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
93 | ofstream * ?|?( ofstream * os, S v ) { return os | v.i | ' ' | v.j; } |
---|
94 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
95 | sarr[i].i = size - i; |
---|
96 | sarr[i].j = size - i + 1; |
---|
97 | sout | sarr[i] | ", "; |
---|
98 | } // for |
---|
99 | sout | endl; |
---|
100 | qsort( sarr, size ); |
---|
101 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
102 | sout | sarr[i] | ", "; |
---|
103 | } // for |
---|
104 | sout | endl; |
---|
105 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
106 | S temp = { size - i, size - i + 1 }; |
---|
107 | S *v = bsearch( temp, sarr, size ); |
---|
108 | sout | *v | ", "; |
---|
109 | } // for |
---|
110 | sout | endl; |
---|
111 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
112 | S temp = { size - i, size - i + 1 }; |
---|
113 | unsigned int posn = bsearch( temp, sarr, size ); |
---|
114 | sout | sarr[posn] | ", "; |
---|
115 | } // for |
---|
116 | sout | endl | endl; |
---|
117 | } // main |
---|
118 | |
---|
119 | // Local Variables: // |
---|
120 | // tab-width: 4 // |
---|
121 | // compile-command: "cfa searchsort.c" // |
---|
122 | // End: // |
---|