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