Bubble Sort Program With Full Introduction

Bubble Sort

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.

Bubble sort should be avoided in the case of large collections. It will not be efficient in the case of a reverse-ordered collection.


STEP 1: Repeat step 2 FOR I=0 to N-1
STEP 2: Repeat FOR J=0 to N-I-1
STEP 3: IF ARR[J] > ARR[J+1]
               SWAP ARR[J] and ARR[J+1]
                        [END OF INNER LOOP]
                        [END OF OUTER LOOP]

   int arr[100],n,i,j,temp;
   printf("\n Enter number of elements \n");
   printf("\n Enter %d integers \n", n);
   scanf("%d", &arr[i]);
   printf("\n Sorted list in ascending order: \n");
   printf("%d \n",arr[i]);

        Here is the output generated by the program-
        1st case:

       2nd case:


Bubble Sort is an algorithm that is used to sort N elements that are given in memory for e.g.: an Array with N number of elements. Bubble Sort compares all the element one by one and sorts them based on their values.
It is called Bubble sort, because with each iteration the smaller element in the list bubbles up towards the first place, just like a water bubble rises up to the water surface.
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and swapping each pair that is out of order.

Post a Comment