-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestprog.cpp
More file actions
55 lines (46 loc) · 1.3 KB
/
testprog.cpp
File metadata and controls
55 lines (46 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include "sorting.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
template<typename T>
void print_array(const T* base, size_t nmemb)
{
for (size_t i = 0; i < nmemb; ++i)
std::cout << base[i] << ' ';
std::cout << std::endl;
}
static int cmp(const int& a, const int& b)
{
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
template <typename T>
static void sort_and_show(T* a, size_t nmemb,
int (*cmp)(const T&, const T&))
{
print_array(a, nmemb);
sorting::qsort(a, nmemb, cmp);
print_array(a, nmemb);
}
int main(void)
{
int a[] = { 2, 7, 5, 8, 9, 6, 3, 4, 1 };
int b[] = { 2, 7 };
int c[] = { 2 };
sort_and_show<int>(a, ARRAY_SIZE(a), cmp);
sort_and_show<int>(b, ARRAY_SIZE(b), cmp);
sort_and_show<int>(c, ARRAY_SIZE(c), cmp);
std::cout << "find_nth(a, 1) = "
<< sorting::find_nth_smallest(a, ARRAY_SIZE(a), 1, cmp)
<< std::endl;
std::cout << "find_nth(a, 9) = "
<< sorting::find_nth_smallest(a, ARRAY_SIZE(a), 9, cmp)
<< std::endl;
std::cout << "find_nth(a, 5) = "
<< sorting::find_nth_smallest(a, ARRAY_SIZE(a), 5, cmp)
<< std::endl;
return 0;
}
/* vim: set ts=4 sw=4 cin et: */