Question

In: Computer Science

using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....

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 

Solutions

Expert Solution

#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;  
}


Related Solutions

In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending 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 descending order selection sort, modified to print out the...
Write a program that uses two identical arrays of at least 25 integers. It should call...
Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Please include comments to understand code. Requirements Implement the following functions: int readData( int **arr) arr is a pointer to pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers....
C++ Write a program for sorting a list of integers in ascending order using the bubble...
C++ Write a program for sorting a list of integers in ascending order using the bubble sort algorithm Requirements Implement the following functions: Implement a function called readData int readData( int **arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers. After the first number,...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation...
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation , all numbers must be between 0 and 100) 2) find the largest number 3) Find the Smallest Number 4) Find the Sum of all numbers in the Array Display: as per the user's section (menu using switch or if else ) Largest Number Smallest Number Sum of all numbers the original Array DO NOT USE METHODS OR FUNCTIONS Submit: Source code (C++) output...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array...
Multidimensional Arrays Design a C program which uses two two-dimensional arrays as follows: - an array which can store up to 50 student names where a name is up to 25 characters long - an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
This is for c++ Write a program that works with two arrays of the same size...
This is for c++ Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT