/* * sorting_tests.cpp * * Created on: 23.01.2013 * Author: trifon */ #include "sorting.cpp" #include using namespace std; const int MAX = 103; const int JUMP = 101; void print(int a[], int n) { for(int i = 0; i < n; i++) cout << a[i] << ' '; } bool ordered(int a[], int n) { int i = 0; while (i < n-1 && a[i] <= a[i+1]) i++; return i == n-1; } bool sortTest(void (*sort_function)(int[],int)) { int hashed[MAX] = {0}; int index = 7; for(int i = 0; i < MAX; i++) { hashed[index] = i; index = (index * JUMP) % MAX; } // print(hashed, MAX); int reverse[MAX] = {0}; for(int i = 0; i < MAX; i++) reverse[i] = MAX - i - 1; // print(reverse, MAX); int almost[MAX] = {0}; for(int i = 0; i < MAX; i++) if (i % 10 == 0) almost[i] = i * 2; else almost[i] = i; // print(almost, MAX); swapCount = 0; sort_function(hashed, MAX); if (!ordered(hashed, MAX)) return false; cout << "Сортирахме hashed с " << swapCount << " размени " << endl; swapCount = 0; sort_function(reverse, MAX); if (!ordered(reverse, MAX)) return false; cout << "Сортирахме reverse с " << swapCount << " размени " << endl; swapCount = 0; sort_function(almost, MAX); if (!ordered(almost, MAX)) return false; cout << "Сортирахме almost с " << swapCount << " размени " << endl; } int main() { sortTest(selectSort); sortTest(bubbleSort); sortTest(shakeSort); sortTest(insertSort); sortTest(shellSort); sortTest(quickSort); }