Question

In C++ language, implement a class that can sort an array of numbers using all three...

In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy.

Note: IF statements increment the counter by 1, unless they have both parameters as elements of the array being sorted. In such a case, the IF statements increment the counter by two instead.

For example, the following line would increase the value of counter by 1:

       if(array[x] > y)

This statement would increase the value by two since it accesses the array twice:

       if (array[x] < array[y])

Assignment statements and reads for printing each increase the counter value by 1.

Swap statements take up 4 accesses (read: get a and store in temp, write: set a to b (a read and a write), write: set b to temp).

Sample Output

Enter an array of 10 values.

Enter value 1: 90

Enter value 2: 33

Enter value 3: 22

Enter value 4: -7

Enter value 5: 100

Enter value 6: 8

Enter value 7: 11

Enter value 9: 12

Enter value 10: 55

Sorting a copy of the array using Bubble Sort...

Sorting a copy of the array using Selection Sort...

Sorting a copy of the array using Quick Sort...

Array has been sorted

Bubble Sort accessed the array 78 times.

Selection Sort accessed the array 99 times.

Quick Sort accessed the array 50 times.

Quick Sort had the least number of accesses.

Run the code on several different arrays and see if one algorithm is consistently faster (i.e., has less accesses than the others). Include your results in the ending comment block of your submitted code. Note that I came up with the numbers/results in the sample output at “random.” They do not represent an actual run of the code on those numbers; I want all your results to come from your code’s actual run.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

please read the following code with comment for better understand.

//Modified sorting program
#include <iostream>
using namespace std;
//swaping function for swapping element
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//bubble sort
int bubble_sort(int arr[])
{
int i, j, temp, c=0,n=10;
for (i = 0; i < n-1; i++)   
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{   //increment by 2 because twice array searching
   c=c+2;
               swap(&arr[j], &arr[j+1]);
           }
  
   return c;
}
//selection sort
int selection_sort(int arr[])
{
int i, j, min,c=0,n=10,temp=0;
for (i = 0; i < n-1; i++)
{
min = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min])
   {
       min = j;
       //increment by 2 because twice array searching
       c=c+2;
      
           }     
  
       swap(&arr[min], &arr[i]);
  
}
  
   return c;
}
//quick sort
int count=0;
int partition (int arr[], int low, int high)
{
int pivot = arr[high],temp;
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <= pivot)
{
i++;
           //increment for only one time
           count=count+1;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
int
quick_sort(int arr[], int low, int high)
{   
if (low < high)
{
int pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
return count;
}
int main(){
   int number[10],number1[10],number2[10],number3[10];
   cout<<"Enter an array of 10 values";
   for(int i=0;i<10;i++){
       cout<<"\nEnter value "<<i+1<<": ";
       cin>>number[10];
   }
   cout<<"\nSorting a copy accessed the array using Bubble Sort...";
   for(int j=0;j<10;j++){
       number1[j]=number[j];
   }
   int c_bubble_sort = bubble_sort(number1);
   cout<<"\nSorting a copy accessed the array using Bubble Sort...";
   for(int j=0;j<10;j++){
       number2[j]=number[j];
   }
   int c_selection_sort = selection_sort(number2);
   cout<<"\nSorting a copy accessed the array using Bubble Sort...";
   for(int j=0;j<10;j++){
       number3[j]=number[j];
   }
   int c_quick_sort = quick_sort(number3,0,9);
   cout<<"\nArray has been sorted";
   cout<<"\nBubble sort Accessed the array "<<c_bubble_sort<<" times.";
   cout<<"\nSelection sort Accessed the array "<<c_selection_sort<<" times.";
   cout<<"\nQuick sort Accessed the array "<<c_quick_sort<<" times.";
   if((c_quick_sort<c_selection_sort) && (c_quick_sort<c_bubble_sort)){
       cout<<"Quick sort had the least number of accesses";
   }
   else if ((c_selection_sort<c_quick_sort) && (c_selection_sort<c_bubble_sort)){
       cout<<"Selection sort had the least number of accesses";
   }
   else{
       cout<<"Bubble sort had the least number of accesses";
   }
}

Add a comment
Know the answer?
Add Answer to:
In C++ language, implement a class that can sort an array of numbers using all three...
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
  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • Analyze two sorts in C++, the first being a bubble sort and the second being a...

    Analyze two sorts in C++, the first being a bubble sort and the second being a quick sort. Create a project for each sort. Use 4 data set sizes. Results will be based on execution time for each sort. The size should have a regular increment. For example, using a regular increment of 10,000, you might have sizes of 10,000, 20,000, 30,000, and 40,000. Choose your data size based upon the ability to obtain meaning results. Need: Two screen shots...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results data Array:...

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

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
Active Questions
ADVERTISEMENT