Bubble Sort in C++

Bubble Sort:

The algorithm and detailed explanation is shared here: https://rishabhjainiitbhu.blogspot.com/2019/08/sorting-algorithms.html#more

The basic algorithm was:
Start at the beginning of an array. Pass through the array, while swapping consecutive elements if not in the given order.

Here is the associated code for the bubble sort in c++

void
bubbleSwap(int* arr, int length){
for (size_t i = 0; i < length; i++) {
bool swaped = false;
for (size_t j = 0; j < length-i; j++) {
if(arr[j]>arr[j+1]){
swap(&arr[j], &arr[j+1]);
swaped = true;
}
}
if(swaped == false){
break;
}
}
}

No comments:

Post a Comment

Installing albert on ubuntu 19.04

Installing Albert on Ubuntu 19.04... Albert is not still released for ubuntu 19.04. But still, you can install it using the following ...