Algorithm: Bubble Sorting
Dec 29, 2020
Maybe the simplest but the most inefficient sorting algorithm
Algorithm
Characteristics
- O(n²)
Implementation
python
def bubblesort(list):
for i in range(len(list) - 1, 0, -1):
for j in range(i):
if list[j] > list[j+1]:
temp = list[j]
list[j] = list[j+1]
list[j+1] = temp
return listlist = [6,20,8,19,56,23,89,41,49,9]
print(bubblesort(list))
c++
#include <iostream>
using namespace std;void bubble(int a[], int s){
for(int i = s-1; i >= 0; i--){
for(int j = 0; j < i; j++){
if (a[j] > a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}int main()
{
int a[10] = {6,20,8,19,56,23,87,41,49,13};
bubble(a, 10);
for(int i = 0; i < 10; i++){
cout << a[i] << endl;
}
}