Data Structure: Unit V (a): Searching and Sorting

Bubble Sort

Operations, Algorithm with Example C Programs | Data Structure

This is the simplest kind of sorting method in this method. We do this bubble sort procedure in several iterations, which are called passes.

Bubble Sort

This is the simplest kind of sorting method in this method. We do this bubble sort procedure in several iterations, which are called passes.

Ex. 6.3.1: Sort the following elements using bubble sort.

45  -40  190  99  11

Sol. First store those elements in the array a

Algorithm:

1. Read the total number of elements say n

2. Store the elements in the array

3. Set the i=0.

4. Compare the adjacent elements.

5. Repeat step 4 for all n elements.

6. Increment the value of i by 1 and repeat step 4, 5 for i< n

7. Printf the sorted list of elements.

8. Stop.

Algorithm Bubble (A[0...n-1])

//Problem Description: This algorithm is for sorting the

//elements using bubble sort method

//Input:An array of elements A[0...n-1] that is to be sorted

//Outp ut:. The sorted array A[0...n-1]

for i0 to n-2 do

{

for j0 to n-2-i do

{

if(A[j]>A[j+1])then

{

temp A[j]

A[j] A[j+1].

A[j+1]temp

}

}//end of inner for loop

}//end of outer for loop

Ex. 6.3.2: 'C' Program for sorting elements using bubble sort. Sol. :

/***********************************************************

Program for sorting the elements by bubble sort method

***********************************************************/

/* Header Files*/

#include<stdio.h>

#include<conio.h>

#include <process.h>

/*Declaration*/

void bubblesort(int a[20], int n);

void bubblesort(int a[20], int n)

{

int i,j,m,temp;

for(i=0;i<n-1;i++).

{

for(j=0;j<n;j++)

{ if(a[j]>a[j+1])

{ temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

//Printing the sorted array//

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

printf("\n\t%d",a[i]);

}

void main()

{

int a[20],i,n;

int ch;

clrscr();

printf("\nEnter the number of elements in the sorting array: ");

scanf("%d", &n);

//Accepting n number of elements in the array

printf("\nEnter the elements for the array\n");

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

scanf("%d", &a[i]);

bubblesort (a,n);

getch();

}

/******************** End Of Program ****************/

Output

Enter the number of elements in the sorting array: 5

Enter the elements for the array

30

20

50

40

10

  10

  20

  30

  40

  50

Ex. 6.3.3: Sort the following list of numbers using bubble sort technique 52, 1, 27, 85, 66, 23, 13, 57.

Sol. 

Pass 1:

 

Pass 2:



Pass 5:

Pass 6:


No interchange takes place. Hence the sorted list is

Data Structure: Unit V (a): Searching and Sorting : Tag: : Operations, Algorithm with Example C Programs | Data Structure - Bubble Sort


Data Structure: Unit V (a): Searching and Sorting



Under Subject


Data Structure

CS3301 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation



Related Subjects


Discrete Mathematics

MA3354 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Digital Principles and Computer Organization

CS3351 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Foundation of Data Science

CS3352 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Data Structure

CS3301 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Object Oriented Programming

CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation