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 remove woocommerce checkout fields?

Ans :    In WooCommerce there is no built-in function to remove checkout fields and billing details heading from checkout page. So we remove these fields pragmatically using hooks supplied by Woocommerce. What you need to do is open your child theme's functions.php file and write the following code. functions.php // Removing checkout fields add_filter( 'woocommerce_checkout_fields' , 'wc_remove_checkout_fields' ); function wc_remove_checkout_fields( $fields ) {     unset($fields['billing']['billing_first_name']);     unset($fields['billing']['billing_last_name']);     unset($fields['billing']['billing_company']);     unset($fields['billing']['billing_address_1']);     unset($fields['billing']['billing_address_2']);     unset($fields['billing']['billing_city']);     unset($fields['billing']['billing_postcode']);     unset($fields['bil