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

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...
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...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However,...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
A local instructor wants you to write a c++ program using arrays to calculate the average...
A local instructor wants you to write a c++ program using arrays to calculate the average score made on exams by her students. For simplicity, she always has only 12 students in each course she teaches. She teaches multiple subjects so she would like to enter the name of the exam. She wants the program to also determine the highest and lowest scores and the number of students who passed and failed the exam. A score of 60 or above...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT