- Suppose we want to arrange numbers of a list in ascending order.
- We will divide our list in two parts: Sorted List & Unsorted List.
- Initially all the elements are considered as Unsorted.
- Now we will find minimum element from the unsorted list & swap it with the first element of unsorted list.
- Again we will find minimum element from unsorted list & swap it with the first element of unsorted list.
- "2" will become part of sorted list & process will be repeated for remaining unsorted list.
- Again we will find minimum element from unsorted list & swap it with first element of unsorted list.
- "3" will become part of sorted list & we will repeat our process for remaining unsorted list.
- Again we will find minimum element from unsorted list & swap it with first element of unsorted list.
- As "5" is minimum element & it is already at its correct position so, no swap is required.
- Similarly the remaining elements of unsorted list will become part of sorted list.
Program
//Selection Sort#include<iostream.h>#include<conio.h>int selection_sort(int a[],int n){int i,j,t,u;for(i=0;i<n-1;i++){u=i;//index of first element of unsorted listfor(j=i+1;j<n;j++){if(a[j]<a[u])u=j;//updated when//element less than minimum is found}if(i!=u)//no need to swap{ //if element is already at correct positiont=a[i];a[i]=a[u];a[u]=t;}}}void main(){int a[]={12,5,8,2,1,7,3},i;clrscr();selection_sort(a,7);cout<<"Sorted Array"<<endl;for(i=0;i<7;i++){cout<<a[i]<<endl;}getch();}Selection Sort technique, complexity & program with minimum number of swaps. https://www.instagram.com/reel/Cg6So4Uhwza/?igshid=YmMyMTA2M2Y= #datastructure #sorting #searching #minimumelement #bestcoding #goodcode #leetcode #aws #python #cprogram #cprogramming #bca #mca #it #cs #complexity #clanguage #java #javascripSot #DeveloperMode Selection Sort Program Algorithm to sort integers Sorting of Integer numbers
No comments:
Post a Comment