vertc

28-03

Mar 28th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. // SORTOWANIE PRZEZ WYBIERANIE
  2.  
  3. #include <iostream>
  4. #include <ctime>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. void gen(int tab[]) {
  10.     srand(time(0));
  11.     for(int i=1;i<100;i++) {
  12.         tab[i] = rand()%100+1;
  13.     }
  14. }
  15.  
  16. void wyswietl(int tab[]) {
  17.     cout<<"Wygenerowane liczby to: ";
  18.     for(int i=1;i<100;i++) {
  19.         cout<<tab[i]<<" ";
  20.     }
  21.     cout<<endl;
  22. }
  23.  
  24. void sortuj(int tab[], int n) {
  25.     for(int j=0;j<n-1;j++) {
  26.         int temp, pmin = j;
  27.         for(int i=j+1;i<n;i++)
  28.             if(tab[i]<tab[pmin])    pmin = i;
  29.         temp = tab[pmin];
  30.         tab[pmin] = tab[j];
  31.         tab[j] = temp;
  32.     }
  33. }
  34.  
  35. void wyswietlsort(int tab[]) {
  36.     cout<<endl<<"Posortowane liczby to: ";
  37.     for(int i=1;i<100;i++) {
  38.         cout<<tab[i]<<" ";
  39.     }
  40.     cout<<endl;
  41.    
  42. }
  43.  
  44. int main () {
  45.     int n = 100, tab[n];
  46.     gen(tab);
  47.     wyswietl(tab);
  48.     sortuj(tab, 100);
  49.     wyswietlsort(tab);
  50. }
  51.  
  52. -------------------------------------------------------------------------------------
  53.  
  54. // SORTOWANIE PRZEZ WSTAWIANIE
  55.  
  56. #include <iostream>
  57. #include <ctime>
  58. #include <cstdlib>
  59.  
  60. using namespace std;
  61.  
  62. void gen(int tab[]) {
  63.     srand(time(0));
  64.     for(int i=1;i<100;i++) {
  65.         tab[i] = rand()%100+1;
  66.     }
  67. }
  68.  
  69. void wyswietl(int tab[]) {
  70.     cout<<"Wygenerowane liczby to: ";
  71.     for(int i=1;i<100;i++) {
  72.         cout<<tab[i]<<" ";
  73.     }
  74.     cout<<endl;
  75. }
  76.  
  77. void sortuj(int tab[], int n) {
  78.     for(int j=n-2;j>=0;j--) {
  79.         int x = tab[j];
  80.         int i = j+1;
  81.         while((i<n) && (x>tab[i])) {
  82.             tab[i-1] = tab[i];
  83.             i++;
  84.         }
  85.     tab[i-1] = x;
  86.     }
  87. }
  88.  
  89. void wyswietlsort(int tab[]) {
  90.     cout<<endl<<"Posortowane liczby to: ";
  91.     for(int i=1;i<100;i++) {
  92.         cout<<tab[i]<<" ";
  93.     }
  94.     cout<<endl;
  95.    
  96. }
  97.  
  98. int main () {
  99.     int n = 100, tab[n];
  100.     gen(tab);
  101.     wyswietl(tab);
  102.     sortuj(tab, 100);
  103.     wyswietlsort(tab);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment