C program to Selection Sort

Below is C program to Selection Sort.

Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* Selection Sort */
#include<stdio.h>
 
int main()
{
 int arr[10],i,j,k,t;
 printf("Please enter 10 values:\n");
 for(i=0;i<10;i++)
  scanf("%d",&arr[i]);
  
 for(i=0;i<10;i++)
 {
  k=i;
  for(j=i;j<10;j++)
  {
   if(a[k]>a[j])
    k=j;
  }
  if(k!=i)
  {
   t=a[k];
   a[k]=a[i];
   a[i]=t;
  }
 }
  
 printf("Sorted Array is:\n");
 for(i=0;i<10;i++)
  printf("%d\n",arr[i]);
  
 return 0;
}

No comments:

Post a Comment