Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- void print_tab(int tab[10], int n){
- for(int i=0;i<n;i++){
- cout << tab[i] << " ";
- }
- cout << "\n";
- }
- void copy_table(int tablea[10], int tableb[10], int n){
- // Wyzeruj pierwszą tablice
- for(int i=0;i<n;i++){
- tablea[i] = 0;
- }
- // Przekopiuj wartości
- for(int i=0;i<n;i++){
- tablea[i] = tableb[i];
- }
- }
- void bubble_sort(int tab[10], int n){
- int temp, zamiana;
- while(true){
- zamiana = false;
- for(int i=0;i<n-1;i++){
- if(tab[i]>tab[i+1]){
- temp = tab[i];
- tab[i] = tab[i+1];
- tab[i+1] = temp;
- zamiana = true;
- }
- }
- if(!zamiana){
- break;
- }
- }
- }
- void selection_sort(int tab[10], int n){
- int min, temp;
- for(int i=0;i<n-1;i++){
- min = i;
- for(int j=i+1;j<n;j++){
- if(tab[j]<tab[min]){
- min = j;
- }
- }
- temp = tab[min];
- tab[min] = tab[i];
- tab[i] = temp;
- }
- }
- void insertion_sort(int tab[10], int n){
- int temp, j;
- for(int i=1;i<n;i++){
- temp = tab[i];
- for(j=i-1;j>=0;j--){
- if(temp<tab[j]){
- tab[j+1] = tab[j];
- } else {
- break;
- }
- }
- tab[j+1] = temp;
- }
- }
- int main(){
- int tab[10] = {9,7,6,3,1,2,0,4,5,8};
- int tab2[10];
- copy_table(tab2, tab, 10);
- cout << "Nieposortowana tablica: ";
- print_tab(tab, 10);
- bubble_sort(tab, 10);
- cout << "Sortowanie babelkowe: ";
- print_tab(tab, 10);
- copy_table(tab, tab2, 10);
- selection_sort(tab, 10);
- cout << "Sortowanie przez wybor: ";
- print_tab(tab, 10);
- copy_table(tab, tab2, 10);
- insertion_sort(tab, 10);
- cout << "Sortowanie przez wstawianie: ";
- print_tab(tab, 10);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement