[73abe95] | 1 | // |
---|
[658f6de0] | 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. |
---|
[73abe95] | 6 | // |
---|
| 7 | // searchsort.c -- |
---|
| 8 | // |
---|
[658f6de0] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Feb 4 18:17:50 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[8f34661] | 12 | // Last Modified On : Thu Aug 9 07:54:57 2018 |
---|
| 13 | // Update Count : 101 |
---|
[73abe95] | 14 | // |
---|
[658f6de0] | 15 | |
---|
[73abe95] | 16 | #include <fstream.hfa> |
---|
| 17 | #include <stdlib.hfa> // bsearch, qsort |
---|
[f2cdc44] | 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; } |
---|
[658f6de0] | 21 | |
---|
| 22 | int main( void ) { |
---|
| 23 | const unsigned int size = 10; |
---|
| 24 | int iarr[size]; |
---|
| 25 | |
---|
[8f34661] | 26 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 27 | iarr[i] = size - i; |
---|
| 28 | sout | iarr[i] | ", "; |
---|
| 29 | } // for |
---|
[f2cdc44] | 30 | sout | endl | endl; |
---|
| 31 | |
---|
| 32 | // ascending sort/search by changing < to > |
---|
[658f6de0] | 33 | qsort( iarr, size ); |
---|
[8f34661] | 34 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 35 | sout | iarr[i] | ", "; |
---|
| 36 | } // for |
---|
| 37 | sout | endl; |
---|
[8f34661] | 38 | for ( i; 0u ~ size ) { // C version |
---|
[f2cdc44] | 39 | int key = size - i; |
---|
[9c47a47] | 40 | int * v = bsearch( &key, iarr, size, sizeof( iarr[0] ), comp ); |
---|
| 41 | sout | key | ':' | *v | ", "; |
---|
[f2cdc44] | 42 | } // for |
---|
| 43 | sout | endl; |
---|
[9c47a47] | 44 | |
---|
[8f34661] | 45 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 46 | int * v = bsearch( size - i, iarr, size ); |
---|
| 47 | sout | size - i | ':' | *v | ", "; |
---|
[658f6de0] | 48 | } // for |
---|
[707446a] | 49 | sout | endl; |
---|
[8f34661] | 50 | for ( i; 0u ~ size ) { |
---|
[707446a] | 51 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
[9c47a47] | 52 | sout | size - i | ':' | iarr[posn] | ", "; |
---|
[707446a] | 53 | } // for |
---|
[658f6de0] | 54 | sout | endl | endl; |
---|
| 55 | |
---|
[94980502] | 56 | // descending sort/search by changing < to > |
---|
[8f34661] | 57 | for ( i; 0u ~ size ) { |
---|
[94980502] | 58 | iarr[i] = i + 1; |
---|
| 59 | sout | iarr[i] | ", "; |
---|
| 60 | } // for |
---|
| 61 | sout | endl; |
---|
| 62 | { |
---|
| 63 | int ?<?( int x, int y ) { return x > y; } |
---|
| 64 | qsort( iarr, size ); |
---|
[8f34661] | 65 | for ( i; 0u ~ size ) { |
---|
[94980502] | 66 | sout | iarr[i] | ", "; |
---|
| 67 | } // for |
---|
| 68 | sout | endl; |
---|
[8f34661] | 69 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 70 | int * v = bsearch( size - i, iarr, size ); |
---|
| 71 | sout | size - i | ':' | *v | ", "; |
---|
[94980502] | 72 | } // for |
---|
[707446a] | 73 | sout | endl; |
---|
[8f34661] | 74 | for ( i; 0u ~ size ) { |
---|
[707446a] | 75 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
[9c47a47] | 76 | sout | size - i | ':' | iarr[posn] | ", "; |
---|
[707446a] | 77 | } // for |
---|
[94980502] | 78 | } |
---|
| 79 | sout | endl | endl; |
---|
| 80 | |
---|
[658f6de0] | 81 | double darr[size]; |
---|
[8f34661] | 82 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 83 | darr[i] = size - i + 0.5; |
---|
| 84 | sout | darr[i] | ", "; |
---|
| 85 | } // for |
---|
| 86 | sout | endl; |
---|
| 87 | qsort( darr, size ); |
---|
[8f34661] | 88 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 89 | sout | darr[i] | ", "; |
---|
| 90 | } // for |
---|
| 91 | sout | endl; |
---|
[8f34661] | 92 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 93 | double * v = bsearch( size - i + 0.5, darr, size ); |
---|
| 94 | sout | size - i + 0.5 | ':' | *v | ", "; |
---|
[658f6de0] | 95 | } // for |
---|
[707446a] | 96 | sout | endl; |
---|
[8f34661] | 97 | for ( i; 0u ~ size ) { |
---|
[707446a] | 98 | unsigned int posn = bsearch( size - i + 0.5, darr, size ); |
---|
[9c47a47] | 99 | sout | size - i + 0.5 | ':' | darr[posn] | ", "; |
---|
[707446a] | 100 | } // for |
---|
[658f6de0] | 101 | sout | endl | endl; |
---|
| 102 | |
---|
| 103 | struct S { int i, j; } sarr[size]; |
---|
| 104 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
[09687aa] | 105 | ofstream & ?|?( ofstream & os, S v ) { return os | v.i | ' ' | v.j; } |
---|
[8f34661] | 106 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 107 | sarr[i].i = size - i; |
---|
| 108 | sarr[i].j = size - i + 1; |
---|
| 109 | sout | sarr[i] | ", "; |
---|
| 110 | } // for |
---|
| 111 | sout | endl; |
---|
| 112 | qsort( sarr, size ); |
---|
[8f34661] | 113 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 114 | sout | sarr[i] | ", "; |
---|
| 115 | } // for |
---|
| 116 | sout | endl; |
---|
[8f34661] | 117 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 118 | S temp = { size - i, size - i + 1 }; |
---|
[9c47a47] | 119 | S * v = bsearch( temp, sarr, size ); |
---|
| 120 | sout | temp | ':' | *v | ", "; |
---|
[658f6de0] | 121 | } // for |
---|
[707446a] | 122 | sout | endl; |
---|
[8f34661] | 123 | for ( i; 0u ~ size ) { |
---|
[707446a] | 124 | S temp = { size - i, size - i + 1 }; |
---|
| 125 | unsigned int posn = bsearch( temp, sarr, size ); |
---|
[9c47a47] | 126 | sout | temp | ':' | sarr[posn] | ", "; |
---|
[707446a] | 127 | } // for |
---|
[658f6de0] | 128 | sout | endl | endl; |
---|
[9c47a47] | 129 | { |
---|
[93cdd5c] | 130 | unsigned int getKey( const S & s ) { return s.j; } |
---|
[8f34661] | 131 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 132 | sout | sarr[i] | ", "; |
---|
| 133 | } // for |
---|
| 134 | sout | endl; |
---|
[8f34661] | 135 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 136 | S * v = bsearch( size - i + 1, sarr, size ); |
---|
| 137 | sout | size - i + 1 | ':' | *v | ", "; |
---|
| 138 | } // for |
---|
| 139 | sout | endl; |
---|
[8f34661] | 140 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 141 | unsigned int posn = bsearch( size - i + 1, sarr, size ); |
---|
| 142 | sout | size - i + 1 | ':' | sarr[posn] | ", "; |
---|
| 143 | } // for |
---|
| 144 | sout | endl | endl; |
---|
| 145 | } |
---|
[658f6de0] | 146 | } // main |
---|
| 147 | |
---|
| 148 | // Local Variables: // |
---|
| 149 | // tab-width: 4 // |
---|
| 150 | // compile-command: "cfa searchsort.c" // |
---|
| 151 | // End: // |
---|