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 |
---|
13 | // Last Modified On : Wed Feb 17 12:21:04 2016 |
---|
14 | // Update Count : 46 |
---|
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 | |
---|
40 | double darr[size]; |
---|
41 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
42 | darr[i] = size - i + 0.5; |
---|
43 | sout | darr[i] | ", "; |
---|
44 | } // for |
---|
45 | sout | endl; |
---|
46 | qsort( darr, size ); |
---|
47 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
48 | sout | darr[i] | ", "; |
---|
49 | } // for |
---|
50 | sout | endl; |
---|
51 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
52 | double *v = bsearch( size - i + 0.5, darr, size ); |
---|
53 | sout | *v | ", "; |
---|
54 | } // for |
---|
55 | sout | endl | endl; |
---|
56 | |
---|
57 | struct S { int i, j; } sarr[size]; |
---|
58 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
59 | ofstream * ?|?( ofstream * os, S v ) { return os | v.i | ' ' | v.j; } |
---|
60 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
61 | sarr[i].i = size - i; |
---|
62 | sarr[i].j = size - i + 1; |
---|
63 | sout | sarr[i] | ", "; |
---|
64 | } // for |
---|
65 | sout | endl; |
---|
66 | qsort( sarr, size ); |
---|
67 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
68 | sout | sarr[i] | ", "; |
---|
69 | } // for |
---|
70 | sout | endl; |
---|
71 | for ( unsigned int i = 0; i < size; i += 1 ) { |
---|
72 | S temp = { size - i, size - i + 1 }; |
---|
73 | S *v = bsearch( temp, sarr, size ); |
---|
74 | sout | *v | ", "; |
---|
75 | } // for |
---|
76 | sout | endl | endl; |
---|
77 | } // main |
---|
78 | |
---|
79 | // Local Variables: // |
---|
80 | // tab-width: 4 // |
---|
81 | // compile-command: "cfa searchsort.c" // |
---|
82 | // End: // |
---|