Monday 14 March 2016

SELECTION SORT


Write a C program to perform selection sort on an array of n elements. 
Selection sort algorithm starts by comparing first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is compared. This completes the first step of selection sort.(Refer below diagram ....)

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next n integers correspond to the elements in the array.

Output Format: 
Refer sample output for formatting specs.

Sample Input and Output1: 
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of elements in the array
6
Enter element 1
2
Enter element 2
7
Enter element 3
3
Enter element 4
8
Enter element 5
5
Enter element 6
1
Selection sort.
array before sorting:
2 7 3 8 5 1
After Iteration 1
1 7 3 8 5 2
After Iteration 2
1 2 7 8 5 3
After Iteration 3
1 2 3 8 7 5
After Iteration 4
1 2 3 5 8 7
After Iteration 5
1 2 3 5 7 8
After Iteration 6
1 2 3 5 7 8
array after sorting:
1 2 3 5 7 8

Sample Input and Output2: 
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of elements in the array
5
Enter element 1
13
Enter element 2
9
Enter element 3
7
Enter element 4
4
Enter element 5
2
Selection sort.
array before sorting:
 13 9 7 4 2
 After Iteration 1
 2 13 9 7 4
 After Iteration 2
 2 4 13 9 7
 After Iteration 3
 2 4 7 13 9
 After Iteration 4
 2 4 7 9 13
 After Iteration 5
 2 4 7 9 13
 array after sorting:
 2 4 7 9 13

Code:
  #include<stdio.h>
int main(){
  int a[20],n,i;
  int j,temp=0,k;
  printf("Enter the number of elements in the array\n");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    printf("Enter element %d\n",i);
    scanf("%d",&a[i]);
  }
  printf("Selection sort.\n");
  printf("array before sorting:\n");
  for(i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  for(i=1;i<=n;i++)
  {
    for(j=i+1;j<=n;j++)
    {
      if(a[i]>a[j])
      {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
      }
    }
    printf("\nAfter Iteration %d\n",i);
      for(k=1;k<=n;k++)
      {
        printf("%d ",a[k]);
      }
  }
  printf("\narray after sorting:\n");
  for(i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  return 0;
}

3 comments:

  1. Why a[20]?
    you are getting the size from the user.
    so
    scnaf("%d",&n);
    int a[n];

    ReplyDelete
  2. #include
    int main()
    {
    int n,i,j,temp=0;
    printf("Enter the number of elements in the array\n");
    scanf("%d",&n);
    int array[n];
    for(i=1;i<=n;i++)
    {
    printf("Enter element %d\n",i);
    scanf("%d",&array[i]);
    }
    printf("Selection sort.\narray before sorting:\n");
    for(i=1;i<=n;i++)
    {
    printf("%d ",array[i]);
    }
    printf("\n");
    for(j=1;j<=n;j++)
    {
    for(i=j;iarray[i+1])
    {
    temp=array[i+1];
    array[i+1]=array[j];
    array[j]=temp;
    }

    }
    printf("After Iteration %d\n",j);
    for(i=1;i<=n;i++)
    {
    printf("%d ",array[i]);
    }
    printf("\n");
    }
    printf("array after sorting:\n");
    for(i=1;i<=n;i++)
    {
    printf("%d ",array[i]);
    }
    return 0;
    }

    ReplyDelete