Question

C++ This program will compare sorting algorithms by examining how the number of swaps/exchanges differs between...

C++

This program will compare sorting algorithms by examining how the number of swaps/exchanges differs between the two algorithms, and demonstrate the exchanges visually.

This program will repeat until the user elects to quit.

Ask the user how large of an array they'd like to create and fill with random numbers. The range of array elements they can select is between 5 and 20 inclusive. Use input validation on this 5-20 range. The range of random numbers is 0-99 inclusive.

The array that you create must be declared of the size that the user enters, not some arbitrary large number.

Pass your array to a function that will populate it with the random numbers.

Your program must call a function that you create for each of these sort functions, and you pass the array to it. Use the code we've already seen in class for these sort functions. Modify your sort functions such that these features/functionalities are added:

  • Add code to keep a count of how many exchanges/swaps take place to accomplish the sort. Remember, for bubble sort, there are many swaps that occur. For select sort, there are fewer. Your code should reflect these properties of the sorts. Display the results at the end.
  • Add code to print out the array contents after each pass of the sort. This creates the sorting visualization.

Your results should very closely resemble:

This program will create an array of random numbers of a size that
you specify. It will sort the array using both the Bubble and
Select sort algorithms. Next it will visualize the sorting process
and display the number of sorting exchanges that took place in each.

How large of an array would you like? Choose between 5-20: 10

Bubble Sort of this array: 
96 87 45 49 83 69 89 21 17 52

Sorting visualization:
87 96 45 49 83 69 89 21 17 52
87 45 96 49 83 69 89 21 17 52
87 45 49 96 83 69 89 21 17 52
87 45 49 83 96 69 89 21 17 52
87 45 49 83 69 96 89 21 17 52
87 45 49 83 69 89 96 21 17 52
87 45 49 83 69 89 21 96 17 52
87 45 49 83 69 89 21 17 96 52
87 45 49 83 69 89 21 17 52 96
45 87 49 83 69 89 21 17 52 96
45 49 87 83 69 89 21 17 52 96
45 49 83 87 69 89 21 17 52 96
45 49 83 69 87 89 21 17 52 96
45 49 83 69 87 21 89 17 52 96
45 49 83 69 87 21 17 89 52 96
45 49 83 69 87 21 17 52 89 96
45 49 69 83 87 21 17 52 89 96
45 49 69 83 21 87 17 52 89 96
45 49 69 83 21 17 87 52 89 96
45 49 69 83 21 17 52 87 89 96
45 49 69 21 83 17 52 87 89 96
45 49 69 21 17 83 52 87 89 96
45 49 69 21 17 52 83 87 89 96
45 49 21 69 17 52 83 87 89 96
45 49 21 17 69 52 83 87 89 96
45 49 21 17 52 69 83 87 89 96
45 21 49 17 52 69 83 87 89 96
45 21 17 49 52 69 83 87 89 96
21 45 17 49 52 69 83 87 89 96
21 17 45 49 52 69 83 87 89 96
17 21 45 49 52 69 83 87 89 96

Select Sort of this array: 
96 87 45 49 83 69 89 21 17 52

Sorting visualization:
17 87 45 49 83 69 89 21 96 52
17 21 45 49 83 69 89 87 96 52
17 21 45 49 83 69 89 87 96 52
17 21 45 49 83 69 89 87 96 52
17 21 45 49 52 69 89 87 96 83
17 21 45 49 52 69 89 87 96 83
17 21 45 49 52 69 83 87 96 89
17 21 45 49 52 69 83 87 96 89
17 21 45 49 52 69 83 87 89 96

Number of exchanges:
Bubble: 31
Select: 9

Another visualization (Y/N)? > n
Program ending. Bye!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include<iostream>
#include<time.h>
using namespace std;
void populateArray(int a[],int len)
{
   srand (time(NULL));
   for(int i=0;i<len;i++)
   {
       a[i]=rand() % 100;
   }
}
void printArray(int a[],int len)
{
   for(int i=0;i<len;i++)
   {
       cout<<a[i]<<" ";
   }
   cout<<endl;
}
int bubleSort(int array[],int len)
{
   int i, j, temp;
int swaps = 0;
for(i = 0; i < len-1; ++i)
   {
for(j=0; j<len-1-i; ++j)
       {
if(array[j] > array[j+1])
           {
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
               printArray(array,len);
swaps++;
}
}
}
return swaps;
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
  
int selectionSort(int arr[], int n)
{
int i, j, min_idx,swapCount=0;
  
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
  
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
       swapCount++;
       printArray(arr,n);
}
   return swapCount;
}
int main()
{
   int arraySize;
   int *array;
   char choice;
   while(true)
   {
       while(true)
       {
           cout<<"How large of an array would you like? Choose between 5-20:";
           cin>>arraySize;
           if(arraySize>=5 && arraySize<=20)
               break;
           else
           cout<<"Invalid size! Try again."<<endl;
       }
       array=new int[arraySize];
       populateArray(array,arraySize);

      
       cout<<"Bubble Sort of this array: "<<endl;
       printArray(array,arraySize);
       cout<<endl<<endl;
       int numberofSwapsBuble=bubleSort(array,arraySize);
       cout<<"\nSelct Sort of this array: "<<endl;
       printArray(array,arraySize);
       cout<<endl<<endl;
       int numberOfSwapsSelection=selectionSort(array,arraySize);
       cout<<"\nNumber of exchanges:\nBubble: "<<numberofSwapsBuble<<endl;
       cout<<"Select: "<<numberOfSwapsSelection<<endl<<endl;
       cout<<"Another visualization (Y/N)? > ";
       cin>>choice;
       if(choice=='n')
           break;
   }
   cout<<"Program ending. Bye!"<<endl<<endl;
   return 1;
}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
C++ This program will compare sorting algorithms by examining how the number of swaps/exchanges differs between...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...

    Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Program this in C There is a lot of sorting algorithms. So far, we have covered...

    Program this in C There is a lot of sorting algorithms. So far, we have covered selection and insertion sort. However, we only covered how selection sort is implemented using an array. Every sorting algorithm implemented using an array can also be implemented using a linked list. In this assignment, you will implement the selection sort algorithm using a linked list instead of an array. You will first create an interface for handling string items. Basically, you will need to...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • does anyone know how to do this? C++ Your task for this assignment is to use...

    does anyone know how to do this? C++ Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT