#include <iostream> using namespace std; void ExchangeSortDesc(int a[], int sz) { if (sz < 2) return; for (int i = 0; i < sz - 1; i++) { for (int j = i + 1; j < sz; j++) { if (a[i] < a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; } } } } int main() { int a[] = {1, 2, 3, 10, 8, 7, 9, 5, 6, 4}; #define ITMES_NUM (sizeof(a)/sizeof(int)) // output original for (unsigned int i = 0; i < ITMES_NUM; i++) { cout << a[i] << " "; } cout << endl; // sort it ExchangeSortDesc(a, ITMES_NUM); // output result for (unsigned int i = 0; i < ITMES_NUM; i++) { cout << a[i] << " "; } cout << endl; return 0; }
Monday, April 19, 2010
C++ exercises: exchange sort
Q: Write an exchange sort function to sort the list in descending order.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment