Notification texts go here Contact Us Click here

Merge sorting in Data structure with C Language program .

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated
  1. MERGE SORT :

  2. MERGE Sort IS a popular AND efficient sorting algorithm that follows
  3. the divide-and-conquer approach TO sort an array OR list OF elements.
  4. It divides the INPUT INTO smaller parts, sorts them individually, AND
  5. THEN merges the sorted parts TO obtain the final sorted RESULT.
  6. The TIME complexity OF MERGE Sort IS O(n log n), making it one OF
  7. the fastest sorting algorithms FOR LARGE datasets.
  8.  
  9. Here's a high-level description of the Merge Sort algorithm:
  10.  
  11. 1. *Divide*: The unsorted array is divided into two halves.
  12. This step continues recursively until the sub-arrays contain only
  13. one element, which is inherently sorted.
  14.  
  15. 2. **Conquer (Sort)**: Each sub-array is sorted individually either by
  16. applying Merge Sort again on them or using a different sorting algorithm,
  17. like Insertion Sort or Bubble Sort, for small enough sub-arrays.
  18.  
  19. 3. **Combine (Merge)**: The sorted sub-arrays are merged together to create
  20. larger sorted arrays. This merging process combines the elements from both
  21. sub-arrays in a way that maintains the sorted order.
  22.  
  23. ```Algorithm Of Merge Sort.
  24.  
  25. MergeSort(arr, left, right):
  26. if left < right:
  27. middle = (left + right) / 2
  28. MergeSort(arr, left, middle)
  29. MergeSort(arr, middle + 1, right)
  30. Merge(arr, left, middle, right)
  31.  
  32. Merge(arr, left, middle, right):
  33. n1 = middle - left + 1
  34. n2 = right - middle
  35. leftArr[n1], rightArr[n2]
  36.  
  37. for i = 0 to n1 - 1:
  38. leftArr[i] = arr[left + i]
  39. for j = 0 to n2 - 1:
  40. rightArr[j] = arr[middle + 1 + j]
  41.  
  42. i = 0, j = 0, k = left
  43. while i < n1 and j < n2:
  44. if leftArr[i] <= rightArr[j]:
  45. arr[k] = leftArr[i]
  46. i++
  47. else:
  48. arr[k] = rightArr[j]
  49. j++
  50. k++
  51.  
  52. while i < n1:
  53. arr[k] = leftArr[i]
  54. i++
  55. k++
  56. while j < n2:
  57. arr[k] = rightArr[j]
  58. j++
  59. k++
  60.  
  61.  
  62. Keep in mind that Merge Sort is generally considered a stable sorting algorithm
  63. (maintains the relative order of equal elements) and is suitable for large datasets
  64. due to its efficient time complexity.
  65.  
  66. Sure! Let's walk through an example OF MERGE Sort step BY step TO understand how it works.
  67. We'll use the following unsorted array:
  68.  
  69. Sure! Let's sort the given unsorted array [7, 3, 2, 16, 24, 4, 11, 9]
  70. USING the MERGE Sort algorithm.
  71.  
  72. Step 1: Divide
  73. --------------
  74. 1. Divide the array INTO two halves:
  75. - LEFT half: [7, 3, 2, 16]
  76. - RIGHT half: [24, 4, 11, 9]
  77.  
  78. 2. Divide the LEFT half INTO two more halves:
  79. - LEFT half: [7, 3]
  80. - RIGHT half: [2, 16]
  81.  
  82. 3. Continue dividing the sub-arrays until each one contains ONLY one element.
  83.  
  84. Step 2: Sort (Conquer)
  85. -------------------------
  86. 1. Sort the leftmost sub-arrays:
  87. - [7, 3] (sort USING MERGE sort TO GET [3, 7])
  88. - [2, 16] (sort USING MERGE sort TO GET [2, 16])
  89.  
  90. 2. Sort the rightmost sub-arrays:
  91. - [24, 4] (sort USING MERGE sort TO GET [4, 24])
  92. - [11, 9] (sort USING MERGE sort TO GET [9, 11])
  93.  
  94. Step 3: MERGE (Combine)
  95. --------------------------
  96. 1. MERGE the two leftmost sub-arrays [3, 7] AND [2, 16]:
  97. - Compare 3 AND 2, pick 2, move TO the NEXT element IN the RIGHT array.
  98. - Compare 3 AND 16, pick 3, move TO the NEXT element IN the LEFT array.
  99. - Since the LEFT array IS exhausted, append the remaining elements FROM
  100. - the RIGHT array ([16]).
  101.  
  102. RESULT: [2, 3, 7, 16]
  103.  
  104. 2. MERGE the two rightmost sub-arrays [4, 24] AND [9, 11]:
  105. - Compare 4 AND 9, pick 4, move TO the NEXT element IN the LEFT array.
  106. - Compare 9 AND 24, pick 9, move TO the NEXT element IN the RIGHT array.
  107. - Since the RIGHT array IS exhausted, append the remaining elements FROM
  108. - the LEFT array ([11]).
  109.  
  110. RESULT: [4, 9, 11, 24]
  111.  
  112. 3. Finally, MERGE the two merged sub-arrays [2, 3, 7, 16] AND [4, 9, 11, 24]:
  113. - Compare 2 AND 4, pick 2, move TO the NEXT element IN the LEFT array.
  114. - Compare 3 AND 4, pick 3, move TO the NEXT element IN the LEFT array.
  115. - Compare 4 AND 7, pick 4, move TO the NEXT element IN the LEFT array.
  116. - Compare 7 AND 9, pick 7, move TO the NEXT element IN the LEFT array.
  117. - Compare 9 AND 11, pick 9, move TO the NEXT element IN the LEFT array.
  118. - Compare 11 AND 16, pick 11, move TO the NEXT element IN the LEFT array.
  119. - Compare 16 AND 24, pick 16, move TO the NEXT element IN the LEFT array.
  120. - Since the LEFT array IS exhausted, append the remaining elements FROM
  121. - the RIGHT array ([24]).
  122.  
  123. RESULT: [2, 3, 4, 7, 9, 11, 16, 24]
  124.  
  125. The final sorted array IS [2, 3, 4, 7, 9, 11, 16, 24], AND the MERGE Sort algorithm IS
  126. complete.
  127. AS you can see, MERGE Sort divides the problem INTO smaller parts, sorts them individually,
  128. AND THEN merges them TO obtain the final sorted array.
  1. Program of Merge sort Using C language.


  2. #include <stdio.h>
  3.  
  4. // Merge two sorted subarrays into one sorted array
  5. void merge(int arr[], int left, int middle, int right) {
  6. int left_size = middle - left + 1;
  7. int right_size = right - middle;
  8.  
  9. int left_array[left_size];
  10. int right_array[right_size];
  11.  
  12. // Copy data to temporary arrays
  13. for (int i = 0; i < left_size; i++)
  14. left_array[i] = arr[left + i];
  15. for (int j = 0; j < right_size; j++)
  16. right_array[j] = arr[middle + 1 + j];
  17.  
  18. // Merge the two temporary arrays back into arr
  19. int i = 0, j = 0, k = left;
  20. while (i < left_size && j < right_size) {
  21. if (left_array[i] <= right_array[j]) {
  22. arr[k] = left_array[i];
  23. i++;
  24. } else {
  25. arr[k] = right_array[j];
  26. j++;
  27. }
  28. k++;
  29. }
  30.  
  31. // Copy any remaining elements of left_array and right_array
  32. while (i < left_size) {
  33. arr[k] = left_array[i];
  34. i++;
  35. k++;
  36. }
  37. while (j < right_size) {
  38. arr[k] = right_array[j];
  39. j++;
  40. k++;
  41. }
  42. }
  43.  
  44. // Merge Sort function
  45. void merge_sort(int arr[], int left, int right) {
  46. if (left < right) {
  47. int middle = left + (right - left) / 2;
  48.  
  49. // Sort the first and second halves separately
  50. merge_sort(arr, left, middle);
  51. merge_sort(arr, middle + 1, right);
  52.  
  53. // Merge the sorted halves
  54. merge(arr, left, middle, right);
  55. }
  56. }
  57.  
  58. // Function to print an array
  59. void print_array(int arr[], int size) {
  60. for (int i = 0; i < size; i++)
  61. printf("%d ", arr[i]);
  62. printf("\n");
  63. }
  64.  
  65. int main() {
  66. int arr[] = {38, 27, 43, 3, 9, 82, 10};
  67. int size = sizeof(arr) / sizeof(arr[0]);
  68.  
  69. printf("Original array: ");
  70. print_array(arr, size);
  71.  
  72. merge_sort(arr, 0, size - 1);
  73.  
  74. printf("Sorted array: ");
  75. print_array(arr, size);
  76.  
  77. return 0;
  78. }
  79.  
  80. OUTPUT :
  81.  
  82. Original array: 38 27 43 3 9 82 10
  83. Sorted array: 3 9 10 27 38 43 82
  84.  

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.