Advertisement
Narzew

Sortowanie

Apr 21st, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void print_tab(int tab[10], int n){
  7.     for(int i=0;i<n;i++){
  8.         cout << tab[i] << " ";
  9.     }
  10.     cout << "\n";
  11. }
  12.  
  13. void copy_table(int tablea[10], int tableb[10], int n){
  14.     // Wyzeruj pierwszą tablice
  15.     for(int i=0;i<n;i++){
  16.         tablea[i] = 0;
  17.     }
  18.     // Przekopiuj wartości
  19.     for(int i=0;i<n;i++){
  20.         tablea[i] = tableb[i];
  21.     }
  22. }
  23.  
  24. void bubble_sort(int tab[10], int n){
  25.     int temp, zamiana;
  26.     while(true){
  27.         zamiana = false;
  28.         for(int i=0;i<n-1;i++){
  29.             if(tab[i]>tab[i+1]){
  30.  
  31.                 temp = tab[i];
  32.                 tab[i] = tab[i+1];
  33.                 tab[i+1] = temp;
  34.                 zamiana = true;
  35.  
  36.             }
  37.         }
  38.         if(!zamiana){
  39.             break;
  40.         }
  41.     }
  42. }
  43.  
  44. void selection_sort(int tab[10], int n){
  45.     int min, temp;
  46.     for(int i=0;i<n-1;i++){
  47.         min = i;
  48.         for(int j=i+1;j<n;j++){
  49.             if(tab[j]<tab[min]){
  50.                 min = j;
  51.             }
  52.         }
  53.         temp = tab[min];
  54.         tab[min] = tab[i];
  55.         tab[i] = temp;
  56.     }
  57.  
  58. }
  59.  
  60. void insertion_sort(int tab[10], int n){
  61.     int temp, j;
  62.     for(int i=1;i<n;i++){
  63.         temp = tab[i];
  64.         for(j=i-1;j>=0;j--){
  65.             if(temp<tab[j]){
  66.                 tab[j+1] = tab[j];
  67.             } else {
  68.                 break;
  69.             }
  70.         }
  71.         tab[j+1] = temp;
  72.     }
  73. }
  74.  
  75. int main(){
  76.     int tab[10] = {9,7,6,3,1,2,0,4,5,8};
  77.     int tab2[10];
  78.     copy_table(tab2, tab, 10);
  79.     cout << "Nieposortowana tablica: ";
  80.     print_tab(tab, 10);
  81.     bubble_sort(tab, 10);
  82.     cout << "Sortowanie babelkowe: ";
  83.     print_tab(tab, 10);
  84.     copy_table(tab, tab2, 10);
  85.     selection_sort(tab, 10);
  86.     cout << "Sortowanie przez wybor: ";
  87.     print_tab(tab, 10);
  88.     copy_table(tab, tab2, 10);
  89.     insertion_sort(tab, 10);
  90.     cout << "Sortowanie przez wstawianie: ";
  91.     print_tab(tab, 10);
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement