Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

INSTRUCTIONS In C++ write a class that can be used to sort numbers using the bub

ID: 3697016 • Letter: I

Question

INSTRUCTIONS

In C++ write a class that can be used to sort numbers using the bubble, insertion, and selection sort algorithms. The class should be properly written with a separate specification and implementation file. It should minimally contain the following methods outlined in the given shell:

#define LIST_SIZE 7

class Sorting {

private:

int numbersArray[LIST_SIZE];

public:

Sorting();

void DisplayArray();

void LoadArray(int*);

void BubbleSort();

void SelectionSort();

void InsertionSort();

}; // end - Sorting

The class should contain a method that displays the elements in the numbersArray along a single line. A LoadArray() method should also be included that takes an integer pointer as its only argument. The pointer should reference an array of elements and LoadArray() should copy those values into the numbersArray with a loop. When one of the sorting methods is evoked, the numbersArray should be sorted in the appropriate fashion with calls to the DisplayArray() method after each pass of the given algorithm. You may assume the list contains exactly 7 elements each time numbers are loaded into the class at first, but the preprocessor statement LIST_SIZE should be modifiable and the program’s logic should scale to accommodate an array of arbitrary size (the array initialization statement would have to be modified as well). The following driver function should seamlessly interface with your class:

#include <iostream>

using namespace std;

#include "sorting.h"

int main() {

Sorting sortClass;

int sortMe[LIST_SIZE] = {15, 14, 16, 7, 4, 8, 1};

// INITIAL ARRAY

sortClass.LoadArray(sortMe);

cout << "THE INITIAL LIST CONTAINS" << endl;

sortClass.DisplayArray();

cout << endl;

// BUBBLE SORT

sortClass.LoadArray(sortMe);

cout << "BEGIN BUBBLE SORT" << endl;

sortClass.BubbleSort();

sortClass.DisplayArray();

// INSERTION SORT

sortClass.LoadArray(sortMe);

cout << "BEGIN INSERTION SORT" << endl;

sortClass.InsertionSort();

sortClass.DisplayArray();

// SELECTION SORT

sortClass.LoadArray(sortMe);

cout << "BEGIN SELECTION SORT" << endl;

sortClass.SelectionSort();

sortClass.DisplayArray();

return 0;

}

Explanation / Answer

void selectionSort(int[] array)

{

    // let us find the smallest element starting from position i

    for (int i = 0; i < array.length - 1; i++)

    {

        int min = i; // now we will record the position of the smallest

        for (int j = i + 1; j < arry.length; j++)

        {

            // further update min when finding a smaller element

            if (array[j] < array[min])

                min = j;

        }

        // finally put the smallest element at position i

        swap(array, i, min);// call the swap function

    }

}

public static void swap (int[] arrray, int i, int j)

{

    int tempory = array[i];

    array[i] = array[j];

    array[j] = tempory;

}

void insertionSort(int[] array)

{

    for (int i = 1; i < arr.length; i++)

    {

        //This is a a temporary copy of the current element

        int temp = array[i];

        int j;

        // now,find the position for insertion

        for (j = i; j > 0; j--)

        {

            if (array[j - 1] < temp)

                break;

            // lets shift the sorted part to right

            array[j] = array[j - 1];

        }

        // finally insert the current element

        array[j] = temp;

    }

}

// bubble sort

void bubbleSort (int[] array)

{

    for (int i = array.length - 1; i >= 0; i--)

    {

        //we will bubble up

        for (int j = 0; j <= i - 1; j++)

        {

            if (array[j] > array[j + 1])

                swap(array, j, j + 1);

        }

    }

}

void display()     // to display the sorted array

{ for(i=0;i<10;i++)

cout<<array[i];

}}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote