× back Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort
Next Topic → ← Previous Topic

Sorting

Bubble sort

Procedure ↓

Pseudo code ↓

                
void bubbleSort(int A[], int n)
{
    for(i = 0; i < n - 1; i++)
    {
        for(j = 0; j < n - 1 - i; j++)
        {
            if(A[j]> A[j+1])
                swap(A[j], A[j+1]);
        }
    }
}
                
            
                
void bubbleSort(int A[], int n)
{
    int flag;
    for(i = 0; i < n - 1; i++)
    {
        flag = 0;
        for(j = 0; j < n - 1 - i; j++)
        {
            if(A[j]> A[j+1])
            {
                swap(A[j], A[j+1]);
                flag = 1;
            }
        }
        if(flag == 0) break;
    }
}
                
            

Program code ↓

                   
#include <stdio.h>
void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void bubbleSort(int A[], int n)
{
    int i, j, flag;
    for (i = 0; i < n - 1; i++)
    {
        flag = 0;
        for (j = 0; j < n - 1 - i; j++)
        {
            if (A[j] > A[j + 1])
            {
                swap(&A[j], &A[j + 1]);
                flag = 1;
            }
        }
        if (flag == 0)
            break;
    }
}
int main()
{
    int A[] = {3, 7, 9, 10, 6, 5, 12, 4, 11, 2}, n = 10, i;
    bubbleSort(A, n);
    printf("The sorted array : ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", A[i]);
    }
    printf("\n");
    return 0;
}
                   
               

Selection sort

Procedure ↓

Algorithm / Psuedo code ↓

                
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]);
    }
}
                
            

Program code ↓

                   
#include<stdio.h>
void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
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]);
    }
}
int main()
{
    int A[] = {11, 12, 7, 2, 6, 9, 4, 5, 10, 3}, n = 10, i;
    selectionSort(A, n);
    printf("\nDisplay the sorted array : ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", A[i]);
    }
    return 0;
}
                   
               

Insertion Sort

Procedure ↓

Algorithm / Psuedo code ↓

                
void insertionSort(int A[], int n)
{
    for(i = 1; i < n; i++)
    {
        j = i - 1;
        x = A[i];
        while(j > -1 && A[j]>x)
        {
            A[j+1] = A[j];
            j--;
        }
        A[j+1] = x;
    }
}
                
            

Program code ↓

                   
#include <stdio.h>
void insertionSort(int A[], int n)
{
    int i, j, x;
    for (i = 1; i < n; i++)
    {
        j = i - 1;
        x = A[i];
        while (j > -1 && A[j] > x)
        {
            A[j + 1] = A[j];
            j--;
        }
        A[j + 1] = x;
    }
}
int main()
{
    int A[] = {11, 12, 7, 2, 6, 9, 4, 5, 10, 3}, n = 10, i;
    insertionSort(A, n);
    printf("\nDisplay the sorted array : ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", A[i]);
    }
    return 0;
}
                   
               

Quick Sort

Idea behind quick sort ↓

Procedure ↓

The above procedure is known as partitioning procedure in which pivot element is moved to sorted place.
So quick sort uses partitioning procedure for sorting the elements and also it is recursive. So in code we will write the partitioned procedure and then quick sort procedure.

Partitioning algorithm ↓

                
int partition(int A[], int l, int h)
{
    int pivot = A[l];
    int i = l, j = h;
    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[l], A[j]);
    return j;
}
                
            

Best case → If partitioning is done in middle. O(nlogn)
Worst case → If partitioning is on any end. O(n2) (already sorted)

Program code ↓

                   
#include <stdio.h>
void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
int partition(int A[], int l, int h)
{
    int pivot = A[l];
    int i = l, j = h;
    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[l], &A[j]);
    return j;
}
void quickSort(int A[], int l, int h)
{
    int j;
    if (l < h)
    {
        j = partition(A, l, h);
        quickSort(A, l, j);
        quickSort(A, j + 1, h);
    }
}
int main()
{
    int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, n = 10, i;
    quickSort(A, 0, n);
    printf("\nDisplay the sorted array : ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", A[i]);
    }
    return 0;
}
                   
               

