Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void getData(vector<int>&);
- void bubbleSort(vector<int>&);
- void insertionSort(vector<int>&);
- void selectionSort(vector<int>&);
- void printVector(vector<int>&);
- void printInfo();
- int main() {
- int choose;
- vector<int> myArray;
- printInfo();
- do {
- cin >> choose;
- if(choose == 1) {
- getData(myArray);
- } else if(choose == 2) {
- bubbleSort(myArray);
- } else if(choose == 3) {
- insertionSort(myArray);
- } else if(choose == 4) {
- selectionSort(myArray);
- } else if(choose == 5) {
- printVector(myArray);
- }
- cout << endl;
- printInfo();
- } while(choose != 6);
- return 0;
- }
- void getData(vector<int>& myArray) {
- int element;
- int n;
- cout << "Podaj rozmiar tablicy: ";
- cin >> n;
- for(int i = 0; i < n; i++) {
- cout << endl << "Podaj element " << i << ": ";
- cin >> element;
- myArray.push_back(element);
- }
- }
- void bubbleSort(vector<int> & a) {
- bool swapp = true;
- while(swapp) {
- swapp = false;
- for(size_t i = 0; i < a.size()-1; i++) {
- if(a[i] > a[i + 1]) {
- a[i] += a[i + 1];
- a[i + 1] = a[i] - a[i + 1];
- a[i] -= a[i + 1];
- swapp = true;
- }
- }
- }
- cout << "Posortowalem babelkowo" << endl;
- }
- void insertionSort(vector<int> & data) {
- int i, j, tmp;
- for(i = 1; i < data.size(); i++) {
- j = i;
- tmp = data[i];
- while(j > 0 && tmp < data[j - 1]) {
- data[j] = data[j - 1];
- j--;
- }
- data[j] = tmp;
- }
- cout << "Posortowalem przez wstawianie" << endl;
- }
- void selectionSort(vector<int> & data) {
- int vecsize = data.size();
- for(int j = 0; j < vecsize - 1; ++j) {
- int minimum = j;
- for(int i = j + 1; i < vecsize; ++i) {
- if (data.at(minimum) > data.at(i)) {
- minimum = i;
- }
- }
- if (minimum != j)
- swap(data.at(j), data.at(minimum));
- }
- cout << "Posortowalem przez wybieranie" << endl;
- }
- void printVector(vector<int>& path) {
- for(vector<int>::const_iterator i = path.begin(); i != path.end(); ++i)
- cout << *i << " ";
- cout << endl << endl;
- }
- void printInfo() {
- cout << "1 - Pobranie danych" << endl;
- cout << "2 - Sortowanie babelkowe" << endl;
- cout << "3 - Sortowanie przez wstawianie" << endl;
- cout << "4 - Sortowanie przez wybieranie" << endl;
- cout << "5 - Wydruk tablicy" << endl;
- cout << "6 - Koniec programu" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement