0% found this document useful (0 votes)
36 views

Merge Sort

Merge sort is a sorting algorithm that works by dividing the input array into two halves, recursively sorting each half, and then merging the sorted halves back together. It has the following steps: 1) Divide the array into two halves of roughly equal size. 2) Recursively sort the two halves. 3) Merge the sorted halves back into a single sorted array by comparing elements and selecting the smaller/larger to place in the final array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Merge Sort

Merge sort is a sorting algorithm that works by dividing the input array into two halves, recursively sorting each half, and then merging the sorted halves back together. It has the following steps: 1) Divide the array into two halves of roughly equal size. 2) Recursively sort the two halves. 3) Merge the sorted halves back into a single sorted array by comparing elements and selecting the smaller/larger to place in the final array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MERGE SORT https://www.youtube.com/watch?

v=mB5HXBb_HY8
Merge sort is defined as a sorting algorithm that works by dividing an array
into smaller subarrays, sorting each subarray, and then merging the sorted
subarrays back together to form the final sorted array.

In simple terms, we can say that the process of merge sort is to divide the
array into two halves, sort each half, and then merge the sorted halves back
together. This process is repeated until the entire array is sorted.

Merge sort is a sorting algorithm that works by dividing the input array into two

halves, sorting each half recursively, and then merging the sorted halves.

● Divide: The algorithm starts by dividing the array into two halves, roughly

equal in size. If the array has only one element or is empty, it is already

considered sorted.

● Conquer: The algorithm recursively sorts the two halves of the array. This

process continues until each subarray contains only one element, which is

considered sorted.

● Merge: The sorted subarrays are then merged back together to create a single

sorted array. This is done by comparing the elements of the two subarrays

and selecting the smaller (or larger) element to place in the final sorted array.

The process continues until all elements are merged.


CODE:
def merge_sort(arr):
if len(arr) <= 1:
return arr

mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]

return merge(merge_sort(left_half), merge_sort(right_half))

def merge(left, right):


merged = []
left_index = 0
right_index = 0

while left_index < len(left) and right_index < len(right):


if left[left_index] <= right[right_index]:
merged.append(left[left_index])
left_index += 1
else:
merged.append(right[right_index])
right_index += 1

merged += left[left_index:]
merged += right[right_index:]

return merged

The space complexity of this implementation of merge sort is O(n), where n is


the number of elements in the input array. This is because merge sort requires
additional space to store the sorted halves of the input array during the sorting
process.
BINARY SEARCH

BY DIVIDE AND CONQURE:


CODE:
def binary_search(sorted_list, left_pointer, right_pointer, target):
if left_pointer >= right_pointer:
return -1

mid_index = (left_pointer + right_pointer) // 2


mid_val = sorted_list[mid_index]

if mid_val == target:
return mid_index
elif mid_val > target:
return binary_search(sorted_list, left_pointer, mid_index, target)
else:
return binary_search(sorted_list, mid_index + 1, right_pointer, target)
MATRIX MULTIPLICATION

TIME COMPLEXCITY
For a 2D array, the space complexity is O(n*n).
QUICK SORT
https://www.youtube.com/watch?v=7h1s2SojIRw
3n , n factorial, n^2, log(n), root n, n log(n), 2^n, n^3, n^2 log(n) sort
them in increasing order

Let's reorder them:

​ log(n) (logarithmic)
​ root n (√n) (square root)
​ n (linear)
​ n log(n) (linearithmic)
​ n^2 log(n) (n squared logarithmic)
​ n^2 (quadratic)
​ n^3 (cubic)
​ 2^n (exponential)
​ n! (factorial)

This should be the correct order from smallest to largest


growth rate.

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