Array Sorting

Sorting is a technique to arrange the elements in an order (increasing or decreasing).

In this program, we are going to sort the content of an array using selection sort algorithm.

#include <stdio.h>

int main()
{
  int num[5];
  int i,j,temp;

  printf("Enter elements for the array : \n");
  for(i=0;i<5;i++)
  {
    scanf("%d",&num[i]);
  }

  printf("Unsorted Array : \n");
  for(i=0;i<5;i++)
  {
    printf("%d ",num[i]);
  }


  // Sorting procedure

  for(i=0;i<4;i++)
  {
    for(j=i+1;j<5;j++)
    {
      if(num[i] > num[j])
      {
        temp = num[i];
        num[i] = num[j];
        num[j] = temp;
      }
    }
  }

  printf("\nSorted Array : \n");
  for(i=0;i<5;i++)
  {
    printf("%d ",num[i]);
  }

  return 0;
}
OUTPUT:
Enter elements for the array :
15
3
7
10
1
Unsorted Array :
15 3 7 10 1
Sorted Array :
1 3 7 10 15

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.