In: Computer Science
using c++
10. Sorting Orders
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 it 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 it using
an ascending order selection
sort, modified to print out the array contents after each pass of
the sort.
test case:
Bubble Sort The unsorted values are: 7 2 3 8 4 5 6 1 sort pass #1 : 2 7 3 8 4 5 6 1 sort pass #2 : 2 3 7 8 4 5 6 1 sort pass #3 : 2 3 7 4 8 5 6 1 sort pass #4 : 2 3 7 4 5 8 6 1 sort pass #5 : 2 3 7 4 5 6 8 1 sort pass #6 : 2 3 7 4 5 6 1 8 sort pass #7 : 2 3 4 7 5 6 1 8 sort pass #8 : 2 3 4 5 7 6 1 8 sort pass #9 : 2 3 4 5 6 7 1 8 sort pass #10 : 2 3 4 5 6 1 7 8 sort pass #11 : 2 3 4 5 1 6 7 8 sort pass #12 : 2 3 4 1 5 6 7 8 sort pass #13 : 2 3 1 4 5 6 7 8 sort pass #14 : 2 1 3 4 5 6 7 8 sort pass #15 : 1 2 3 4 5 6 7 8 The sorted values are: 1 2 3 4 5 6 7 8 Selection Sort The unsorted values are: 7 2 3 8 4 5 6 1 sort pass #1 : 1 2 3 8 4 5 6 7 sort pass #2 : 1 2 3 8 4 5 6 7 sort pass #3 : 1 2 3 8 4 5 6 7 sort pass #4 : 1 2 3 4 8 5 6 7 sort pass #5 : 1 2 3 4 5 8 6 7 sort pass #6 : 1 2 3 4 5 6 8 7 sort pass #7 : 1 2 3 4 5 6 7 8 The sorted values are: 1 2 3 4 5 6 7 8
#include <iostream>
using namespace std;
//function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j,k,temp=0,p=0;
//Displays the unsorted array values
cout<<"The unsorted values are: \n";
for (k = 0; k < n; k++)
cout << arr[k] << " ";
cout<<"\n";
//logic for bubble sort
//In this sorting technique, adjacent elements are compared,
//if first element is greater than
//second element those 2 elements will swapped.
//This process is continues untill we get thecorrect sequence
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
p++;
//swapping the adjacent values if arr[j] > arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
//Displays the content of array in each pass
cout<<"sort pass #"<<p<<": ";
for (k = 0; k < n; k++)
cout <<arr[k] << " ";
cout << "\n";
}
}
}
//Dispalys the sorted array values
cout<<"The sorted values are: \n";
for (k = 0; k < n; k++)
cout << arr[k] << " ";
}
//function for selection sort
void selectionSort(int arr[], int n)
{
int i, j, min,k,temp=0;
//Displays the unsorted array values
cout<<"The unsorted values are: \n";
for (k = 0; k < n; k++)
cout << arr[k] << " ";
cout<<"\n";
//logic for selection sort
//In selection sort, sorts an array by repeatedly finding the
minimum element
//from unsorted part and putting it at the beginning.
for (i = 0; i < n-1; i++)
{
min = i;
// Find the minimum element in unsorted array
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[min])
min = j;
}
//swapping the first element with the minimum
value
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
//Displays the content of array in each pass
cout<<"sort pass #"<<i+1<<": ";
for (k = 0; k < n; k++)
cout << arr[k] << " ";
cout<<"\n";
}
//Dispalys the sorted array values
cout<<"The sorted values are: \n";
for (k = 0; k < n; k++)
cout << arr[k] << " ";
}
//main function starts from here
int main()
{
int arr1[8] = {7,2,3,8,4,5,6,1};
int arr2[8] = {7,2,3,8,4,5,6,1};
int size =8;
cout<<"bubble sort:\n";
//calling the function bubble sort
bubbleSort(arr1, size);
cout<<"\n\nselection sort:\n";
//calling the function selection sort
selectionSort(arr2,size);
return 0;
}