Bubble Sort Program With Full Introduction - CodeTextPro | Web Tutorial | Technology

CodeTextPro.com Provides - Web design, Technology gyan, Digital marketing, Online earnings, Seo, Adsense, Technology news, Motivation and inspiration, Latest Technology Affairs, Gadgets, Blogging

Tuesday, January 8, 2019

Bubble Sort Program With Full Introduction

Bubble Sort
Introduction

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.



Algorithm:

BUBBLE_SORT (ARR,N)
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]
STEP 4: EXIT





Program Of BUBBLE SORT:
   #include<stdio.h>
   #include<conio.h>
   main()
   {
   int arr[100],n,i,j,temp;
   printf("\n Enter number of elements \n");
   scanf("%d",&n);
   printf("\n Enter %d integers \n", n);
   for(i=0;i<n;i++)
   scanf("%d", &arr[i]);
   for(i=0;i<(n-1);i++)
   {
   for(j=0;j<n-i-1;j++)
   {
   if(arr[j]>arr[j+1]) 
   {
   temp=arr[j];
   arr[j]=arr[j+1];
   arr[j+1]=temp;
   }}}
   printf("\n Sorted list in ascending order: \n");
   for(i=0;i<n;i++)
   printf("%d \n",arr[i]);
   getch();
   }

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

      
       2nd case:
       


Discussion:

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.



No comments:

Post a Comment