Hi @Stuti Thakur ,
For point-related knowledge, you need to review it again. The array you are sorting is an integer one-dimensional array, so the function parameter should also be an array of the same type. I modified the code you provided, now the code can realize the sorting function, you could refer to it.
By the way , you could use this function when uploading the code, which will help everyone understand your code more easily.
#include <iostream>
using namespace std;
class sort {
public:
int size;
sort()
{
cout << "Enter the size of array for sorting";
cin >> size;
}
};
template <class T>
class Selection :public sort
{
public:
int* Array = new int[size];
void swap(int* a, int* b);
void printArray(int* Array, int size);
void selectionSort(int* Array, int size);
Selection()
{
cout << "Enter the array for sorting";
for (int i = 0; i < size; i++)
{
cin >> Array[i];
}
}
};
template <class T>
void Selection<T>::swap(int* a, int* b) // function to swap the the position of two elements
{
int temp = *a;
*a = *b;
*b = temp;
}
template <class T>
void Selection<T>::printArray(int* Array, int size) // function to print an array
{
for (int i = 0; i < size; i++)
{
cout << Array[i] << " ";
}
cout << endl;
}
template <class T>
void Selection<T>::selectionSort(int* Array, int size) {
for (int step = 0; step < size - 1; step++) {
int min = step;
for (int i = step + 1; i < size; i++)
{
if (Array[i] < Array[min]) // To sort in descending order, change > to < in this line.
min = i; // Select the minimum element in each loop.
}
swap(&Array[min], &Array[step]); // put minimum value at the correct position
}
}
int main() // driver code
{
Selection<int>l;
l.selectionSort(l.Array, l.size);
cout << "Sorted array in Acsending Order:\n";
l.printArray(l.Array, l.size);
}
Best regards,
Elya
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.