Question: flowchart for the following program: #include #include using namespace std ; int bubble_sort ( int a [], int n ) { int i , j

flowchart for the following program:

#include

#include

using namespace std;

int bubble_sort(int a[], int n)

{

int i, j, x;

int exchanges = 0;

for( i = 0 ; i < n - 1 ; i++)

{

for( j = 0 ; j < n - i - 1 ; ++j)

{

if (a[ j + 1 ] < a[ j ])

{

// swap the j and j + 1 th element of a

x = a[j];

a[ j ] = a[ j + 1 ];

a[ j + 1 ] = x;

exchanges++;

}

}

}

return exchanges;

}

int selection_sort(int a[], int n)

{

int i, j, min, x;

int exchanges = 0;

// traverse the array

for (i = 0; i < n - 1; i++)

{

// store the index of the smallest element

min = i;

// traverse the array from the element after i till the end

for (j = i + 1; j < n; j++)

{

// if the current element is smaller than the min element

if (a[j] < a[min])

min = j;

}

// Swap first element and min

x = a[min];

a[min] = a[i];

a[i] = x;

exchanges++;

}

return exchanges;

}

int main()

{

int arr1[20], arr2[20];

int i;

for( i = 0 ; i < 20 ; i++ )

{

// generate random number in range 0 - 100

arr1[i] = rand() % 101;

arr2[i] = arr1[i];

}

cout<<"First array : ";

int exchanges1 = bubble_sort(arr1, 20);

int exchanges2 = selection_sort(arr2, 20);

for( i = 0 ; i < 20 ; i++ )

cout<<arr1[i]<<" ";

cout<<" Exchanges in bubble sort : "<<exchanges1;

cout<<" Second array : ";

for( i = 0 ; i < 20 ; i++ )

cout<<arr2[i]<<" ";

cout<<" Exchanges in selection sort : "<<exchanges2;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!