Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort the array using an ascending order bubble sort modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort the array using an ascending order selection sort modified to print out the array contents after each pass of the sort.

Upload your c++ file

 

#include <iostream>

using namespace std;

 

int main() {

 

//array initialization

int array1[]      = {8, 2, 7, 3, 1, 5, 4, 6};

int array2[]      = {8, 2, 7, 3, 1, 5, 4, 6};

int swap, minIndex, tmp;

 

//print first array

cout << “First array (unordered): “;

for(int i = 0; i < 8; i++){

cout << array1[i] << ” “;

}

 

//bubble sort

for (int c = 0 ; c < 8; c++){

for (int d = 0 ; d < 8 – c – 1; d++){

if (array1[d] > array1[d+1]){

swap       = array1[d];

array1[d]   = array1[d+1];

array1[d+1] = swap;

}

}

}

cout << endl;

 

//print ordered first array

cout << “First array (ordered): “;

for(int i = 0; i < 8; i++){

cout << array1[i]  << ” “;

}

cout << endl;

 

//print second array

cout << “Second array (unordered): “;

for(int i = 0; i < 8; i++){

cout << array2[i]  << ” “;

}

cout << endl;

 

//selection sort

for (int i = 0; i < 8; i++) {

minIndex = i;

for (int j = i + 1; j < 8; j++)

if (array2[j] < array2[minIndex])

minIndex = j;

if (minIndex != i) {

tmp = array2[i];

array2[i] = array2[minIndex];

array2[minIndex] = tmp;

}

}

 

//print ordered second array

cout << “Second array (ordered): “;

for(int i = 0; i < 8; i++){

cout << array2[i]  << ” “;

}

cout << endl;

 

return 0;

}