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
Array programs
- Sequence filter in array in C
- Array Search (Linear search)
- Array Merging
- Array Sorting
- C program to take marks and show percentage accordingly.
- C program to take user input to an array and show its content.
- C program to search an element in the array using a user – defined function.
- C program to search an element in the array using bsearch() function.
- C program to sort an array using qsort() functions.
- Recursive binary search in C.
Trending