Data Structure (2130702)

BE | Semester-3   Winter-2016 | 01/02/2017

Q4) (a)

Write a ‘C’ function for Selection sort.

#include<stdio.h>
void main()
{
 int array[100], n, i, j, min, temp;
 printf("Enter number of elements\n");
 scanf("%d", &n); printf("Enter %d integers\n", n);
 for ( i = 0 ; i < n ; i++ )
 {
  scanf("%d", &array[i]);
 }
 for ( i = 0 ; i < ( n - 1 ) ; i++ )
 {
  min = i;
  for ( j = i + 1 ; j < n ; j++ )
   {
    if ( array[min] > array[j] )
    min = j;
   }
   if ( min != i )
   {
    temp = array[i];
    array[i] = array[min];
    array[min] = temp;
   }
  }
  printf("Sorted list in ascending order:\n");
  for ( i = 0 ; i < n ; i++ )
  {
   printf("%d\n", array[i]);
  }
  getch();
 }