
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain the merge sort technique in C language
Sorting is the process of arranging the elements either in ascending (or) descending order.
Types of sorting
C language provides five sorting techniques, which are as follows −
- Bubble sort (or) Exchange Sort
- Selection sort
- Insertion sort(or) Linear sort
- Quick sort (or) Partition exchange sort
- Merge Sort (or) External sort
Merge sort
Merge sort is a divided and conquer method. It divides an array in two half’s, conquer recursively and merge (combine).
Let’s consider an example which is given below −
Take an unsorted array and apply merge sort technique to sort the array.
38, 27, 43, 3, 9, 82, 10
Now, combine an array by sorting as shown below −
Example
Following is the C program to sort the elements by using the merge sort technique −
#include <stdio.h> #define max 10 int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 }; int b[10]; void merging(int low, int mid, int high) { int l1, l2, i; for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if(a[l1] <= a[l2]) b[i] = a[l1++]; else b[i] = a[l2++]; } while(l1 <= mid) b[i++] = a[l1++]; while(l2 <= high) b[i++] = a[l2++]; for(i = low; i <= high; i++) a[i] = b[i]; } void sort(int low, int high) { int mid; if(low < high) { mid = (low + high) / 2; sort(low, mid); sort(mid+1, high); merging(low, mid, high); } else { return; } } int main() { int i; printf("List before sorting
"); for(i = 0; i <= max; i++) printf("%d ", a[i]); sort(0, max); printf("
List after sorting
"); for(i = 0; i <= max; i++) printf("%d ", a[i]); }
Output
When the above program is executed, it produces the following output −
List before sorting 10 14 19 26 27 31 33 35 42 44 0 List after sorting 0 10 14 19 26 27 31 33 35 42 44
- Related Articles
- Explain the quick sort technique in C language.
- Explain Merge Sort in Python
- Explain the procedure of selection sort in C language
- Explain the insertion sort by using C language.
- Merge Sort Tree in C++
- 3-way Merge Sort in C++
- Merge Sort using Multithreading in C++
- Merge Sort
- How to merge to arrays in C language?
- C++ Program to Implement Merge Sort
- C Program for Iterative Merge Sort
- Merge sort vs quick sort in Javascript
- C program to sort an array by using merge sort
- How to perform Merge Sort using C#?
- Merge Sort for Linked Lists using C++.

Advertisements