Task: Sorting's using a Class
Code:
#include<iostream>
using namespace std;
class Sort
{
int arr[50];
int n;
public:
void userInput();
void fixedInput();
void bubble();
void insertion();
void selection();
void display();
};
void Sort::userInput()
{
cout << "Enter size of elements: " << endl;
cin >> n;
cout << "Enter " << n << " values" << endl;
for (int b = 0; b <= n - 1; b++)
{
cout << "Value [" << b + 1 << "]:";
cin >> arr[b];
}
cout << "Elements of your unsorted list: [ ";
for (int c = 0; c <= n - 1; c++)
{
cout << arr[c] << " ";
}
cout << "]" << endl;
}
void Sort::fixedInput()
{
n = 4;
arr[0] = 4;
arr[1] = 2;
arr[2] = 1;
arr[3] = 3;
}
void Sort::bubble()
{
for (int i = 1; i < n; ++i)
{
for (int j = 0; j < (n - i); ++j)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void Sort::insertion()
{
for (int i = 1 ; i < n; i++)
{
int temp = arr[i];
int j = i - 1;
while ((temp < arr[j]) && (j >= 0))
{
arr[j+1] = arr[j]; //moves element forward
j = j - 1;
}
arr[j+1] = temp; //insert element in proper place
}
}
void Sort::selection()
{
for (int i = 0; i < n - 1; i++) //loop through all numbers
{
int indexMin = i; //set current element as minimum
for (int j = i + 1; j < n; j++) //check the element to be minimum
{
if (arr[j] < arr[indexMin])
{
indexMin = j;
}
}
if (indexMin != i)
{
//swap the numbers
int temp = arr[indexMin];
arr[indexMin] = arr[i];
arr[i] = temp;
}
}
}
void Sort::display()
{
cout << "Elements of your sorted list: [ ";
for (int x = 0; x < n; x++)
{
cout << arr[x] << " ";
}
cout << " ]";
}
int main()
{
Sort mSort;
//mSort.userInput();
mSort.fixedInput();
//mSort.bubble();
mSort.insertion();
//mSort.selection();
mSort.display();
return 0;
}
Task: Sorting's using a Class (with Template)
Code:
#include<iostream>
using namespace std;
template<typename ItemType>
class Sort
{
ItemType arr[50];
int n;
public:
void userInput();
void fixedInput();
void bubble();
void insertion();
void selection();
void display();
};
template<typename ItemType>
void Sort<ItemType>::userInput()
{
cout << "Enter size of elements: " << endl;
cin >> n;
cout << "Enter " << n << " values" << endl;
for (int b = 0; b <= n - 1; b++)
{
cout << "Value [" << b + 1 << "]:";
cin >> arr[b];
}
cout << "Elements of your unsorted list: [ ";
for (int c = 0; c <= n - 1; c++)
{
cout << arr[c] << " ";
}
cout << "]" << endl;
}
template<typename ItemType>
void Sort<ItemType>::fixedInput()
{
n = 4;
arr[0] = 4.3;
arr[1] = 2.5;
arr[2] = 1.6;
arr[3] = 3.2;
}
template<typename ItemType>
void Sort<ItemType>::bubble()
{
for (int i = 1; i < n; ++i)
{
for (int j = 0; j < (n - i); ++j)
{
if (arr[j] > arr[j+1])
{
ItemType temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
template<typename ItemType>
void Sort<ItemType>::insertion()
{
for (int i = 1 ; i < n; i++)
{
ItemType temp = arr[i];
int j = i - 1;
while ((temp < arr[j]) && (j >= 0))
{
arr[j+1] = arr[j]; //moves element forward
j = j - 1;
}
arr[j+1] = temp; //insert element in proper place
}
}
template<typename ItemType>
void Sort<ItemType>::selection()
{
for (int i = 0; i < n - 1; i++) //loop through all numbers
{
int indexMin = i; //set current element as minimum
for (int j = i + 1; j < n; j++) //check the element to be minimum
{
if (arr[j] < arr[indexMin])
{
indexMin = j;
}
}
if (indexMin != i)
{
//swap the numbers
ItemType temp = arr[indexMin];
arr[indexMin] = arr[i];
arr[i] = temp;
}
}
}
template<typename ItemType>
void Sort<ItemType>::display()
{
cout << "Elements of your sorted list: [ ";
for (int x = 0; x < n; x++)
{
cout << arr[x] << " ";
}
cout << " ]";
}
int main()
{
Sort<float> mSort;
//mSort.userInput();
mSort.fixedInput();
mSort.bubble();
//mSort.insertion();
//mSort.selection();
mSort.display();
return 0;
}
0 Comments