Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <ctime>
  2. #include <iostream>
  3. #define MAX 50 //zmieniamy z zależności od wielkości tablicy
  4. #include <string>
  5. #include <cstdlib>
  6. #include <algorithm>
  7. KOCHAM CIĘ KOCHANIE
  8. using namespace std;
  9.  
  10. void show(int *tab)
  11. {
  12. for(int i = 0; i < MAX; i++)
  13. {
  14. cout << tab[i] << "||";
  15. }
  16. }
  17.  
  18. void sort_reverse(int *tab)
  19. {
  20. sort(tab, tab + MAX, greater< int >()); // sortowanie malejące (na potrzeby testów)
  21. }
  22.  
  23. void sort_increase(int * tab)
  24. {
  25. sort(tab,tab + MAX); //sortowanie rosnące (na potrzeby testów)
  26. }
  27.  
  28. int main()
  29. {
  30. srand(time(NULL));
  31. int tab[MAX], flag;
  32.  
  33. for (int i = 0; i < MAX; i++)
  34. {
  35. tab[i] = rand() % (4*MAX-1)+ 0; //wypełniamy tablicę losowymi liczbami
  36. cout << tab[i] << " ";
  37.  
  38. if ((i + 1) % 5 == 0)
  39. {
  40. cout << endl;
  41. }
  42. }
  43. //sort_increase(tab); //funkcja sortujaca rosnaca, na potrzeby testow
  44. sort_reverse(tab); //funkcjo sortuajca malejaco, na potrzeby testow
  45.  
  46.  
  47. //bool is_sorted = false; //sprawdzanie, czy tablica jest posortowana (nie jest)
  48. int z=0;
  49. int counter_swap = 0; //licznik przestawień
  50. int counter_if = 0; //licznik porównań
  51.  
  52. int temp, j;
  53.  
  54. for( int i = 1; i < MAX; i++ )//sortowanie przez wstawianie
  55. {
  56. temp = tab[ i ];
  57.  
  58. for( j = i - 1; j >= 0 && tab[ j ] > temp; j-- )
  59. tab[ j + 1 ] = tab[ j ];
  60.  
  61. tab[ j + 1 ] = temp;
  62. }
  63.  
  64. cout << "Zamiany: "<<counter_swap<< endl;
  65. cout << "Porownania: "<<counter_if << endl;
  66. show(tab);
  67.  
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement