C++ Programming Blog

 
 
 

Image

#include <iostream.h>
#include <conio.h>
#define MAX 10

class selsort{
    int arr[MAX],n;
    public:
    void getdata();
    void showdata();
    void sortLogic();
};

void selsort :: getdata(){
    cout<<\"How many elements you require : \";
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>arr[i];
}

void selsort :: showdata(){
    cout<<\"\\n--Display--\\n\";
    for(int i=0;i<n;i++)
        cout<<arr[i]<<\"   \";
}

void selsort :: sortLogic(){
    int temp,min;
    for(int i=0;i<n;i++){
        min=i;
        for(int j=i+1;j<n;j++){
            if(arr[min] > arr[j]){
                min=j;
            }
        }
        temp = arr[min];
        arr[min] = arr[i];
        arr[i] = temp;
        cout<<\"\\n arr[min] = \"<<arr[min]<<\"  arr[i] = \"<<arr[i];
    }
}

void main(){
    clrscr();
    cout<<\"\\n*****Selection Sort*****\\n\";
    selsort obj;
    obj.getdata();
    obj.sortLogic();
    obj.showdata();
    getch();
}

 
 
Didn't find what you were looking for? Find more on Program to perform selection sort