Wednesday, May 22, 2019

Selection sort C Program and Algorithm -Data Structures


For many other Language  Programs related to microprocessor 8086, data structures  and C++ visit our  BLOG 



Algorithm :(number of comparisions = n*(n-1)/2 and there are n-1 exchanges in this sort)
1. start
2. initialize small = arr[k]
3. initialize pos =k
4. repeate for j=k+1 to n 
5. if(small >arr[j]) then set small =arr[j]  set pos=j
6. stop

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],temp,n,i,j,pos;
clrscr();
printf(“enter array size”);
scanf(“%d”,&n);
printf(“enter array data”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
pos=I;
for(j=i+1;j<n;j++)
{
if(a[pos]>a[j])
pos=j;
}
if(pos!=i)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf(“result\n\n\n”);
for(i=0;i<ni++)
printf(“%d\n”,a[i]);
}

For many other Language Programs related to microprocessor 8086 ,C++, Data Structures visit our  BLOG 

There are programs of bubble , selection , linear search , pop, push ,etc for data structures

No comments:

Post a Comment