20CA2013 Data Structures Lab
Exercise number : 5
Date: 19th August, 2021
Bubble Sorting an Array
• Problem : To sort an array in an efficient way.
• Aim : To sort an array using the efficient Bubble Sort algorithm.
• Algorithm :
Step 1 : Start the program
Step 2 : Include libraries <iostream> and <conio.h>
Step 3 : Define a function read - Parameters : int array[10], int n;
Return type : void
Step 4 : Define a function print - Parameters : int array[10], int n;
Return type : void
Step 5 : Define a function bubble_sort - Parameters : int array[10],
int n; Return type : void
Step 6 : Inside function main traverse step 7 to step 14
Step 7 : Declare int array[10], n, t, position
Step 8 : Input n as number of array elements
Step 9 : Call function read - Parameters : array, n
Step 10 : Call function print - Parameters : array, n
Step 11 : Call function bubble_sort - Parameters : array, n
Step 12 : Call function getch()
Step 13 : Return 0
Step 14 : End the program
Register number : URK20DA1009
Name : Judah Felix
20CA2013 Data Structures Lab
‣ Function read(int array[10], int n) :
Step 1 : Declare int i
Step 2 : For i = 0; i < n; i++ repeat step 3
Step 3 : Input array[i] as the elements to be entered into the array
Step 4 : End the function
‣ Function print(int array[10], int n) :
Step 1 : Declare int i
Step 2 : For i = 0; i < n; i++ repeat step 3
Step 3 : Display array[i]
Step 4 : End the function
‣ Function bubble_sort(int array[10], int n)
Step 1 : Declare int i, j, temp
Step 2 : For i = 0; i < n - 1; i++ repeat step 3 to step 7
Step 3 : For j = 0; j < n - i; i++ repeat step 4 to step 7
Step 4 : If array[j] > array[j+1] execute step 5 to step 7
Step 5 : Set temp = array[j]
Step 6 : Set array[j] = array[j+1]
Step 7 : Set array[j+1] = temp
Step 8 : For i = 0; i < n; i++ repeat step 9
Step 9 : Display array[i]
Step 10 : End the function
Register number : URK20DA1009
Name : Judah Felix
20CA2013 Data Structures Lab
• Source code :
#include <iostream>
#include <conio.h>
using namespace std;
void read(int array[10], int n)
{
int i;
cout << "\nEnter the " << n <<" elements one by one : ";
for(i = 0; i < n; i++)
cin >> array[i];
}
void print(int array[10], int n)
{
int i;
cout << "\nThe array elements you entered are : ";
for(i = 0; i < n; i++)
cout << array[i] << "\t";
}
void bubble_sort(int array[10], int n)
{
int i, j, temp;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (i = 0; i < n; i++)
cout << array[i] << '\t';
}
int main()
{
int array[10], n, t, position;
cout << "\nEnter the number of elements : ";
cin >> n;
read(array, n);
print(array, n);
cout << "\n\nThe elements you entered in sorted order : ";
Register number : URK20DA1009
Name : Judah Felix
20CA2013 Data Structures Lab
bubble_sort(array, n);
getch();
return 0;
}
• Output 1 :
Enter the number of elements : 5
Enter the 5 elements one by one : 50 80 25 34 70
The array elements you entered are : 50 80 25
34 70
The elements you entered in sorted order : 25 34
50 70 80
• Output 2 :
Enter the number of elements : 10
Enter the 10 elements one by one : 34 56 32 12 60 45 34 90
100 550
The array elements you entered are : 34 56 32 12
60 45 34 90 100 550
The elements you entered in sorted order : 12 32 34
34 45 56 60 90 100 550
Register number : URK20DA1009
Name : Judah Felix
20CA2013 Data Structures Lab
• Result :
The above program was executed and the output was verified
for a sample set of input values.
Register number : URK20DA1009
Name : Judah Felix