Merge Sort

Merging two array in a single array

  • Merging is a process of combining two sorted list into a single sorted list
  • In the last iteration there will be some elements that will be remaining to be added to C array and definitly this is going to happen if m and n are not equal.

Algorithm ↓

                    
void merge(int A[], int B[], int m, int n)
{
    int i, j, k;
    i = k = 0;
    while(i < m && j < n)
    {
        if(A[i] < B[j])
            C[k++] = A[i++];
        else 
            C[k++] = B[j++];
    }
    // now this is for if there are some remaining elements
    for(; i < m; i++)
        C[k++] = A[i];
    for(; j < n; j++)
        C[k++] = B[j];
    // either of 'for' loop will execute
}
                    
                

Merging two list which is in single array

  • Now we have to update the previously written algorithm for single array.
                    
void merge(int A[], int l, int mid, int h)
{
    int i, j, k;
    i = l;
    j = mid + 1;
    k = l;
    // we need array to merge into 
    int B[h + 1];
    while(i < mid && j <= h)
    {
        if(A[i] < A[j])
            B[k++] = A[i++];
        else 
            B[k++] = A[j++];
    }

    for(; i <= mid; i++)
        B[k++] = A[i];
    for(; j <= h; j++)
        B[k++] = A[j];
    
    // now all the elements should be copied back to array A
    for(i = l; i <= h; i++) A[i] = B[i];
}
                    
                
  • Now this modified code works for single array now.
  • In merge sort we will utilize this technique.

Two way merge sort (Iterative version)

merge sort function for iterative version ↓

                
void iMergeSort(int A[], int n)
{
    int p, i, l, mid, h;
    for(p = 2; p <= n; p = p * 2)
    {
        for(i = 0; i + p - 1 < n; i = i + p)
        {
            l = i;
            h = i + p - 1;
            mid = (l + h) / 2;
            merge(A, l, mid, h);
        }
    }
    if(p/2 < n)
        merge(A, 0, p/2, n-1);
}
                
            
                   
#include <stdio.h>
void merge(int A[], int l, int mid, int h)
{
    int i = l, j = mid + 1, k = l;
    int B[h+1];
    while(i <= mid && j <= h)
    {
        if(A[i] < A[j])
            B[k++] = A[i++];
        else 
            B[k++] = A[j++];
    }
    for(; i <= mid; i++)
        B[k++] = A[i];
    for(; j <=h; j++)
        B[k++] = A[j];
    
    // copying element from B to A 
    for(i = l; i <= h; i++) A[i] = B[i];
}
void iMergeSort(int A[], int n)
{
    int p, l, h, mid, i;
    for(p = 2; p <= n; p = p * 2)
    {
        for(i = 0; i + p - 1 < n; i = i + p)
        {
            l = i;
            h = i + p - 1;
            mid = (l + h) / 2;
            merge(A, l, mid, h);
        }
    }
    if(p/2 < n)
    {
        merge(A, 0, p/2 - 1, n);
    }
}
int main()
{
    int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, n = 10, i;
    iMergeSort(A, n);
    printf("\nDisplay the sorted array : ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", A[i]);
    }
    return 0;
}
                   
               

Two way merge sort (recursive version)

                       
#include <stdio.h>
void merge(int A[], int l, int mid, int h)
{
    int i = l, j = mid + 1, k = l;
    int B[h+1];
    while(i <= mid && j <= h)
    {
        if(A[i] < A[j])
            B[k++] = A[i++];
        else 
            B[k++] = A[j++];
    }
    for(; i <= mid; i++)
        B[k++] = A[i];
    for(; j <=h; j++)
        B[k++] = A[j];
    
    for(i = l; i <= h; i++) A[i] = B[i];
}                
void mergeSort(int A[], int l, int h)
{
    int mid;
    if(l < h)
    {
        mid = ( l + h ) / 2;
        mergeSort(A, l, mid);
        mergeSort(A, mid + 1, h);
        merge(A, l, mid, h);
    }
}
int main()
{
    int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, n = 10, i;
    mergeSort(A, 0, n - 1);
    printf("\nDisplay the sorted array : ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", A[i]);
    }
    return 0;
}