how to return a bubble sort function in C++ code example
Example 1: bubble sort in c++
void bubble_sort( int A[ ], int n ) {
int temp;
for(int k = 0; k< n-1; k++) {
for(int i = 0; i < n-k-1; i++) {
if(A[ i ] > A[ i+1] ) {
temp = A[ i ];
A[ i ] = A[ i+1 ];
A[ i + 1] = temp;
}
}
}
}
Example 2: code for bubble sort in c++
void bubbleSort (int S[ ], int length) {
bool isSorted = false;
while(!isSorted)
{
isSorted = true;
for(int i = 0; i<length; i++)
{
if(S[i] > S[i+1])
{
int temp = S[i];
S[i] = S[i+1];
S[i+1] = temp;
isSorted = false;
}
}
length--;
}
}