![]() |
| Sorting of an Array in C |
In this article we will be going to discuss about Sorting of an Array in C in which following topics are included such as,
- Sorting of an Array in C
- Sorted Array
- Types of an Array Sorting
- Insertion Sort
- Selection Sort
- Bubble Sort
1. Sorting of an Array in C :
2. Sorted Array:
3. Types of Array Sorting:
- Insertion Sort
- Selection Sort
- Bubble Sort
1. Insertion Sort:
Example of Insertion Sort:
#include<stdio.h>
int main()
{
int i,j,key;
int a[5];
printf("Enter the element:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(j=0;j<5;j++)
{
key=a[j];
i=j-1;
while(i>=0 && a[i]>key)
{
a[i+i]=a[i];
i=i-1;
}
a[i+1]=key;
}
printf("Sorted array:\n");
for(i=0;i<5;i++)
printf("%d",a[i]);
return 0;
}
_____________________________________________________________
![]() |
| Example of Insertion Sort |
2. Selection Sort:
Example of Selection Sort:
#include<stdio.h>
int main()
{
int i,j,temp;
int a[5];
printf("Enter the element:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("Sorted array:\n");
for(i=0;i<5;i++)
printf("%d \n",a[i]);
return 0;
______________________________
![]() |
| Example of Selection Sort |
3. Bubble Sort:
Example of Bubble Sort:
#include<stdio.h>
int main()
{
int i,j,temp;
int a[5];
printf("Enter the element:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=5-1;j>i;j--)
{
if(a[j]>a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j]=temp;
}
}
}
printf("Sorted array:\n");
for(i=0;i<5;i++)
printf("%d \n",a[i]);
return 0;
}
_____________________________________________________________
![]() |
| Example of Bubble Sort |
Conclusion:
In this article we have learn about Sorting of an Array in C and its three major used method of sorting in array, rather than this we will discuss Sorting of an array in detail during the study of Data Structure, rest of all if you have any doubt in this article than feel free ask in comment section



