C program for Selection Sort

Write a C Program For Selection Sort 

C program for Selection Sort: In Selection sort, the smallest element is exchanged with the first element of the unsorted list of elements (the exchanged element takes the place where the smallest element is initially placed). Then the second smallest element is exchanged with the second element of the unsorted list of elements and so on until all the elements are sorted.
C Program For Binary Search: Check Here

Program

#include <stdio.h>

int main()
{
int array[100], n, c, d, position, t;

  printf(“Enter number of elementsn“);
scanf(“%d”, &n);

  printf(“Enter %d integersn“, n);

  for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);

  for (c = 0; c < (n – 1); c++) // finding minimum element (n-1) times
{
position = c;

    for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}

  printf(“Sorted list in asending order:n“);

  for (c = 0; c < n; c++)
printf(“%dn“, array[c]);

  return 0;
}

Output

Selection sort output

 

Leave a Comment

error: Content is protected !!