The Artima Developer Community
Sponsored Link

Java Buzz Forum
Bubble sort algorithm in c with explanation

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Bubble sort algorithm in c with explanation Posted: May 20, 2017 1:22 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Bubble sort algorithm in c with explanation
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Bubble sort is one of the example of sorting algorithm.
  • Bubble sort in data structure
  • Bubble sort is a procedure of sorting elements in ascending or descending order.
  • If we want to sort an array of numbers [4,2,3,1]  using bubble sort it will return [1,2,3,4] after sorting in ascending order.


 Bubble sort in c with explanation

  • Bubble sort algorithm of ascending order will move higher valued elements to right and lower value elements to left.
  • For this we need to compare each adjacent elements in an array and check they are in order or not if not swap those two elements.
  • After completion of first iteration highest value in the array will be moved to last or final position.
  • In the worst case all the highest elements in array like [9 8 7 5 4] completely in reverse order so we need to iterate all of the elements in N times.
  • Repeat this process N- 1 time in worst case to sort set of elements.
  • Worse case complexity : O(n2
  • In the best case scenario all the elements are already in sorted order so one iteration with no swaps required.
  • Best case complexity: O(n)
  • Let us see the graphical representation of explanation of bubble sort algorithm in c programming language.

Bubble sort algorithm with example diagram

bubble sort algorithm in c

Bubble sort algorithm pseudocode:

Program #1 : Write a C program to sort list of elements using bubble sort algorithm.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //Bubble sort algorithm in c
  4. //www.instanceofjava.com
  5. int main(int argc, char *argv[])
  6. {
  7.    int array[100], n,i,j,k, swap;
  8.  
  9.   printf("Enter number of elements to sort\n");
  10.   scanf("%d", &n);
  11.  
  12.   printf("Enter %d elements \n", n);
  13.   for (i = 0; i< n; i++)
  14.     scanf("%d", &array[i]);
  15.  
  16.   printf("\nBefore sorting \n ");
  17.    for (k = 0; k < n; k++) {
  18.       printf("%5d", array[k]);
  19.    }
  20.  
  21.    for (i = 1; i < n; i++) {
  22.       for (j = 0; j < n - 1; j++) {
  23.          if (array[j] > array[j + 1]) {
  24.             swap = array[j];
  25.             array[j] = array[j + 1];
  26.             array[j + 1] = swap;
  27.          }
  28.       }
  29.       
  30.       }
  31.  
  32.  printf("\nAfter Bubble sort elements are:\n");
  33.    for (k = 0; k < n; k++) {
  34.       printf("%5d", array[k]);
  35.    }
  36.  
  37.   return 0;
  38. }

 Output:


bubble sort algorithm in c program recursive

  • Now we will see program on recursive bubble sort algorithm in c
  • Bubble sort using recursive function in c

Program #2: Write a c program for bubble sort using recursion


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //Bubble sort algorithm in c
  4. //www.instanceofjava.com
  5. void bubblesort(int array[], int n);
  6. int main(int argc, char *argv[])
  7. {
  8.    int array[10], n,i,k;
  9.  
  10.   printf("Enter number of elements to sort\n");
  11.   scanf("%d", &n);
  12.  
  13.   printf("Enter %d elements \n", n);
  14.   for (i = 0; i< n; i++)
  15.     scanf("%d", &array[i]);
  16.  
  17.   printf("\nBefore sorting \n ");
  18.    for (k = 0; k < n; k++) {
  19.       printf("%5d", array[k]);
  20.    }
  21.  bubblesort(array,n);
  22.    
  23.   getch();
  24. }
  25.  
  26. void bubblesort(int array[], int n){
  27.       int  i,j,k, swap;
  28.   for (i = 1; i < n; i++) {
  29.       for (j = 0; j < n - 1; j++) {
  30.          if (array[j] > array[j + 1]) {
  31.             swap = array[j];
  32.             array[j] = array[j + 1];
  33.             array[j + 1] = swap;
  34.          }
  35.       }
  36.       
  37.       }
  38.  
  39.  printf("\nAfter Bubble sort elements are:\n");
  40.    for (k = 0; k < n; k++) {
  41.       printf("%5d", array[k]);
  42.    }    
  43. }

Output:

  1. Enter number of elements to sort
  2. 6
  3. Enter 6 elements
  4. 9 8 2 3 5 1
  5.  
  6. Before sorting
  7.      9    8    2    3    5    1
  8. After Bubble sort elements are:
  9.     1    2    3    5    8    9

Read: Bubble sort algorithm in c with explanation

Topic: C program to find ASCII value of a string or character Previous Topic   Next Topic Topic: C program to print given number in words

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use