Selection Sort (सिलेक्शन सॉर्ट )

Selection Sort (सिलेक्शन सॉर्ट ) :-

It is simple and effective method of sorting in which first of all we assume that first element of the list is minimum and store it in variable min after that we compare all other elements of list with min variable to find out which element is minimum in the list. In the end we have minimum element in variable min , now we swap min variable with first element of list. this process will continue for all other elements of the list.    

यह सॉर्टिंग की सरल एवं प्रभावी युक्ति है इसमें सर्वप्रथम लिस्ट के पहले एलिमेंट को सबसे छोटा एलिमेंट माना जाता है एवं उसे min नामक वेरिएबल में रखा जाता है इसके पश्चात् इस min की तुलना लिस्ट के अन्य सभी एलेमेंट्स से यह जांचने के लिए की जाती है कि उनमे से सबसे छोटा एलिमेंट कौन सा है? अंत में हमे सबसे छोटा एलिमेंट प्राप्त हो जाता है जिसे नया min मानकर पहले स्थान पर रखा जाता है इस हेतु स्वैपिंग का प्रयोग किया जाता है। यही कार्य लिस्ट के अन्य एलेमेंट्स के लिए किया जाता है जब तक की सभी एलेमेंट्स सॉर्ट न हो जाये।      

Program/function/algorithm for Selection sort:-

File Name:- ssort.cpp

#include<iostream.h>
#include<conio.h>
#define N 5
void ssort(){
int list[N],i,j,k,min,temp;
cout<<"Enter Elements of List"<<endl;
for(i=0;i<N;i++){
cout<<"Enter "<<i+1<<" th element=";
cin>>list[i];
}
cout<<"Before Sorting List is"<<endl;
for(i=0;i<N;i++){
cout<<list[i]<<" ";
}
for(i=0;i<N-1;i++){
min=list[i];
k=i;
for(j=i+1;j<N;j++){
if(list[j]<min){
min=list[j];
k=j;
}
}
temp=list[i];
list[i]=list[k];
list[k]=temp;
}

cout<<endl<<"After Sorting List is:"<<endl;
for(i=0;i<N;i++){
cout<<list[i]<<" ";
}
return;
}
void main(){
clrscr();
cout<<"Selection Sort"<<endl;
ssort();
getch();
}



File Name:- ssort.c

#define N 5
void ssort(){
int list[N],i,j,k,min,temp;
printf("Enter Elements of List\n");
for(i=0;i<N;i++){
printf("Enter %d element=",i+1);
scanf("%d",&list[i]);
}
printf("Before Sorting List is:\n");
for(i=0;i<N;i++){
printf("%d ", list[i]);
}
for(i=0;i<N-1;i++){
min=list[i];
k=i;
for(j=i+1;j<N;j++){
if(list[j]<min){
min=list[j];
k=j;
}
}
temp=list[i];
list[i]=list[k];
list[k]=temp;
}

printf("\nAfter Sorting List is:\n");
for(i=0;i<N;i++){
printf("%d ",list[i]);
}
return;
}
void main(){
clrscr();
printf("Selection Sort\n");
ssort();
getch();
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...