[658f6de0] | 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 |
---|
[a6151ba] | 12 | // Last Modified On : Tue Jul 5 18:06:07 2016 |
---|
| 13 | // Update Count : 56 |
---|
[658f6de0] | 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 | endl; |
---|
| 38 | |
---|
[94980502] | 39 | // descending sort/search by changing < to > |
---|
| 40 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 41 | iarr[i] = i + 1; |
---|
| 42 | sout | iarr[i] | ", "; |
---|
| 43 | } // for |
---|
| 44 | sout | endl; |
---|
| 45 | { |
---|
| 46 | int ?<?( int x, int y ) { return x > y; } |
---|
| 47 | qsort( iarr, size ); |
---|
| 48 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 49 | sout | iarr[i] | ", "; |
---|
| 50 | } // for |
---|
| 51 | sout | endl; |
---|
| 52 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 53 | int *v = bsearch( size - i, iarr, size ); |
---|
| 54 | sout | *v | ", "; |
---|
| 55 | } // for |
---|
| 56 | } |
---|
| 57 | sout | endl | endl; |
---|
| 58 | |
---|
[658f6de0] | 59 | double darr[size]; |
---|
| 60 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 61 | darr[i] = size - i + 0.5; |
---|
| 62 | sout | darr[i] | ", "; |
---|
| 63 | } // for |
---|
| 64 | sout | endl; |
---|
| 65 | qsort( darr, size ); |
---|
| 66 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 67 | sout | darr[i] | ", "; |
---|
| 68 | } // for |
---|
| 69 | sout | endl; |
---|
| 70 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 71 | double *v = bsearch( size - i + 0.5, darr, size ); |
---|
| 72 | sout | *v | ", "; |
---|
| 73 | } // for |
---|
| 74 | sout | endl | endl; |
---|
| 75 | |
---|
| 76 | struct S { int i, j; } sarr[size]; |
---|
| 77 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
| 78 | ofstream * ?|?( ofstream * os, S v ) { return os | v.i | ' ' | v.j; } |
---|
| 79 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 80 | sarr[i].i = size - i; |
---|
| 81 | sarr[i].j = size - i + 1; |
---|
| 82 | sout | sarr[i] | ", "; |
---|
| 83 | } // for |
---|
| 84 | sout | endl; |
---|
| 85 | qsort( sarr, size ); |
---|
| 86 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 87 | sout | sarr[i] | ", "; |
---|
| 88 | } // for |
---|
| 89 | sout | endl; |
---|
| 90 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
| 91 | S temp = { size - i, size - i + 1 }; |
---|
| 92 | S *v = bsearch( temp, sarr, size ); |
---|
| 93 | sout | *v | ", "; |
---|
| 94 | } // for |
---|
| 95 | sout | endl | endl; |
---|
| 96 | } // main |
---|
| 97 | |
---|
| 98 | // Local Variables: // |
---|
| 99 | // tab-width: 4 // |
---|
| 100 | // compile-command: "cfa searchsort.c" // |
---|
| 101 | // End: // |
---|