Skip to main content

Bubble Sort Algorithm in C

BUBBLE SORT :

Bubble sort is one of the simple sorting algorithm that sometimes referred to as sinking sort.
It compares each pair of adjacent element and swaps them if they are in wrong order.
Let us take an unsorted list of elements and sort them by applying this algorithm.
Elements of the array -
                               
                                9        3        6       7       1        5


Pass 1: 
                  9      3      6       7       1        5       arr[0] > arr[1], Exchange
                  3      9      6       7       1        5       arr[1] > arr[2], Exchange
                  3      6      9       7       1        5       arr[2] > arr[3], Exchange
                  3      6      7       9       1        5       arr[3]> arr[4], Exchange
                  3      6      7       1       9        5       arr[4]> arr[5], Exchange
   
                  3      6       7       1       5       9

Pass 2:
                 3       6       7       1        5       9      arr[0] < arr[1]
                 3       6       7       1        5       9      arr[1] < arr[2]
                 3       6             1        5       9      arr[2] > arr[3], Exchange
                 3       6       1       7        5       9      arr[3] > arr[4], Exchange


                 3       6       1       5        7       9

Pass 3:
                3        6       1       5       7        9       arr[0] < arr[1]
                3             1       5       7        9       arr[1] > arr[2], Exchange
                3        1       6       5       7        9       arr[2] > arr[3], Exchange

                3        1       5       6       7        9

Pass 4:    
                3        1       5       6       7        9        arr[0] > arr[1], Exchange
                1             5       6       7        9        arr[1] < arr[2]

                1        3      5        6       7        9

Pass 5:    
                1        3      5        6       7        9        arr[0] < arr[1]

       
                1       3       5        6        7       9


Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],i,j,n,temp;
clrscr();
printf("Enter how many number you want to display: ");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\nElements before sorted are: \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nElements after sorted are: \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
getch();

}


Output :




Comments

Popular posts from this blog

Algorithm, Flowchart and Program code to print the larger of two numbers

       Q. Write a program in C to print the larger of two numbers with                t he  help of   algorithm and flowchart.        Algorithm:        Step 1: Start       Step 2: Read a, b .        /* a, b two numbers */        Step 3: If a>b then        /*Checking */                       Display “a is the largest number”.                   Otherwise                       Display “b is the largest number”.   Step 4: Stop.     Flowchart  :     Program:       Output:  

How to customize header part of storefront theme in wordpress?

Solution:  You can easily customize storefront theme from functions.php of storefront child theme. With the help of storefront_header hook you can do that. The following code is used to remove storefront header default features- add_action("after_setup_theme", "storefront_child_header_remove"); function storefront_child_header_remove() {     remove_action("storefront_header","storefront_site_branding",20);     remove_action("storefront_header","storefront_secondary_navigation",30);     remove_action("storefront_header","storefront_product_search",40);     remove_action("storefront_header","storefront_primary_navigation_wrapper",42);     remove_action("storefront_header","storefront_primary_navigation",50);     remove_action("storefront_header","storefront_header_cart",60);     remove_action("storefront_header","storefront_pri...

How to add TextBox to display product price inside woocommerce single product page?

Adding TextBox to display product price inside woocommerce single product page -  Step -1 :  Write the following code inside your functions.php file -  add_action( 'woocommerce_after_add_to_cart_quantity', 'woocommerce_total_product_price', 31 ); function woocommerce_total_product_price() {       ?>      <input type="hidden" class="product-currency" value=<?php echo get_woocommerce_currency_symbol()?> />     <input type="text" id="product_total_price" readonly="readonly">     <input type="hidden" id="calculate_product_total_price" >     <?php } Step - 2 :  Create a js file as "test.js" and write the following code inside "test.js" file - jQuery(function($) {     /* Clearing product price textbox and hidden price textbox when clear link is clicked */   $('.reset_variations').on('click...