What is a Sorting of an Array in C Programming ?

sorting of an array
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,

  1. Sorting of an Array in C
  2. Sorted Array
  3. Types of an Array Sorting
  4. Insertion Sort
  5. Selection Sort
  6. Bubble Sort

1. Sorting of an Array in C :

Array Sorting is the process of taking the individual elements of an array and arranging them in the same type of logical order according to series of rules defined by the user or in another words we can say that sorting of an array is the process with the help of which programmer can arrange the element of an array in ascending or as well as descending order.

2. Sorted Array:

A Sorted array is an array data structure in which each element is sorted either in ascending order or descending order and placed at equally spaced addressed in the computer memory.

3. Types of Array Sorting:

There are several method of array sorting but here we will discuss three most used method for sorting an array in C,
  1. Insertion Sort
  2. Selection Sort
  3. Bubble Sort

1. Insertion Sort:

Insertion Sort is a sorting algorithms that place an unsorted array at its suitable place in each iteration.

Insertion sort works similarly as we sort card in our hand in card game, we assume that the first card is already sorted, then we select an unsorted card, if the card is greater than the card in the hand, it is placed right otherwise to the left, In the same way other unsorted card taken and put in the right place.

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
Example of Insertion Sort
_____________________________________________________________

2. Selection Sort:

In selection sort, the first smallest element is selected from unsorted array and placed at the first position, after that second smallest element selected and placed in the second position the process continue until the array is sorted.

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;

______________________________

Sorting of an array by selection sort
Example of Selection Sort
______________________________


3. Bubble Sort:

Bubble Sort is sorting algorithm that compare to adjacent element and swap them until they are not intend order.
Just like movement of air bubble in the water that rise up to the surface of each element of the array move to the end in each iteration. Therefore it is called 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;
}
    

_____________________________________________________________

Sorting of an array by using bubble sort
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

Post a Comment (0)
Previous Post Next Post