Professional Documents
Culture Documents
Exercise 5 - Bubble Sorting An Array
Exercise 5 - Bubble Sorting An Array
Exercise number : 5
Date: 19th August, 2021
• 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
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
• Output 2 :
Enter the number of elements : 10