Selection Sort Using Templates
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
T checkInput(T Input, string content);
template <typename Type>
class SelectionSort
{
Type array[10];
Type arr[10];
public:
SelectionSort(int); // Default Constructer Declaration
~SelectionSort() {} // Destructer
bool selectionSort(int);
void traverseBeforeSorting(int);
void traverseAfterSorting(int);
};
// Default Constructer Definition
template <typename Type>
SelectionSort<Type>::SelectionSort(int num)
{
char content2[20];
for (int i = 0; i < num; i++)
{
sprintf(content2, "\nEnter The Value of Element %d\n", i + 1);
cout << content2;
cin >> array[i];
array[i] = checkInput(array[i], content2);
arr[i] = array[i];
}
}
template <typename Type>
bool SelectionSort<Type>::selectionSort(int num)
{
Type temp;
int min_index;
for (int i = 0; i < num - 1; i++)
{
min_index = i;
for (int j = i + 1; j < num; j++)
{
if (array[j] < array[min_index])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return 1;
}
template <typename Type>
void SelectionSort<Type>::traverseBeforeSorting(int num)
{
cout << "\nArray Before Sorting = ";
for (int i = 0; i < num; i++)
{
cout << arr[i] << " ";
}
cout << '\n';
}
template <typename Type>
void SelectionSort<Type>::traverseAfterSorting(int num)
{
cout << "\nArray After Sorting = ";
for (int i = 0; i < num; i++)
{
cout << array[i] << " ";
}
cout << "\n";
}
template <typename T>
T checkInput(T Input, string content)
{
while (!cin.good())
{
cout << "Wrong Input, Please Enter It Correctly\n";
cin.clear();
cin.ignore(100, '\n');
cout << content;
cin >> Input;
}
while (Input < 0)
{
cout << "This Input Cannot Be Negative\n";
cin.clear();
cin.ignore(100, '\n');
cout << content;
cin >> Input;
}
return Input;
}
// Driver Function
int main()
{
int num = 0;
bool flag = 0;
string content1 = "Enter The Number of Elements\n";
SelectionSort<int> *ss = NULL; // NULL Pointer
// SelectionSort<float> *ss = NULL; // NULL Pointer
while (true)
{
int userInput;
string content = "\n1. Press 1 To Take Input\n2. Press 2 To Print Array Before Sorting\n3. Press 3 To Sort Array\n4. Press 4 To Print Array After Sorting\n5. Press 5 To Exit\n";
cout << content;
cin >> userInput;
userInput = checkInput(userInput, content);
switch (userInput)
{
case 1:
cout << content1;
cin >> num;
num = checkInput(num, content1);
ss = new (nothrow) SelectionSort<int>(num);
// ss = new (nothrow) SelectionSort<float>();
if (!ss)
{
cout << "Memory Allocation Failed\n";
exit(0);
}
break;
case 2:
if (num == 0)
{
cout << "Array is Empty\n";
}
else
{
ss->traverseBeforeSorting(num);
}
break;
case 3:
if (num == 0)
{
cout << "Array is Empty\n";
}
else
{
flag = ss->selectionSort(num);
cout << "\nArray Sorted\n";
}
break;
case 4:
if (num == 0)
{
cout << "Array is Empty\n";
}
else if (!flag)
{
cout << "\nArray is Not Sorted, First Sort It.\n";
}
else
{
ss->traverseAfterSorting(num);
}
break;
case 5:
exit(0);
default:
cout << "Please Enter Correct Input\n";
break;
}
}
delete ss;
return 0;
}
// Output
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 2
// Array is Empty
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 3
// Array is Empty
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 4
// Array is Empty
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 1
// Enter The Number of Elements
// 8
// Enter The Value of Element 1
// 22
// Enter The Value of Element 2
// 44
// Enter The Value of Element 3
// 99
// Enter The Value of Element 4
// 88
// Enter The Value of Element 5
// 66
// Enter The Value of Element 6
// 44
// Enter The Value of Element 7
// 55
// Enter The Value of Element 8
// 11
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 2
// Array Before Sorting = 22 44 99 88 66 44 55 11
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 4
// Array is Not Sorted, First Sort It.
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 3
// Array Sorted
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 4
// Array After Sorting = 11 22 44 44 55 66 88 99
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 2
// Array Before Sorting = 22 44 99 88 66 44 55 11
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 4
// Array After Sorting = 11 22 44 44 55 66 88 99
// 1. Press 1 To Take Input
// 2. Press 2 To Print Array Before Sorting
// 3. Press 3 To Sort Array
// 4. Press 4 To Print Array After Sorting
// 5. Press 5 To Exit
// 5
Comments
Post a Comment