Question

In: Computer Science

4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...

4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be
implemented using a dynamic array as well as a singly linked list. You are required to compare the
performance (sorting time) for the dynamic array and singly-linked list-based implementations.

You are given the startup codes for the dynamic array and singly-linked list based implementations. You
are required to implement the Selection Sort function in each of these codes. The main function is written
for you in such a way that the code will run for different values of list sizes ranging from 1000 to 20000,
in increments of 1000. The inputs for the maximum value and number of trials are 5000 and 50 for all
cases. The codes will output the average sorting time (in milliseconds) for each of value of list size for the
two implementations.
You are then required to tabulate the average sorting times observed for the two implementations and then
plot the same in Excel. Generate one plot that displays (compares) the sorting times for both the
implementations. Use the Add Trend Line option in Excel, choose the Power function option and display
the equations and the R2 values for the two curves. The R2 values are expected to be closer to 1.0. Make
sure the two equations and the R2 values are clearly visible in your Excel plot. (Please answer in C++)

Dynamic list implementation code:

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
#include <time.h>
#include <stdlib.h>

using namespace std;

// implementing the dynamic List ADT using array
// operations to be implemented: read, Modify, delete, isEmpty, insert, countElements

class List{

   private:
       int *array;
       int maxSize; // useful to decide if resizing (doubling the array size) is needed
       int endOfArray;
  
   public:
       List(int size){
           array = new int[size];
           maxSize = size;
           endOfArray = -1;
       }
      
       void deleteList(){
           delete[] array;
       }
      
       bool isEmpty(){
          
           if (endOfArray == -1)
               return true;
          
           return false;
       }
      
       void resize(int s){
          
           int *tempArray = array;
          
           array = new int[s];
          
           for (int index = 0; index < min(s, endOfArray+1); index++){
               array[index] = tempArray[index];
           }
          
           maxSize = s;
          
       }
  
      
       void insert(int data){
          
           if (endOfArray == maxSize-1)
               resize(2*maxSize);
          
           array[++endOfArray] = data;
          
       }
      
      
       void insertAtIndex(int insertIndex, int data){
          
           // if the user enters an invalid insertIndex, the element is
           // appended to the array, after the current last element          
           if (insertIndex > endOfArray+1)
               insertIndex = endOfArray+1;
          
           if (endOfArray == maxSize-1)
               resize(2*maxSize);          
          
           for (int index = endOfArray; index >= insertIndex; index--)
               array[index+1] = array[index];
          
           array[insertIndex] = data;
           endOfArray++;
          
       }
      
      
       int read(int index){
           return array[index];
       }
      
       void modifyElement(int index, int data){
           array[index] = data;
       }
          
      
       void deleteElement(int deleteIndex){
          
           // shift elements one cell to the left starting from deleteIndex+1 to endOfArray-1
           // i.e., move element at deleteIndex + 1 to deleteIndex and so on
          
           for (int index = deleteIndex; index < endOfArray; index++)
               array[index] = array[index+1];
          
           endOfArray--;  
      
       }
      
       int countList(){
           int count = 0;
           for (int index = 0; index <= endOfArray; index++)
               count++;
          
           return count;
       }
      
       void print(){
          
           for (int index = 0; index <= endOfArray; index++)
               cout << array[index] << " ";
          
           cout << endl;
          
       }

};


void SelectionSort(List list){
  
// Implement the selection sorting algorithm here...
  
  
}


