Quick actions

cmd+k|ctrl+k

Navigation

Languages

BubbleSort

Snippet info

Language

Cpp

Visibility

public

Author

dimonkoko1

Created

2018-10-07T11:46:55Z

Updated

2018-10-07T11:46:55Z

#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

void bubbleSort(int *arrayPtr,int lenght_array)
{
 int temp = 0;
 bool exit = false;
 
 while(!exit)
 {
     exit = true;
     for(int int_counter = 0; int_counter < (lenght_array - 1); int_counter++)
     {
         if(arrayPtr[int_counter] > arrayPtr[int_counter + 1])
         {
             temp = arrayPtr[int_counter];
             arrayPtr[int_counter] = arrayPtr[int_counter + 1];
             arrayPtr[int_counter + 1] = temp;
             exit = false;
         }
     }
 }
}

int main() {
    srand(time(NULL));
    
    int size_array;
    cout << "Введите размер масива:" ;
    cin >> size_array;
    
    int *sorted_array = new int [size_array];
    for(int counter = 0; counter < size_array ; counter++)
    {
        sorted_array[counter] = rand() % 100;
        cout << setw(2) << sorted_array[counter] << "  ";
    }
    cout << "\n\n" ;
    
    bubbleSort(sorted_array , size_array);
    
    for(int counter = 0; counter < size_array; counter++)
    {
        cout << setw(2) << sorted_array[counter] << "   ";
    }
    
    cout << "\n";
    
    return 0;
}
INFO