Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //XCODE
- #include <iostream>
- #include <stdio.h>
- #include <cstdlib>
- #include <ctime>
- #include <climits>
- int externalCycleCount = 0;
- int internalCycleCount = 0;
- int numberOfSwaps = 0;
- void bubble_sort(long *mas,long n)
- {
- externalCycleCount = 0;
- internalCycleCount = 0;
- numberOfSwaps = 0;
- for(int i = 1; i < n; ++i)
- {
- externalCycleCount++;
- for(int j = 0; j < n - i; ++j)
- {
- internalCycleCount++;
- if(mas[j]>mas[j+1])
- {
- std::swap(mas[j+1],mas[j]);
- numberOfSwaps++;
- }
- }
- }
- }
- void print_mas(long *mas,long n)
- {
- std::cout << "Sorted mas: ";
- for (int i = 0; i < n; i++)
- {
- std::cout << mas[i] << " ";
- }
- std::cout << std::endl;
- std::cout << "External cycles: " << externalCycleCount << std::endl;
- std::cout << "Internal cycles: " << internalCycleCount << std::endl;
- std::cout << "Number of swaps: " << numberOfSwaps << std::endl;
- }
- int main(int argc, const char * argv[]) {
- long n,k,buf;
- bool ch;
- bool per = false;
- std::srand(std::time(0));
- do
- {
- std::cout<<"Input N:";
- std::cin>>n;
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cin.ignore(INT_MAX, '\n');
- }
- }
- while(n <= 0);
- do
- {
- std::cout<<"If you want random generation - input 1\n If you want hand-made generation - input 0\n";
- std::cin>>ch;
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cin.ignore(INT_MAX, '\n');
- }
- }
- while(ch != 0 && ch != 1);
- long* mas = new long[n];
- for(int i = 0; i<n; i++){
- switch (ch) {
- case 0:
- std::cin>>mas[i];
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cin.ignore(INT_MAX, '\n');
- }
- break;
- case 1:
- mas[i] = rand();
- break;
- default:
- break;
- }
- }
- for (int i = 0; i<n; i++) {
- std::cout<<mas[i]<<" ";
- }
- std::cout<<"\n";
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n-i-1; j++) {
- if (mas[j] > mas[j+1]) {
- long b = mas[j];
- mas[j] = mas[j+1];
- mas[j+1] = b;
- per = true;
- }
- }
- if(per==false){
- break;
- }
- }
- for (int i = 0; i<n; i++) {
- std::cout<<mas[i]<<" ";
- }
- bubble_sort(mas, n);
- print_mas(mas, n);
- delete[] mas;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement