0% found this document useful (0 votes)
21 views5 pages

Merge Sort Group-5

Merge sort is a popular sorting algorithm that follows the divide-and-conquer approach. It works by dividing the input array into halves, sorting each half separately using recursive calls, and then merging the sorted halves back into a single sorted array. The algorithm is efficient for large datasets due to its divide-and-conquer approach. Pseudocode and a Python implementation are provided to demonstrate how merge sort works by recursively dividing and merging sub-arrays until the entire array is sorted.

Uploaded by

Salma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Merge Sort Group-5

Merge sort is a popular sorting algorithm that follows the divide-and-conquer approach. It works by dividing the input array into halves, sorting each half separately using recursive calls, and then merging the sorted halves back into a single sorted array. The algorithm is efficient for large datasets due to its divide-and-conquer approach. Pseudocode and a Python implementation are provided to demonstrate how merge sort works by recursively dividing and merging sub-arrays until the entire array is sorted.

Uploaded by

Salma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Group-5

MERGE SORT
Introduction
• Merge sort is a popular sorting algorithm that follows the divide-
and-conquer approach. The algorithm works by dividing the input
array into two halves, sorting each half separately, and then
merging the two sorted halves back into a single sorted array.And is
known for its stability and efficiency on large datasets. It is
commonly used in various applications, including sorting massive
amounts of data, parallel computing, and external sorting. In this
algorithm, the main operations are merging and splitting the array,
making it easy to implement and understand.
MERGE SORT
ALGORITHM

Top-down Approach
It starts at the top and works its
way down, splitting the array in
half, making a recursive call, and
merging the results until it reaches
the bottom of the array tree.
Merge Sort How Code Works

• Pseudocode for MergeSort • Implement the Merge Sort In Python

As we know, merge sort works on the divide and


def merge_sort(arr):
conquer approach. It repeatedly divides the array into if len(arr) > 1:
smaller parts until it is left with a single element. Now let mid = len(arr) // 2
L = arr[:mid]
us see the pseudocode of merge sort. R = arr[mid:]
merge_sort(L)
• Step 1: Declare the variable low and high to mark merge_sort(R)
the start and end of the array. i=j=k=0
while i < len(L) and j < len(R):
• Step 2: Low will equal 0, and high will equal array if L[i] < R[j]:
arr[k] = L[i]
size -1. i += 1
else:
• Step 3: Calculate mid using low + high / 2. arr[k] = R[j]
j += 1
• Step 4: Call the mergeSort function on the part (low, k += 1
while i < len(L):
mid) and (mid+1, high). arr[k] = L[i]
i += 1
• Step 5: This calling will continue till low<high is k += 1
satisfied. while j < len(R):
arr[k] = R[j]
j += 1
• Step 6: Finally, call the merge function to merge k += 1
these two halves. arr = [12, 11, 13, 5, 6, 7]
merge_sort(arr)
print(arr)
Summary
• Merge Sort is a sorting algorithm that uses the divide-and-conquer
approach to sort an array. It is efficient for large data sets. Merge
Sort first divides the input array into smaller sub-arrays until each
sub-array contains only one element. Overall, Merge Sort is a
reliable and efficient sorting algorithm that is widely used in
many applications.
• Disadvantages of Merge Sort
• Merge sort is not efficient for sorting input of large size if you are having low stack
space.
• Merge sort while sorting the array goes through the entire process even if the array is
sorted.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy