0% found this document useful (0 votes)
4 views6 pages

Merge Sort: Merge Sort Divides The Array Into Two Halves, Sorts Them Recursively, and Then Merges Them

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

Merge Sort: Merge Sort Divides The Array Into Two Halves, Sorts Them Recursively, and Then Merges Them

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

1.

Merge Sort

Merge Sort divides the array into two halves, sorts them recursively, and
then merges them.
MergeSort(arr, left, right)

if left >= right

return

mid = (left + right) // 2

MergeSort(arr, left, mid)

MergeSort(arr, mid + 1, right)

Merge(arr, left, mid, right)

Merge(arr, left, mid, right)

create temp array for merged result

i = left, j = mid + 1, k = 0

while i <= mid and j <= right

if arr[i] <= arr[j]

temp[k++] = arr[i++]

else
temp[k++] = arr[j++]

while i <= mid

temp[k++] = arr[i++]

while j <= right

temp[k++] = arr[j++]

copy temp array back to arr[left:right+1]

2. Quick Sort

Quick Sort uses a pivot to partition the array into two halves and recursively
sorts them.
QuickSort(arr, low, high)

if low < high

pivotIndex = Partition(arr, low, high)

QuickSort(arr, low, pivotIndex - 1)

QuickSort(arr, pivotIndex + 1, high)

Partition(arr, low, high)

pivot = arr[high]

i = low - 1

for j = low to high - 1

if arr[j] <= pivot

i=i+1

swap arr[i] and arr[j]

swap arr[i + 1] and arr[high]

return i + 1

3. Maximum Sum Subarray (Divide and Conquer)


This algorithm finds the maximum sum subarray using the divide-and-conquer
approach.

MaxSumSubarray(arr, left, right)

if left == right
return arr[left]

mid = (left + right) // 2

leftSum = MaxSumSubarray(arr, left, mid)

rightSum = MaxSumSubarray(arr, mid + 1, right)

crossSum = MaxCrossingSum(arr, left, mid, right)

return max(leftSum, rightSum, crossSum)

MaxCrossingSum(arr, left, mid, right)

leftMax = -∞, sum = 0

for i = mid downto left

sum = sum + arr[i]

if sum > leftMax

leftMax = sum

rightMax = -∞, sum = 0

for i = mid + 1 to right

sum = sum + arr[i]

if sum > rightMax

rightMax = sum

return leftMax + rightMax

Tower of Hanoi Problem


The Tower of Hanoi problem involves moving n discs from a source peg to a
destination peg, using an auxiliary peg, following these rules:

1. Only one disc can be moved at a time.

2. A disc cannot be placed on top of a smaller disc.

Recursive Approach
The solution involves the following recursive steps:

1. Move the top n−1 discs from the source peg to the auxiliary peg.

2. Move the largest disc (the nth disc) directly to the destination peg.

3. Move the n−1 discs from the auxiliary peg to the destination peg.
Base Case
If there is only one disc (n=1):

 Move the disc directly from the source peg to the destination peg.

TowerOfHanoi(n, source, destination, auxiliary)

if n == 1

print "Move disc 1 from", source, "to", destination

return

TowerOfHanoi(n-1, source, auxiliary, destination)

print "Move disc", n, "from", source, "to", destination

TowerOfHanoi(n-1, auxiliary, destination, source)

Karatsuba Multiplication Problem


Multiply two large integers using the Karatsuba algorithm, which is a Divide
and Conquer method to reduce the time complexity of standard multiplication.
KaratsubaMultiply(X, Y)

if X < 10 or Y < 10

return X * Y

n = max(number of digits in X, number of digits in Y)

m=n/2

a = X / 10^m

b = X % 10^m

c = Y / 10^m
d = Y % 10^m

ac = KaratsubaMultiply(a, c)

bd = KaratsubaMultiply(b, d)

ab_cd = KaratsubaMultiply(a + b, c + d)

ad_bc = ab_cd - ac - bd

return ac * 10^(2 * m) + ad_bc * 10^m + bd

You might also like