IN C++ PLEASE INSTRUCTIONS Write a class that can be used to sort numbers using
ID: 3677826 • Letter: I
Question
IN C++ PLEASE
INSTRUCTIONS
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
Please find the required class below :
#include <stdio.h>
#define LIST_SIZE 7
class Sorting {
private:
int numbersArray[LIST_SIZE];
public:
Sorting();
void DisplayArray(){
for(int k=0;k<LIST_SIZE;k++) {printf("%d ",numbersArray[k]);}
printf(" ");
}
void LoadArray(int* arr){
for(int i=0;i<LIST_SIZE;i++)
numbersArray[i] = arr[i];
}
void BubbleSort(){
int i,j,x;
for(i=0;i<LIST_SIZE;i++) {
for(j=i+1;j<LIST_SIZE;j++) {
if(numbersArray[i]>numbersArray[j]) {
x=numbersArray[i];
numbersArray[i]=numbersArray[j];
numbersArray[j]=x;
}
}
}
}
void SelectionSort(){
int i,j,x,min,k;
for(i=0;i<LIST_SIZE;i++) {
min=i;
for(j=i+1;j<LIST_SIZE;j++) {
if(numbersArray[min]>numbersArray[j]) {
min=j;
}
x=numbersArray[min];
numbersArray[min]=numbersArray[j];
numbersArray[j]=x;
}
}
}
void InsertionSort(){
int i,j,key;
for(i=1;i<LIST_SIZE;i++)
{
key=numbersArray[i];
while(i>0 && numbersArray[i-1]>key) {
j=numbersArray[i];
numbersArray[i]=numbersArray[i-1];
numbersArray[i-1]=j;
--i;
}
}
}
};