int main(){

   using namespace std::chrono;
  
   srand(time(NULL));
  

  
   int maxValue;
   cout << "Enter the max value: ";
   cin >> maxValue;
  
   int numTrials;
   cout << "Enter the number of trials: ";
   cin >> numTrials;

for (int listSize = 1000; listSize <= 20000; listSize += 1000){

double totalSortingTime = 0;
  
for (int trials = 1; trials <= numTrials; trials++){
  
   List integerList(1);
  
   for (int i = 0; i < listSize; i++){
      
       int value = 1 + rand() % maxValue;  
       integerList.insertAtIndex(i, value);
   }
  

  
  
   high_resolution_clock::time_point t1 = high_resolution_clock::now();
   SelectionSort(integerList);
   high_resolution_clock::time_point t2 = high_resolution_clock::now();
  
   duration<double, std::milli> sortingTime_milli = t2 - t1;
   totalSortingTime += sortingTime_milli.count();
  
   integerList.deleteList();
  
}
  
  
   cout << "Average sorting time for list size " << listSize << " is " << (totalSortingTime/numTrials) << " milli seconds " << endl;

} // list size   loop

   system("pause");
  
return 0;
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <ctime>

#include <ratio>

#include <chrono>

#include <time.h>

#include <stdlib.h>

using namespace std;

// implementing the dynamic List ADT using array

// operations to be implemented: read, Modify, delete, isEmpty, insert, countElements

class List

{

private:

    int *array;

    int maxSize; // useful to decide if resizing (doubling the array size) is needed

    int endOfArray;

public:

    List(int size)

    {

        array = new int[size];

        maxSize = size;

        endOfArray = -1;

    }

    void deleteList()

    {

        delete[] array;

    }

    bool isEmpty()

    {

        if (endOfArray == -1)

            return true;

        return false;

    }

    void resize(int s)

    {

        int *tempArray = array;

        array = new int[s];

        for (int index = 0; index < min(s, endOfArray + 1); index++)

        {

            array[index] = tempArray[index];

        }

        maxSize = s;

    }

    void insert(int data)

    {

        if (endOfArray == maxSize - 1)

            resize(2 * maxSize);

        array[++endOfArray] = data;

    }

    void insertAtIndex(int insertIndex, int data)

    {

        // if the user enters an invalid insertIndex, the element is

        // appended to the array, after the current last element

        if (insertIndex > endOfArray + 1)

            insertIndex = endOfArray + 1;

        if (endOfArray == maxSize - 1)

            resize(2 * maxSize);

        for (int index = endOfArray; index >= insertIndex; index--)

            array[index + 1] = array[index];

        array[insertIndex] = data;

        endOfArray++;

    }

    int read(int index)

    {

        return array[index];

    }

    void modifyElement(int index, int data)

    {

        array[index] = data;

    }

    void deleteElement(int deleteIndex)

    {

        // shift elements one cell to the left starting from deleteIndex+1 to endOfArray-1

        // i.e., move element at deleteIndex + 1 to deleteIndex and so on

        for (int index = deleteIndex; index < endOfArray; index++)

            array[index] = array[index + 1];

        endOfArray--;

    }

    int countList()

    {

        int count = 0;

        for (int index = 0; index <= endOfArray; index++)

            count++;

        return count;

    }

    void print()

    {

        for (int index = 0; index <= endOfArray; index++)

            cout << array[index] << " ";

        cout << endl;

    }

};

void SelectionSort(List list)

{

    // Implement the selection sorting algorithm here...

    for (int i = 0; i < list.countList(); i++)

    {

        for (int j = i + 1; j < list.countList(); j++)

        {

            if (list.read(i) > list.read(j))

            {

                int temp = list.read(i);

                list.modifyElement(i, list.read(j));

                list.modifyElement(j, temp);

            }

        }

    }

}

int main()

{

    using namespace std::chrono;

    srand(time(NULL));

    int maxValue;

    cout << "Enter the max value: ";

    cin >> maxValue;

    int numTrials;

    cout << "Enter the number of trials: ";

    cin >> numTrials;

    for (int listSize = 1000; listSize <= 20000; listSize += 1000)

    {

        double totalSortingTime = 0;

        for (int trials = 1; trials <= numTrials; trials++)

        {

            List integerList(1);

            for (int i = 0; i < listSize; i++)

            {

                int value = 1 + rand() % maxValue;

                integerList.insertAtIndex(i, value);

            }

            high_resolution_clock::time_point t1 = high_resolution_clock::now();

            SelectionSort(integerList);

            high_resolution_clock::time_point t2 = high_resolution_clock::now();

            duration<double, std::milli> sortingTime_milli = t2 - t1;

            totalSortingTime += sortingTime_milli.count();

            integerList.deleteList();

        }

        cout << "Average sorting time for list size " << listSize << " is " << (totalSortingTime / numTrials) << " milli seconds " << endl;

    } // list size   loop

    system("pause");

    return 0;

}


Related Solutions

(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
Write a version of the selection sort algorithm in a function called selectionSort that can be...
Write a version of the selection sort algorithm in a function called selectionSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *Need answer in C++*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT