Merge Sort: Merge Sort Divides The Array Into Two Halves, Sorts Them Recursively, and Then Merges Them
Merge Sort: Merge Sort Divides The Array Into Two Halves, Sorts Them Recursively, and Then Merges Them
Merge Sort
Merge Sort divides the array into two halves, sorts them recursively, and
then merges them.
MergeSort(arr, left, right)
return
i = left, j = mid + 1, k = 0
temp[k++] = arr[i++]
else
temp[k++] = arr[j++]
temp[k++] = arr[i++]
temp[k++] = arr[j++]
2. Quick Sort
Quick Sort uses a pivot to partition the array into two halves and recursively
sorts them.
QuickSort(arr, low, high)
pivot = arr[high]
i = low - 1
i=i+1
return i + 1
if left == right
return arr[left]
leftMax = sum
rightMax = sum
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.
if n == 1
return
if X < 10 or Y < 10
return X * 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