In this article bubble sort in c we give the information bout the basic concept in the bubble sort is to pass through the array sequentially number of times. In each pass, each element is compared with its successor.

Bubble Sort in C

Sorting:

Introduction:

There are many applications where the data is stored in sorted order. Employee data, Telephone directory, Merit list etc. are the examples where we can store data in sorted order.

Definition:

Sorting is an operation of arranging data in some given order such as ascending or descending.

In other words sorting means to arrange a set of items in sequence.

Examples:

Suppose the data contains 6 elements as follows

Data:    52,  12,  26,  67,  20,  10

After sorting data in ascending order

Data:    10,  12,  20,  26,  52,  67

After sorting data in descending order

Data:    67,  52,  26,  20,  12,  10

The sorting methods can be divided into two parts i.e. internal and external.

  1. Internal Method: In internal sorting, data that is going to be sorted will be in main memory.
  2. External Method: In external sorting data will be on auxiliary storage devices like tape, floppy, hard disk etc.

Sorting Methods:

There are some methods to sort given data in given order. These are

  • Bubble Sort (Exchange Sort)
  • Insertion Sort
  • Selection Sort
  • Quick Sort

Bubble Sort (Exchange Sort):

The basic concept in the bubble sort is to pass through the array sequentially number of times. In each pass, each element is compared with its successor. If the first elements is greater than the second element i.e. if they are not in proper order then the position of the elements are interchanged. Then next element is compared with its successor and same process is repeated for all the elements in an array.

After the first pass, the largest element is in its proper position i.e. last position. During the second pass, the second largest element occupies the second last position. Since each iteration places a new element into its proper position, an array on n elements requires no more than n-1 iterations. The method is called the bubble sort because each number slowly “bubbles” up to its proper position.

Bubble sort algorithm in c:

Step 1:            Start

2:             Read n elements of array a[ ]

3:             Let p=0

4:             Let q=0

5:             if (a[q] > a[q+1] )

Swap a[ q ] and a[ q+1 ]

6:             q= q+1

7:             if q<n-p-1 go to step 5

8:             p=p+1

9:             if p< n-1 go to step 4

10:             Print sorted array

11:            Stop.

 Example: Consider array elements as 34, 6, 14, 8, 2

Iteration 1:

34 6 14 8 2

                          Swap

6 34 14 8 2

                                                       Swap               

6 14 34 8 2

                                                                                 Swap

6 14 8 34 2

                                                                                                                 Swap

6 14 8 2 34

 Iteration 2:

6 14 8 2 34

                   No Swap

 

6 14 8 2 34

                                                    Swap

6 8 14 2 34

                                                                                     Swap

6 8 2 14 34

 Iteration 3:

6 8 2 14 34

                   No Swap

 

6 8 2 14 34

                                                        Swap

6 2 8 14 34

Iteration 4:

6 2 8 14 34

                           Swap

 

2 6 8 14 34

The complete set of iteration is as follows:

Iteration 1:     6          14        8          2          34

2:     6          8          2          14        34

3:     6          2          8          14        34

4:     2          6          8          14        34

//Bubble sort program:

#include<stdio.h>

#include<conio.h>

#define MAX 6

void  main()

{

int array[MAX]={56, 23, 12, 20, 5, 15};

int i,  j, k, temp;

clrscr();

printf(“\n Unsorted List is : ”);

for(i=0;i<MAX;i++)

printf(“%d\t”,array[i]);

for(i=0;i<MAX;i++)

{

for(j=0;j<MAX-i-1;j++)

{

if(array[j]>array[j+1])

{

temp=array[j];

array[j]=array[j+1];

array[j+1]=temp;

}

}

}

printf(“\n Sorted List is : ”);

for(i=0;i<MAX;i++)

printf(“%d\t”,array[i]);

getch();

}

Output:

Unsorted List is : 56     23     12     20     5     15

Sorted List is : 5     12     15        20      23      56

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *