Sorting is nothing but
arranging the data in ascending or descending order. The term sorting came
into picture, as humans realised the importance of searching quickly.
There are so many things
in our real life that we need to search for, like a particular record in database, roll numbers in merit list, a particular telephone number in
telephone directory, a particular page in a book etc. All this would have been
a mess if the data was kept unordered and unsorted, but fortunately the concept
of sorting came into existence, making it easier for everyone
to arrange data in an order, hence making it easier to search.
Sorting arranges
data in a sequence which makes searching easier.
Bubble Sort is
a simple algorithm which is used to sort a given set of
n
elements
provided in form of an array with n
number
of elements.Bubble Sort compares all
the element one by one and sort them based on their values. If the given array has to
be sorted in ascending order, then bubble sort will start by comparing the
first element of the array with the second element, if the first element is
greater than the second element, it will swap both the
elements, and then move on to compare the second and the third element, and so
on.
If we have total
n
elements,
then we need to repeat this process for n-1
times. Step by Step Process
The bubble sort algorithm is performed using the following steps...
1. Starting with the first element(index = 0), compare the current element with the next element of the array.
2. If the current element is greater than the next element of the array, swap them.
3. If the current element is less than the next element, move to the next element. Repeat Step 1.
Bubble Sort Logic
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(j = 0; j < n-i-1; j++)
{
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
Let's consider an array
with values
{5, 1, 6, 2, 4, 3}
Implementation of Bubble Sort Algorithm using C Programming Language
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// print the sorted array
printf("Sorted Array: ");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[100], i, n, step, temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter element no. %d: ", i+1);
scanf("%d", &arr[i]);
}
// call the function bubbleSort
bubbleSort(arr, n);
return 0;
}
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// print the sorted array
printf("Sorted Array: ");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[100], i, n, step, temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter element no. %d: ", i+1);
scanf("%d", &arr[i]);
}
// call the function bubbleSort
bubbleSort(arr, n);
return 0;
}
Output
No comments:
Post a Comment