Quick Sort
- Read the manpage of
qsort().
- According to the type revealed in the function prototype,
design a function cmpFunc() so that the following code can sort an array
of strings
in alphabetical order.
#include <iostream>
using std::cin;
using std::cout;
void show_array(char* a[], int n)
{
int i;
for (i=0; i<n; i++)
cout << a[i] << '\n';
cout << "=====\n";
}
int main()
{
const int N = 10;
char word[N][20];
char* a[N];
for (int i=0; i<N; i++)
{
cin >> word[i];
a[i] = &word[i][0];
}
qsort(a, N, sizeof(a[0]), cmpFunc);
show_array(a, N);
return 0;
}
The output of the program may look like the following:
Apple
apple
Alice
banana
BANANA
Coconut
Guava
Watermelon
Papaya
Grape
=====
Alice
Apple
BANANA
Coconut
Grape
Guava
Papaya
Watermelon
apple
banana