SELECTION SORT
Selection Sort: In Slection Sort we need to search for the smallest element in the list and interchange it with the element in the first place (0th) position. And we need to search for the smallest element in the list and interchange it with element in the second (1st) potion.
#include<iostream>
using namespace std;
template <class T>
void read(T a[], int n) {
for(int i=0;i<n;i++) {
cout<<"Enter elements:";
cin>>a[i];
}
}
template <class T>
void display(T a[], int n) {
for(int i=0;i<n;i++) {
cout<<" "<<a[i];
}
}
template <class T>
void sort(T a[], int n) {
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(a[j]<a[i]) {
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
int main() {
int arr[20],size;
cout<<"Enter the size of the array:";
cin>>size;
read(arr,size);
display(arr,size);
sort(arr,size);
cout<<"after sorting"<<endl;
display(arr,size);
}
Video Links:
Part 1: https://youtu.be/unhfw346nxQ
Part 2: https://youtu.be/IBklPVcek9o

Comments
Post a Comment