0% found this document useful (0 votes)
8 views36 pages

2__Sorting Algoritham

The document discusses sorting algorithms, focusing on Insertion Sort and Merge Sort. Insertion Sort is simple and efficient for small datasets but has a worst-case time complexity of O(n²), while Merge Sort uses a divide-and-conquer approach with a consistent time complexity of O(n log n) across all cases. Each algorithm's advantages and disadvantages are outlined, highlighting their performance characteristics and memory requirements.

Uploaded by

imrank39199
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)
8 views36 pages

2__Sorting Algoritham

The document discusses sorting algorithms, focusing on Insertion Sort and Merge Sort. Insertion Sort is simple and efficient for small datasets but has a worst-case time complexity of O(n²), while Merge Sort uses a divide-and-conquer approach with a consistent time complexity of O(n log n) across all cases. Each algorithm's advantages and disadvantages are outlined, highlighting their performance characteristics and memory requirements.

Uploaded by

imrank39199
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/ 36

CHAPTER#2 SORTING AND SEARCHING

ALGORITHM
Sorting Algorithm

A sorting algorithm is used to arrange elements of an array/list in a specific order.

1. Insertion sort algorithm


(incremental approach)
Insertion sort is a very simple method to sort numbers in an ascending or descending order.
This method follows the incremental method. It can be compared with the technique how cards
are sorted at the time of playing a game. The array is searched sequentially and unsorted items
are moved and inserted into the sorted sub-list (in the same array).

Insertion Sort Algorithm


Step 1 − If it is the first element, it is already sorted. return 1;

Step 2 − Pick next element

Step 3 − Compare with all elements in the sorted sub-list

Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted

Step 5 − Insert the value

Step 6 − Repeat until list is sorted

Design and analysis of


Algorithm 1
Pseudo code

Example:

Design and analysis of


Algorithm 2
Key is the extra value space reserved in memory for A[j], where we place our key value and free
the space for swapping.

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.

Design and analysis of


Algorithm 3
Random-access machine (RAM) model

 Instructions are executed one after another. No concurrent operations.


 It’s too tedious to define each of the instructions and their associated time costs.
 Instead, we recognize that well use instructions commonly found in real computers:
 Arithmetic: add, subtract, multiply, divide, remainder, floor, ceiling). Also, shift
left/shift right (good for multiplying/dividing by 2 k).
 Data movement: load, store, copy.
 Control: conditional/unconditional branch, subroutine call and return.

 Each of these instructions takes a constant amount of time. The RAM model uses integer
and floating-point types.

How do we analyze an algorithms running time?

The time taken by an algorithm depends on the input.

 Sorting 1000 numbers takes longer than sorting 3 numbers.


 A given sorting algorithm may even take differing amounts of time on two inputs of the
same size.
 For example, we will see that insertion sort takes less time to sort elements when they are
already sorted than when they are in reverse sorted order.

Complexities/ analysis of insertion sort:

Time Complexity of Insertion Sort

 The worst-case time complexity of the Insertion sort is O(n2)

 The average case time complexity of the Insertion sort is O(n2)

 The time complexity of the best case is O(N).

Design and analysis of


Algorithm 4
Example for worst case:

9 8 7 6 5 4 3 2

Space Complexity of Insertion Sort

 The auxiliary space complexity of Insertion Sort is O(1)

Analysis of Insertion sort (cormen series)

Design and analysis of


Algorithm 5
Design and analysis of
Algorithm 6
Design and analysis of
Algorithm 7
Design and analysis of
Algorithm 8
Design and analysis of
Algorithm 9
Design and analysis of
Algorithm 10
Design and analysis of
Algorithm 11
Application/ advantages:
 It is simple to implement.

 It is efficient on small datasets.

 It is stable (does not change the relative order of elements with equal keys)

 It is in-place (only requires a constant amount O (1) of extra memory space).

 It is an online algorithm, which can sort a list when it is received.

Disadvantages:
 insertion sort is inefficient against more extensive data sets

 The insertion sort exhibits the worst-case time complexity of O(n2)

2. Merge sort algorithm (Divide and


conquer approach)

Divide and conquer approach

A divide-and-conquer algorithm has three parts:

 Divide a large problem into smaller sub problems.

 Conquer the sub problems by recursively solving them.

 To find the solutions to the original problem, combine the solutions of the sub
problems.

Design and analysis of


Algorithm 12
Merge sort algorithm:

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.

Design and analysis of


Algorithm 13
Merge sort algorithm can be executed in two ways:

 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.

Design and analysis of


Algorithm 14
Merge sort Algorithm:
Step 1 − if it is only one element in the list it is already sorted, return.

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

Design and analysis of


Algorithm 15
Now,

After dividing the following merge sort pseudo code used for merging the sub problems.

Pseudo code

Example

Design and analysis of


Algorithm 16
Design and analysis of
Algorithm 17
Design and analysis of
Algorithm 18
Design and analysis of
Algorithm 19
Design and analysis of
Algorithm 20
Design and analysis of
Algorithm 21
Design and analysis of
Algorithm 22
Design and analysis of
Algorithm 23
Design and analysis of
Algorithm 24
Design and analysis of
Algorithm 25
Design and analysis of
Algorithm 26
Complexities of merge sort
Time complexity

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.

 Best Time Complexity: O(nlogn)

 Average Time Complexity: O(nlogn)

 Worst Time Complexity: O(nlogn)

Space complexity

The space complexity of merge sort is O(n). It is because, in merge sort, an extra variable
is required for swapping.

Design and analysis of


Algorithm 27
Design and analysis of
Algorithm 28
Design and analysis of
Algorithm 29
Design and analysis of
Algorithm 30
Design and analysis of
Algorithm 31
Design and analysis of
Algorithm 32
Design and analysis of
Algorithm 33
Design and analysis of
Algorithm 34
Design and analysis of
Algorithm 35
Advantages
 Merge sort is a stable sorting algorithm, which means it maintains the relative order of
equal elements in the input array.

 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.

Design and analysis of


Algorithm 36

You might also like