Bubble sort - noorulzaman/c_basics GitHub Wiki

Bubble Sort

1.Formation: Make an Array of 5,6,7,10....so on .

int apr[10] = {98,23,67,58,11,63,19,32,78};

Then we use FOR LOOP twice and then swap the bigger elements by small elements by using IF conditioner operator and then print these elements by using Printf function.

for (int i = 0; i < 10; i++)

for (int j = i+1; j < 10; j++)

  1. Functioning :

1:Right from the IF conditioner operator ,

if (apr[i] > apr[j]) { int temp = apr[i]; apr[i] = apr[j]; apr[j] = temp ; }

First of all ,

i = 0 and j = 1 ,

i = 0 and j = 2 ,

i = 0 and j = 3 ,

And so on til it approaches to 10 . During this it swaps the smaller element by using IF operator and then, it continues again just like following :

          i = 1 and j = 2

          i = 1 and j = 3

          i = 1 and j = 4

It does the same as above till we reaches the i = 10 2: At the end , we prints the value by using Printf function as follows :

for (int i = 0; i < 5; i++)

{

printf("value[%d] = %d\n",i,mar[i] );

}

SO, This is called Bubble Sort.