Notification texts go here Contact Us Click here

Merge sorting in Data structure with Python Language program .

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


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

  42. OUTPUT :
  43. [3, 9, 10, 27, 38, 43, 82]
  44.  

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.