1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
void switch_elements( int , int , int * );
void bubble_sort( int *, const int );
int main()
{
const int SIZE = 20;
int array[SIZE] = {
1 , 2 , 6 , 3 , 8 , 10 , -3 , 0 , 0 , 8 , 0 ,
1 , 1 , -2 , -3 , -4 , -5 , -6 , -7 , -7
};
bubble_sort( array , SIZE );
for( const auto &it : array )
std::cout << it << ' ';
}
void switch_elements( int lhs , int rhs , int *array )
{
int temp = array[rhs];
array[rhs] = array[lhs];
array[lhs] = temp;
}
void bubble_sort( int *array , const int SIZE )
{
for( int i = 0; i < SIZE; ++i )
{
for( int j = 0; j < SIZE; ++j )
{
if( array[j] > array[i] )
{
switch_elements( i , j , array );
}
}
}
}
|