2__Sorting Algoritham
2__Sorting Algoritham
ALGORITHM
Sorting Algorithm
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Example:
Correctness
We often use a loop invariant to help us understand why an algorithm gives the correct answer.
To use a loop invariant to prove correctness, we must show three things about it: initialization,
maintenance and termination.
Analyzing algorithms
We want to predict there sources that the algorithm requires. Usually, running time. In order to
predict resource requirements, we need a computational model.
Each of these instructions takes a constant amount of time. The RAM model uses integer
and floating-point types.
9 8 7 6 5 4 3 2
It is stable (does not change the relative order of elements with equal keys)
Disadvantages:
insertion sort is inefficient against more extensive data sets
To find the solutions to the original problem, combine the solutions of the sub
problems.
Merge sort is one of the most efficient sorting algorithms. It is based on the divide-and-
conquer strategy. Merge sort continuously cuts down a list into multiple sub lists until each
has only one item, and then merges those sub lists into a sorted list.
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.
Bottom-Up Approach
The iterative technique is used in the Bottom-Up merge sort approach. It starts with
a "single-element" array and then merges two nearby items while also sorting them.
The combined-sorted arrays are merged and sorted again until only one single unit
of the sorted array remains.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
We are dealing with sub problems, we state each sub problem as sorting a sub array A [p ..r].
initially, p = 1 and r = n, but these values change as we recurse through sub problems. To sort
A[p ..r]:
Divide by splitting into two sub arrays A[p ..q] And A[q +1..r], where q is the halfway point of
A[p ..r].
Conquer by recursively sorting the two sub arrays A[p ..q] and A[q +1..r].
Combine by merging the two sorted sub arrays A[p ..q] and A[q+1..r] to produce a single sorted
sub array A[p ..r]. To accomplish this step, well define a procedure MERGE (A, p,q,r).
The following pseudo code used for divide array into sub array/ sub problems
After dividing the following merge sort pseudo code used for merging the sub problems.
Pseudo code
Example
The time complexity of Merge Sort is O(Nlog(N)) in all 3 cases (worst, average, and best) as
merge sort always divides the array into two halves and takes linear time to merge two halves.
Space complexity
The space complexity of merge sort is O(n). It is because, in merge sort, an extra variable
is required for swapping.
Merge sort has a worst-case time complexity of O(NlogN), which means it performs well
even on large datasets.
Disadvantages
For small datasets, merge sort is slower than other sorting algorithms.
Even if the array is sorted, the merge sort goes through the entire process.
Merge sort is not an in-place sorting algorithm, which means it requires additional
memory to store the sorted data.