In this C++ program, we are going to learn about the bubble sort algorithm, which is a simple sorting algorithm.
Bubble sort is one of the simplest sorting algorithms which is also called the sinking sort algorithm. Bubble sort works by repeatedly comparing and swapping the elements adjacent to each other in different passes until the array gets sorted. with Bubble sort, we can only sort small number of data elements.
Consider an array arr[] = {5, 1, 4, 2, 8}
First Pass:
Second Pass:
Third Pass:
The user is asked to enter the size of the array and its elements. The size entered by the user is read into the variable n. The elements to sort are stored in an array called arr[50].To get the elements into the array a for loop
can be used.
Initially, the value of i is set to 0 and checked for the condition i if true then get the first element into arr[i] and increment the value of i.
now all the n elements are get stored in the arr[]. for sorting using for loop
,
initially i=0 and checks the condition, whether it is less than n-1 or not. If the condition evaluates to be true, program flows go inside the loop. j=0 (initially) and the condition j<(n-i-1) evaluates to be true, the program flow goes inside the loop and checks the condition of if block, that is arr[j]>arr[j+1] evaluates to be true, the program flow goes inside the if block. the element at index number j gets swapped with the element at index number j+1. Now program flow goes to the updation part of the inner for loop
and increments the value of j. Again checks the condition, that is j<(n-i-1) evaluates to be true, therefore program flow again goes inside the loop and checks the condition of the if block. If the condition evaluates to be true, then process the three statements, otherwise update the value of j, and check the condition Continue the process similar with the updated value, until the condition of the outer for loop
evaluates to be false. If the condition of the outer for loop evaluates to be false, then program flow exits the loop Now the array arr[] gets sorted, just print it.
Step 1: Call the header file iostream.
Step 2: Use the namespace std
.
Step 3: Open the integer type main function; int main().
Step 4: Declare integer type variables n, i, j. arr[50], temp;
Step 5: Ask the user to enter the size of the array;
Step 6: Get the number into the variable n;
Step 7: Ask the user to enter the numbers;
Step 8: Get the numbers into the array arr[50];
Step 9: Get the first two elements of the array and check the condition arr[0] > arr[1];
Step 10: swap the elements.
Step 11: Get the elements arr[1] and arr[2]; check the condition arr[1]>arr[2];
Step 12: Repeat step 10;
Step 13: Likewise get each element and compare;
Step 14: print the sorted array;
Step 15: Exit;
#include<iostream>
using namespace std;
int main()
{
int n, i, arr[50], j, temp;
cout<<"Enter the Size (max. 50): ";
cin>>n;
cout<<"Enter "<<n<<" Numbers: ";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\nSorting the Array using Bubble Sort Technique..\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"\nArray Sorted Successfully!\n";
cout<<"\nThe New Array is: \n";
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Enter the Size (max. 50): 6 Enter 6 Numbers: 6 2 8 3 9 1 Sorting the Array using Bubble Sort Technique.. Array Sorted Successfully! The New Array is: 1 2 3 6 8 9