Bubble sort, sometimes shortened to bubblesort, also known as exchange sort, is a simple sorting algorithm.

It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted. The algorithm gets its name from the way smaller elements “bubble” to the top (i.e. the beginning) of the list via the swaps. Because it only uses comparisons to operate on elements, it is a comparison sort.

Video of how bubble sort works:

C++ CodeC Code

#include <iostream>
using namespace std;

int main()
{
int array[9]={4,3,5,1,2,0,7,9,4};
for(int i=0; i<9; i++)
for(int j=0; j
if(array[i]
{
int temp = array[j];
array[j]= array[i];
array[i]=temp;
}

for(int i=0; i<9;i++)
cout << array[i] << endl;
}


One Response to “What is Bubble sort and how does it work?”  

  1. 1 Dave

    the video wasn’t really a bubble sort because the people at the end (the sorted people) still checked behind them

Leave a Reply


*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image