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 : Thu Dec 7 09:14:06 2017 |
---|
13 | // Update Count : 77 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream> |
---|
17 | #include <stdlib> // bsearch, qsort |
---|
18 | #include <stdlib.h> // C version of bsearch |
---|
19 | |
---|
20 | int comp( const void * t1, const void * t2 ) { return *(int *)t1 < *(int *)t2 ? -1 : *(int *)t2 < *(int *)t1 ? 1 : 0; } |
---|
21 | |
---|
22 | int main( void ) { |
---|
23 | const unsigned int size = 10; |
---|
24 | int iarr[size]; |
---|
25 | |
---|
26 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
27 | iarr[i] = size - i; |
---|
28 | sout | iarr[i] | ", "; |
---|
29 | } // for |
---|
30 | sout | endl | endl; |
---|
31 | |
---|
32 | // ascending sort/search by changing < to > |
---|
33 | qsort( iarr, size ); |
---|
34 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
35 | sout | iarr[i] | ", "; |
---|
36 | } // for |
---|
37 | sout | endl; |
---|
38 | for ( unsigned int i = 0; i < size; i += 1 ) { // C version |
---|
39 | int key = size - i; |
---|
40 | int *v = bsearch( &key, iarr, size, sizeof( iarr[0] ), comp ); |
---|
41 | sout | *v | ", "; |
---|
42 | } // for |
---|
43 | sout | endl; |
---|
44 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
45 | int *v = bsearch( size - i, iarr, size ); |
---|
46 | sout | *v | ", "; |
---|
47 | } // for |
---|
48 | sout | endl; |
---|
49 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
50 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
51 | sout | iarr[posn] | ", "; |
---|
52 | } // for |
---|
53 | sout | endl | endl; |
---|
54 | |
---|
55 | // descending sort/search by changing < to > |
---|
56 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
57 | iarr[i] = i + 1; |
---|
58 | sout | iarr[i] | ", "; |
---|
59 | } // for |
---|
60 | sout | endl; |
---|
61 | { |
---|
62 | int ?<?( int x, int y ) { return x > y; } |
---|
63 | qsort( iarr, size ); |
---|
64 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
65 | sout | iarr[i] | ", "; |
---|
66 | } // for |
---|
67 | sout | endl; |
---|
68 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
69 | int *v = bsearch( size - i, iarr, size ); |
---|
70 | sout | *v | ", "; |
---|
71 | } // for |
---|
72 | sout | endl; |
---|
73 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
74 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
75 | sout | iarr[posn] | ", "; |
---|
76 | } // for |
---|
77 | } |
---|
78 | sout | endl | endl; |
---|
79 | |
---|
80 | double darr[size]; |
---|
81 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
82 | darr[i] = size - i + 0.5; |
---|
83 | sout | darr[i] | ", "; |
---|
84 | } // for |
---|
85 | sout | endl; |
---|
86 | qsort( darr, size ); |
---|
87 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
88 | sout | darr[i] | ", "; |
---|
89 | } // for |
---|
90 | sout | endl; |
---|
91 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
92 | double *v = bsearch( size - i + 0.5, darr, size ); |
---|
93 | sout | *v | ", "; |
---|
94 | } // for |
---|
95 | sout | endl; |
---|
96 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
97 | unsigned int posn = bsearch( size - i + 0.5, darr, size ); |
---|
98 | sout | darr[posn] | ", "; |
---|
99 | } // for |
---|
100 | sout | endl | endl; |
---|
101 | |
---|
102 | struct S { int i, j; } sarr[size]; |
---|
103 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
104 | ofstream & ?|?( ofstream & os, S v ) { return os | v.i | ' ' | v.j; } |
---|
105 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
106 | sarr[i].i = size - i; |
---|
107 | sarr[i].j = size - i + 1; |
---|
108 | sout | sarr[i] | ", "; |
---|
109 | } // for |
---|
110 | sout | endl; |
---|
111 | qsort( sarr, size ); |
---|
112 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
113 | sout | sarr[i] | ", "; |
---|
114 | } // for |
---|
115 | sout | endl; |
---|
116 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
117 | S temp = { size - i, size - i + 1 }; |
---|
118 | S *v = bsearch( temp, sarr, size ); |
---|
119 | sout | *v | ", "; |
---|
120 | } // for |
---|
121 | sout | endl; |
---|
122 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
123 | S temp = { size - i, size - i + 1 }; |
---|
124 | unsigned int posn = bsearch( temp, sarr, size ); |
---|
125 | sout | sarr[posn] | ", "; |
---|
126 | } // for |
---|
127 | sout | endl | endl; |
---|
128 | } // main |
---|
129 | |
---|
130 | // Local Variables: // |
---|
131 | // tab-width: 4 // |
---|
132 | // compile-command: "cfa searchsort.c" // |
---|
133 | // End: // |
---|