Top 10 Sorting Algorithms in C

In this, we will discuss the basic sorting algorithm in C language.

1.) Bubble Sort :- In this, each pair of adjacent elements is compared, and the elements are swapped if they are not in order.

void BubbleSort(int A[], int n)
{
    int i,j,flag;
    for(i=0; i<n-1; i++)
    {
        flag=0;
        for(j=0; j<n-i-1; j++)
        {
            if(A[j] > A[j+1]){
                Swap(&A[j], &A[j+1]);
                flag=1;
            }
        }
        if(flag==0)
            printf(" Array is Already Sorted :\n\n");
            break;
    }
}

2.) Insertion Sort :- It compares the current element with the largest value in the sorted array. If the current element is greater, then it leaves the element in its place and moves on to the next element else it finds its correct position in the sorted array and moves it to that position. This is done by shifting all the elements, which are larger than the current element, in the sorted array to one position ahead.

void InsertionSort (int A[], int n)
{
    for(int i=1; i<n; i++)
    {
        int j = i-1;
        int x = A[i];
        while(j>-1 && A[j] > x){
            A[j+1] = A[j];
            j--;
        }
        A[j+1] = x;
    }
    // printf(" Array after Sorting :\n\n");
}

3.) Selection Sort :- Find the minimum element in the array and swap with the index element.

void SelectionSort(int A[], int n)
{
    int i,j,k;
    for(i=0; i<n-1; i++)
    {
        for(j=k=i; j<n; j++)
        {
            if(A[j] < A[k])
                k = j;
        }
        Swap(&A[i], &A[k]);
    }
    // printf(" Array after Sorting :\n\n");
}

4.) Quick Sort :- Quick sort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot.

int Partition(int A[], int low, int high)
{
    int pivot = A[low];
    int i=low, j=high;
    do{
        do{
            i++;
        }while(A[i] <= pivot);
        do{
            j--;
        }while(A[j] > pivot);

        if(i<j)
            Swap(&A[i], &A[j]);
    }while(i<j);

    Swap(&A[low], &A[j]);
    return j;
}
void QuickSort(int A[], int low, int high)
{
    int j;
    if(low<high)
    {
        j = Partition(A, low, high);
        QuickSort(A,low,j);
        QuickSort(A,j+1,high);
    }
}

5.) Merge Sort :- It divides the given list into two equal halves, calls itself for the two halves and then merges the two sorted halves.

void MergeSort(int A[], int low, int mid, int high)
{
    int i=low, j=mid+1, k=low;
    int B[100];

    while(i<=mid && j<=high)
    {
        if(A[i] < A[j])
            B[k++] = A[i++];
        else
            B[k++] = A[j++];
    }
    for(; i<=mid; i++)
        B[k++] = A[i];
    for(; j<=high; j++)
        B[k++] = A[j];

    for(i=low; i<=high; i++)
        A[i] = B[i];
}

6.) Iterative Merge Sort :- In iterative merge sort, we will divide the elements into equal halves using a recursive approach and then merge them back as a sorted array using the iterative approach.

void IterativeMergeSort(int A[], int n)
{
    int p,low,high,mid,i;
    for(p=2; p<=n; p=p*2)
    {
        for(i=0; i+p-1<=n; i=i+p)
        {
            low=i;
            high=i+p-1;
            mid = (low+high)/2;
            MergeSort(A,low,mid,high);
        }
    }
    if(p/2<n)
        MergeSort(A,0,p/2-1,n);
}

7.) Recursive Merge Sort :-

void RecursiveMergeSort(int A[], int low, int high)
{
    int mid;
    if(low<high)
    {
        mid = (low+high)/2;
        RecursiveMergeSort(A,low,mid);
        RecursiveMergeSort(A,mid+1,high);
        MergeSort(A,low,mid,high);
    }
}

8.) Count Sort :- Counting sort is a sorting technique that is based on the keys between specific ranges.

int FindMax(int A[], int n)
{
    int max = 65535;
    int i;
    for(i=0; i<n; i++)
    {
        if(A[i] > max)
            max=A[i];
    }
    return max;
}
void CountSort(int A[], int n)
{
    int i,j,max,*C;
    max = FindMax(A,n);
    C = (int *)malloc(sizeof(int)*(max+1));

    for(i=0; i<max+1; i++)
    {
        C[i]=0;
    }
    for(i=0; i<n; i++)
    {
        C[A[i]]++;
    }
    i=0;j=0;
    while (j<max+1) {
        if(C[j] > 0){
            A[i++] = j;
            C[j]--;
        }else
            j++;
    }
}

9.) Bin/Bucket Sort :- Bucket sort is a sorting algorithm that separate the elements into multiple groups said to be buckets. Elements in bucket sort are first uniformly divided into groups called buckets, and then they are sorted by any other sorting algorithm. After that, elements are gathered in a sorted manner.

void BinSort(int A[], int n)
{
    int max = FindMax(A,n);
    int i,j;
    int bucket[max];

    // Bin initilised with NULL
    for(i=0; i<=max; i++)
    {
        bucket[i] = 0;
    }
    for(i=0; i<n; i++)
    {
        bucket[A[i]]++;
    }
    for(i=0,j=0; i<=max; i++)
    {
        while(bucket[i] > 0)
        {
            A[j++] = i;
            bucket[i]--;
        }
    }

}

10.) Shell Sort :- Shell sort has improved the average time complexity of insertion sort. As similar to insertion sort, it is a comparison-based and in-place sorting algorithm. Shell sort is efficient for medium-sized data sets.

void ShellSort(int A[], int n)
{
    int gap, i,j,temp;
    for(gap=n/2; gap>=1; gap /= 2)
    {
        for(i=gap; i<n; i++)
        {
            temp = A[i];
            j = i-gap;
            while(j>=0 && A[j]>temp)
            {
                A[j+gap] = A[j];
                j = j-gap;
            }
            A[j+gap] = temp;
        }
    }
}

Find all the code here