// sortowanie.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "conio.h" #include "windows.h" #include <iostream> #include <cstdlib> #include <ctime> #include <fstream> // zapis wyników do pliku using namespace std; const unsigned int ROZMIAR = 100; // ilosc elementow tablicy const int MAXVAL = 1000; // najwieksza mozliwa wylosowana liczba const unsigned int ILOSC1K = 1024; //1k elementów const unsigned int ILOSC2K = 2048; //2k elementów const unsigned int ILOSC4K = 4096; //4k elementów const unsigned int ILOSC8K = 8182; //8k elementów const unsigned int ILOSC16K = 16384; //16k elementów const unsigned int ILOSC32K = 32768; //32k elementów const unsigned int ILOSC64K = 65536; //64k elementów const unsigned int ILOSC128K = 131072; //128k elementów int tab[ROZMIAR]; int tab1k[ILOSC1K]; int tab2k[ILOSC2K]; int tab4k[ILOSC4K]; int tab8k[ILOSC8K]; int tab16k[ILOSC16K]; int tab32k[ILOSC32K]; int tab64k[ILOSC64K]; int tab128k[ILOSC128K]; void Disp(int *t, int n) { int i; for (i=0;i<n;i++) cout << t[i] << " "; cout << endl << endl; } void BubbleSort(int *t, int n) { int i,j, temp; bool czy_posortowana = true; for (j=0;j<n-1;j++){ czy_posortowana = true; for (i=0;i<n-1-j;i++) if (t[i]>t[i+1]) { czy_posortowana = false; temp = t[i+1]; // zamiana elementow t[i+1] = t[i]; t[i] = temp; } if (czy_posortowana == true) return; } } void BubbleSortBackwards(int *t, int n) { int i,j, temp; bool czy_posortowana = true; for (j=0;j<n-1;j++){ czy_posortowana = true; for (i=0;i<n-1-j;i++) if (t[i]<t[i+1]) { czy_posortowana = false; temp = t[i+1]; // zamiana elementow t[i+1] = t[i]; t[i] = temp; } if (czy_posortowana == true) return; } } void InsertionSort(int *t, int n) { int i, j, temp; for (i=1;i<n;i++) { temp = t[i]; j = i-1; while (j>=0 && t[j] > temp) { t[j+1]=t[j]; j--; } t[j+1]=temp; } } void Merge(int *t,int p, int sr, int k) { int i=sr+1,j=p,x=0,q=p; int tempTab[ROZMIAR]; for (x=p;x<=k;x++) tempTab[x] = t[x]; while (j<=sr && i<=k){ if (tempTab[j]>tempTab[i]) t[q++]=tempTab[i++]; else t[q++]=tempTab[j++]; } while (j<=sr) t[q++]=tempTab[j++]; } void MergeSort(int *t, int p, int k) { int sr = (p+k)/2; if (p<k) { MergeSort(t,p,sr); MergeSort(t,sr+1,k); Merge(t,p,sr,k); } } void QuickSort(int *t, int left, int right) { int i=left; int j=right; int x=t[(left+right)/2]; do{ while(t[i]<x) i++; while(t[j]>x) j--; if(i<=j){ int temp=t[i]; t[i]=t[j]; t[j]=temp; i++; j--; } }while(i<=j); if(left<j) QuickSort(t,left,j); if(right>i) QuickSort(t,i,right); } void Init(int *t, int n, int maxVal, int typ_danych = 0) { int i; for (i=0;i<n;i++) t[i] = rand()%(maxVal+1); if (typ_danych == 1) //tablica posortowana BubbleSort(t,n); if (typ_danych == 2) //tablica odwrotnie posortowana BubbleSortBackwards(t,n); } void TestBubbleSort() { /****** TEST DLA BubbleSort ******/ /**** losowa tablica [0,1000] ****/ time_t start, end; cout << "BubbleSort, dane: losowe liczby [0,1000]" << endl << endl; // 1k: Init (tab1k, ILOSC1K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC1K); end = GetTickCount(); cout << "1k: " << end - start << "ms" << endl << endl; // 2k: Init (tab2k, ILOSC2K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC2K); end = GetTickCount(); cout << "2k: " << end - start << "ms" << endl << endl; // 4k: Init (tab4k, ILOSC4K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC4K); end = GetTickCount(); cout << "4k: " << end - start << "ms" << endl << endl; // 8k: Init (tab8k, ILOSC8K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC8K); end = GetTickCount(); cout << "8k: " << end - start << "ms" << endl << endl; // 16k: Init (tab16k, ILOSC16K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC16K); end = GetTickCount(); cout << "16k: " << end - start << "ms" << endl << endl; // 32k: Init (tab32k, ILOSC32K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC32K); end = GetTickCount(); cout << "32k: " << end - start << "ms" << endl << endl; // 64k: Init (tab64k, ILOSC64K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC64K); end = GetTickCount(); cout << "64k: " << end - start << "ms" << endl << endl; // 128k: Init (tab128k, ILOSC128K, MAXVAL, 0); // tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC128K); end = GetTickCount(); cout << "128k: " << end - start << "ms" << endl << endl; /**** posortowane liczby [0,1000] ****/ cout << "BubbleSort, dane: posortowane liczby [0,1000]" << endl << endl; // 1k: Init (tab1k, ILOSC1K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC1K); end = GetTickCount(); cout << "1k: " << end - start << "ms" << endl << endl; // 2k: Init (tab2k, ILOSC2K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC2K); end = GetTickCount(); cout << "2k: " << end - start << "ms" << endl << endl; // 4k: Init (tab4k, ILOSC4K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC4K); end = GetTickCount(); cout << "4k: " << end - start << "ms" << endl << endl; // 8k: Init (tab8k, ILOSC8K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC8K); end = GetTickCount(); cout << "8k: " << end - start << "ms" << endl << endl; // 16k: Init (tab16k, ILOSC16K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC16K); end = GetTickCount(); cout << "16k: " << end - start << "ms" << endl << endl; // 32k: Init (tab32k, ILOSC32K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC32K); end = GetTickCount(); cout << "32k: " << end - start << "ms" << endl << endl; // 64k: Init (tab64k, ILOSC64K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC64K); end = GetTickCount(); cout << "64k: " << end - start << "ms" << endl << endl; // 128k: Init (tab128k, ILOSC128K, MAXVAL, 1); // posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC128K); end = GetTickCount(); cout << "128k: " << end - start << "ms" << endl << endl; /**** odwrotnie posortowane liczby [0,1000] ****/ cout << "BubbleSort, dane: odwrotnie posortowane liczby [0,1000]" << endl << endl; // 1k: Init (tab1k, ILOSC1K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC1K); end = GetTickCount(); cout << "1k: " << end - start << "ms" << endl << endl; // 2k: Init (tab2k, ILOSC2K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC2K); end = GetTickCount(); cout << "2k: " << end - start << "ms" << endl << endl; // 4k: Init (tab4k, ILOSC4K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC4K); end = GetTickCount(); cout << "4k: " << end - start << "ms" << endl << endl; // 8k: Init (tab8k, ILOSC8K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC8K); end = GetTickCount(); cout << "8k: " << end - start << "ms" << endl << endl; // 16k: Init (tab16k, ILOSC16K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC16K); end = GetTickCount(); cout << "16k: " << end - start << "ms" << endl << endl; // 32k: Init (tab32k, ILOSC32K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC32K); end = GetTickCount(); cout << "32k: " << end - start << "ms" << endl << endl; // 64k: Init (tab64k, ILOSC64K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC64K); end = GetTickCount(); cout << "64k: " << end - start << "ms" << endl << endl; // 128k: Init (tab128k, ILOSC128K, MAXVAL, 2); // odwrotnie posortowana tablica [0,1000] start = GetTickCount(); BubbleSort(tab,ILOSC128K); end = GetTickCount(); cout << "128k: " << end - start << "ms" << endl << endl; /* 4 typ danych tutaj */ } int _tmain(int argc, _TCHAR* ...
Wujek_Misiek