Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SORTOWANIE PRZEZ WYBIERANIE
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- void gen(int tab[]) {
- srand(time(0));
- for(int i=1;i<100;i++) {
- tab[i] = rand()%100+1;
- }
- }
- void wyswietl(int tab[]) {
- cout<<"Wygenerowane liczby to: ";
- for(int i=1;i<100;i++) {
- cout<<tab[i]<<" ";
- }
- cout<<endl;
- }
- void sortuj(int tab[], int n) {
- for(int j=0;j<n-1;j++) {
- int temp, pmin = j;
- for(int i=j+1;i<n;i++)
- if(tab[i]<tab[pmin]) pmin = i;
- temp = tab[pmin];
- tab[pmin] = tab[j];
- tab[j] = temp;
- }
- }
- void wyswietlsort(int tab[]) {
- cout<<endl<<"Posortowane liczby to: ";
- for(int i=1;i<100;i++) {
- cout<<tab[i]<<" ";
- }
- cout<<endl;
- }
- int main () {
- int n = 100, tab[n];
- gen(tab);
- wyswietl(tab);
- sortuj(tab, 100);
- wyswietlsort(tab);
- }
- -------------------------------------------------------------------------------------
- // SORTOWANIE PRZEZ WSTAWIANIE
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- void gen(int tab[]) {
- srand(time(0));
- for(int i=1;i<100;i++) {
- tab[i] = rand()%100+1;
- }
- }
- void wyswietl(int tab[]) {
- cout<<"Wygenerowane liczby to: ";
- for(int i=1;i<100;i++) {
- cout<<tab[i]<<" ";
- }
- cout<<endl;
- }
- void sortuj(int tab[], int n) {
- for(int j=n-2;j>=0;j--) {
- int x = tab[j];
- int i = j+1;
- while((i<n) && (x>tab[i])) {
- tab[i-1] = tab[i];
- i++;
- }
- tab[i-1] = x;
- }
- }
- void wyswietlsort(int tab[]) {
- cout<<endl<<"Posortowane liczby to: ";
- for(int i=1;i<100;i++) {
- cout<<tab[i]<<" ";
- }
- cout<<endl;
- }
- int main () {
- int n = 100, tab[n];
- gen(tab);
- wyswietl(tab);
- sortuj(tab, 100);
- wyswietlsort(tab);
- }
Advertisement
Add Comment
Please, Sign In to add comment