Question

In: Computer Science

C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the...

C++

Analysis of Sorting Algorithms

Design a class AbstractSort that can be used to analyze the number of comparisons performed by a sorting algorithm. The class should have a member function compare that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed. The class should be an abstract class with a pure virtual member function

void sort(int arr[ ], int size)

which, when overridden, will sort the array by calling the compare function to determine the relative order of pairs of numbers. Create a subclass of AbstractSort that uses a simple sorting algorithm to implement the sort function. The class should have a member function that can be called after the sorting is done to retrieve the number of comparisons performed.

Solutions

Expert Solution

//C++ CODE TO COPY//

#include<iostream>
using namespace std;

//abstratc class AbstractSort
class AbstractSort
{
   // Data members of class
public:
   int count_compare;
   //constructor
   AbstractSort()
   {
       this->count_compare = 0;
   }
  
   //compare function which will return true if first index (element) will greater than second
   //element provded in parameters
   bool compare(int elem1, int elem2)
   {
       //increment compare counter each time this function called
       count_compare++;
       if (elem1 > elem2)
           return true;
       else
           return false;
   }
   // Pure Virtual Function sort
   virtual void sort(int arr[], int size) = 0;

};

class sorting : public AbstractSort
{
public:
   //constructor
   sorting() : AbstractSort(){};
   void sort(int arr[], int size)
   {  
       //sorting elements by comparing each to array elements
       for (int i = 0; i < size; i++)
       {
           for (int j = i + 1; j < size; j++){
               //calling parent class compare function to determine maximum number
               if (compare(arr[i], arr[j])){
                   int temp = arr[i];
                   arr[i] = arr[j];
                   arr[j] = temp; // change elements location(index) if previouse one is greater than next one
              
               }
           }
       }
   }

   //getting total comparisons during sorting done
   int getCompareCount()
   {
       return this->count_compare;
   }
};

//amin driver function
int main()
{
   //initialising sorting class object
   sorting *sort = new sorting();
  
   //array which we have to sort
   int arr[] = {3, 8, 100, 23, 50, 7, 1, 55, 2};
   int size = sizeof(arr) / sizeof(arr[0]);

   cout << "\nArray before sorting: ";
   for (int i = 0; i < size; i++)
   {
       cout << arr[i]<<" ";
   }
   //calling to sort function
   sort->sort(arr, size);
   cout << "\n\nArray after sorting: ";
   for (int i = 0; i < size; i++)
   {
       cout << arr[i]<<" ";
   }
   cout << "\n\nNo of Comparison done: " << sort->getCompareCount() << endl;

   return 0;
}

//OUTPUT//

Comment down for any queries!
Please give a thumbs up if you are satisfied and helped with the answer :)


Related Solutions

Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
We saw in class different sorting algorithms, and we generally assumed the input to be sorted...
We saw in class different sorting algorithms, and we generally assumed the input to be sorted wasj an array. Here, the provided input is an unordered linked list of n elements with integer values. We are interested in sorting this list in increasing order (smallest first), in O(n log n) worst case time, while using constant space (also called in-place sorting). Note that recursion requires additional space on the stack, so should EC330 Applied Algorithms and Data Structures for Engineers,...
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex...
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex numbers. A number of the form a +ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. Complex numbers can also be represented as ordered pairs (a, b). The class you will design should have the following features. Constructor Your class...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAM PS: YOU ARE ANSEWRING THE SAME PROGRAMS I WANT DIFFERENT ONE PLEASE , THANK YOU . BECAUSE THE ONE WERE POSTING DOESNT WORKING !!
how can variance analysis be used to analyze and control costs
how can variance analysis be used to analyze and control costs
Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the...
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the same sorting algorithms as discussed in class / in the textbook. (a) What is the running time of Insertion Sort when all elements in a given list of length N are equal? Explain your answer. (b) Give a Θ-bound on the running time of Mergesort for the case that all elements in a given list of length N are equal (assuming N is a...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT