// QucikSort.cpp : Defines the entry point for the console application. #include "stdafx.h" #include #include #include using namespace std; const int N = 6; int a [N]; void quicksort (int r, int s) { cout << "quick " << r << "," << s << endl; for (int k = r; k <= s; k++) cout << a[k] << " "; cout << endl; int h; h=a[(r+s)/2]; int i=r; int j=s; while (i <= j) { while(a[i]h) j--; if ( i < j) { cout << "swap " << i << "," << j << ", a[i] = " << a[i] << ", a[j] = " << a[j] << endl; int p; p=a[i]; a[i]=a[j]; a[j]=p; for (int k = r; k <= s; k++) cout << a[k] << " "; cout << endl; } i++; j--; } cout << "next " << i << "," << j << ", h = " << h << endl; for (int k = r; k <= s; k++) cout << a[k] << " "; cout << endl; if (r < j) quicksort (r, j+1); if (i < s) quicksort (i-1, s); cout << endl; } int _tmain(int argc, _TCHAR* argv[]) { a [0] = 1; a [1] = 10; a [2] = 20; a [3] = 5; a [4] = 7; a [5] = 8; /* initialize random seed: */ /* srand (time (NULL)); for (int i = 0; i < N; i++) a[i] = rand () % 100; */ quicksort (0, N-1); bool ok = true; for (int i = 0; i < N; i++) { if (i+1 < N) if (a[i+1] < a[i]) ok = false; cout << a[i] << " "; } cout << endl; if (ok) cout << "O.K." << endl; else cout << "ERROR" << endl; cout << "Hotovo" << endl; system ("pause"); return 0; }