fork download
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <ctime>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. void TabMirror(int tab1[], int tab2[], int s) //kopiowanie z tab1 do tab2
  9. {
  10. for (int i = 0; i < s; ++i)
  11. {
  12. tab2[i] = tab1[i];
  13. }
  14. }
  15.  
  16. void display(int tab1[], int tab2[], int s) //wyswietlanie tablic
  17. {
  18. for (int i = 0; i < s; ++i)
  19. {
  20. cout << "tablica1: " << tab1[i] << "\t\ttablica2: " << tab2[i] << endl;
  21. }
  22. }
  23.  
  24. void quicksort(int tab2[], int left, int right)
  25. {
  26.  
  27. int v=tab2[(left+right)/2];
  28. int i,j,x;
  29. i=left;
  30. j=right;
  31. do
  32. {
  33. while (tab2[i]<v) i++;
  34. while (tab2[j]>v) j--;
  35. if (i<=j)
  36. {
  37. x=tab2[i];
  38. tab2[i]=tab2[j];
  39. tab2[j]=x;
  40. i++;
  41. j--;
  42. }
  43. }
  44. while (i<=j);
  45. if (j>left) quicksort(tab2,left, j);
  46. if (i<right) quicksort(tab2, i, right);
  47.  
  48. }
  49.  
  50. void InsertSort( int tab2[], int s )
  51. {
  52. SYSTEMTIME st;
  53. GetSystemTime(&st);
  54. int t1=st.wMinute;
  55. int t2=st.wSecond;
  56. int t3= st.wMilliseconds;
  57. int temp, k;
  58. for( int i = 1; i < s; i++ )
  59. {
  60. temp = tab2[ i ];
  61.  
  62. for( k = i - 1; k >= 0 && tab2[ k ] > temp; k-- )
  63. tab2[ k + 1 ] = tab2[ k ];
  64.  
  65. tab2[ k + 1 ] = temp;
  66. }
  67. GetSystemTime(&st);
  68. cout <<"InsertSort: " << st.wSecond-t2;
  69. if(st.wMilliseconds-t3<0)
  70. {
  71. cout << " seconds " << (st.wMilliseconds-t3)*(-1) << " ms."<< endl;;
  72.  
  73. }
  74. else
  75. cout << " seconds " << st.wMilliseconds-t3 << " ms."<< endl;
  76. }
  77.  
  78. void SelectionSort(int tab2[], int s)
  79. {
  80. SYSTEMTIME st;
  81. GetSystemTime(&st);
  82. int t1=st.wMinute;
  83. int t2=st.wSecond;
  84. int t3= st.wMilliseconds;
  85. int j,k;
  86. for(int i = 0; i < s; i++ )
  87. {
  88. k = i;
  89. for( int j = i + 1; j < s; j++ )
  90. if( tab2[ j ] < tab2[ k ] )
  91. k = j;
  92. swap( tab2[ k ], tab2[ i ] );
  93. }
  94. GetSystemTime(&st);
  95. cout <<"SelectionSort: " << st.wSecond-t2;
  96. if(st.wMilliseconds-t3<0)
  97. {
  98. cout << " seconds " << (st.wMilliseconds-t3)*(-1) << " ms."<< endl;;
  99.  
  100. }
  101. else
  102. cout << " seconds " << st.wMilliseconds-t3 << " ms."<< endl;
  103. }
  104.  
  105. void BubbleSort (int tab2[], int s)
  106.  
  107. {
  108. SYSTEMTIME st;
  109. GetSystemTime(&st);
  110. int t1=st.wMinute;
  111. int t2=st.wSecond;
  112. int t3= st.wMilliseconds;
  113. int k;
  114. for(int i = 0; i < s; i++ )
  115. {
  116. for(k = s; k>=0-1; k-- )
  117. {
  118. if( tab2[k]>tab2[k+1])
  119. swap(tab2[k],tab2[k+1]);
  120. }
  121. }
  122. GetSystemTime(&st);
  123. cout <<"BubbleSort: " << st.wSecond-t2;
  124. if(st.wMilliseconds-t3<0)
  125. {
  126. cout << " seconds " << (st.wMilliseconds-t3)*(-1) << " ms."<< endl;;
  127.  
  128. }
  129. else
  130. cout << " seconds " << st.wMilliseconds-t3 << " ms."<< endl;
  131. }
  132. int main()
  133. {
  134. //int s = 5000; //size
  135. int s;
  136. cout << "Compare sorting algorithms efficiency!" << endl << "Press ENTER to continue." << endl;
  137. cin.get();
  138. cout << "Insert array size (recomended 5000 - 100000)" << endl;
  139. cin >> s;
  140. int tab1[s], tab2[s];
  141. //2x random array
  142. srand((unsigned)time(0));
  143. for(int i=0; i<s; i++)
  144. {
  145. tab1[i] = (rand()%1000)+1;
  146. tab2[i]=tab1[i];
  147. }
  148. //SelectionSort
  149. SelectionSort(tab2,s);
  150. //copy array
  151. TabMirror(tab1,tab2,s);
  152. //bubble
  153. BubbleSort(tab2,s);
  154. //copy array
  155. TabMirror(tab1,tab2,s);
  156. //sortowanie szybkie
  157. SYSTEMTIME st;
  158. GetSystemTime(&st);
  159. int t2=st.wSecond;
  160. int t3=st.wMilliseconds;
  161.  
  162. quicksort(tab2,0,s-1);
  163.  
  164. GetSystemTime(&st);
  165. cout <<"QuickSort: " << st.wSecond-t2 << " seconds " << st.wMilliseconds-t3 << " ms."<< endl;
  166. //copy array
  167. TabMirror(tab1,tab2,s);
  168. //InsertSort
  169. InsertSort(tab2,s);
  170. return 0;
  171. }
  172.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:21: fatal error: windows.h: No such file or directory
 #include <windows.h>
                     ^
compilation terminated.
stdout
Standard output is empty