Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

10. Implement Bubble Sort Algorithm

The document outlines the implementation of the Bubble Sort algorithm using C programming. It explains the algorithm's process of comparing and swapping adjacent elements to sort an array, noting its inefficiency for large datasets due to a time complexity of O(n^2). The document includes a sample code and concludes with the successful implementation of the algorithm.

Uploaded by

tuba.khan7575
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

10. Implement Bubble Sort Algorithm

The document outlines the implementation of the Bubble Sort algorithm using C programming. It explains the algorithm's process of comparing and swapping adjacent elements to sort an array, noting its inefficiency for large datasets due to a time complexity of O(n^2). The document includes a sample code and concludes with the successful implementation of the algorithm.

Uploaded by

tuba.khan7575
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO.

10
Aim: Implement Bubble Sort Algorithm.

LO:LO4: Students will be able to select appropriate searching techniques for given problems

Software: ‘C’ Programming

Theory: Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are swapped if they
are not in order. This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2) where n is the number of items.

We take an unsorted array for our example. Bubble sort takes Ο(n 2) time so we're keeping it short and
precise.

Bubble sort starts with very first two elements, comparing them to check which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with
27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array. After one iteration, the
array should look like this −

To be precise, we are now showing how an array should look like after each iteration. After the
second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is completely sorted.

Program:

/* Bubble sort code */


#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}

Output:

Conclusion: We have implemented Bubble Sort Algorithm using ‘C’ program.

You might also like