0% found this document useful (0 votes)
99 views5 pages

Insertion Sort: What Are The Boundary Cases of Insertion Sort Algorithm?

Insertion sort, selection sort, bubble sort, merge sort, quicksort, heap sort, and shell sort are sorting algorithms. Insertion sort is efficient for small data sets and adaptive for partially sorted data. Selection sort uses a greedy approach and makes the minimum number of swaps. Bubble sort is simple but inefficient for large data sets. Merge sort uses divide and conquer and is not in-place. Quicksort and heap sort have average case performance of O(n log n). Shell sort is a variation of insertion sort for medium to large data sets.

Uploaded by

Masood Mughal
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)
99 views5 pages

Insertion Sort: What Are The Boundary Cases of Insertion Sort Algorithm?

Insertion sort, selection sort, bubble sort, merge sort, quicksort, heap sort, and shell sort are sorting algorithms. Insertion sort is efficient for small data sets and adaptive for partially sorted data. Selection sort uses a greedy approach and makes the minimum number of swaps. Bubble sort is simple but inefficient for large data sets. Merge sort uses divide and conquer and is not in-place. Quicksort and heap sort have average case performance of O(n log n). Shell sort is a variation of insertion sort for medium to large data sets.

Uploaded by

Masood Mughal
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/ 5

INSERTION SORT

• Insertion sort is efficient for small data values.


• Insertion sort is adaptive in nature, i.e., it is appropriate for data sets which are
already partially sorted.
• COMPARISON
• What are the Boundary Cases of Insertion Sort algorithm?
Insertion sort takes maximum time to sort if elements are sorted in reverse order.
And it takes minimum time (Order of n) when elements are already sorted.
• What are the Algorithmic Paradigm of Insertion Sort algorithm?
Insertion Sort algorithm follows incremental approach.
• Is Insertion Sort an in-place sorting algorithm?
Yes, insertion sort is an in-place sorting algorithm.
• Is Insertion Sort a stable algorithm?
Yes, insertion sort is a stable sorting algorithm.
• When is the Insertion Sort algorithm used?
• Insertion sort is used when number of elements is small. It can also be useful
when input array is almost sorted, only few elements are misplaced in complete
big array.
• Insertion sort is efficient for small data values
• T(N) = T(N-1) + N RECCURENCE RELATION

SELECTION SORT

• The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from the unsorted part and putting it at
the beginning
• GREEDY METHOD
• two nested loops
• compare that element with every other Array element
• Selection sort makes O(n) swaps which is minimum among all sorting algorithms
mentioned above merge ,heap and insertion
• Is Selection Sort Algorithm stable?
Stability: The default implementation is not stable. However, it can be made stable.
Please see stable selection sort for details.
• Is Selection Sort Algorithm in place?
Yes, it does not require extra space.
• Selection Sort is an in-place algorithm having minimum number of swaps. It
works on greedy approach and takes O(n) swaps to sort the array of n elements.
• T(N) = T(N-1) + N RECCURENCE RELATION
BUBBLE SORT
• Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in the wrong order.
• This algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high.
• Total number of swaps = Total number of comparison
• Total number of comparison (Worst case) = n(n-1)/2
• Total number of swaps (Worst case) = n(n-1)/2
• What is the Boundary Case for Bubble sort?
Bubble sort takes minimum time (Order of n) when elements are already sorted.
Hence it is best to check if the array is already sorted or not beforehand, to avoid
O(N2) time complexity.
• Does sorting happen in place in Bubble sort?
Yes, Bubble sort performs swapping of adjacent pairs without the use of any major
data structure. Hence Bubble sort algorithm is an in-place algorithm.
• Is the Bubble sort algorithm stable?
Yes, the bubble sort algorithm is stable.
• Where is the Bubble sort algorithm used?
Due to its simplicity, bubble sort is often used to introduce the concept of a sorting
algorithm. In computer graphics, it is popular for its capability to detect a tiny error
(like a swap of just two elements) in almost-sorted arrays and fix it with just linear
complexity (2n).
• RECCURENCE RELATION
MERGE SORT
• The Merge Sort algorithm is a sorting algorithm that is based on the Divide and
Conquer paradigm. In this algorithm, the array is initially divided into two equal
halves and then they are combined in a sorted manner.
• Is Merge sort In Place?
No, In merge sort the merging step requires extra space to store the elements.
• Is Merge sort Stable?
Yes, merge sort is stable.
• How can we make Merge sort more efficient?
Merge sort can be made more efficient by replacing recursive calls with Insertion
sort for smaller array sizes, where the size of the remaining array is less or equal to
43 as the number of operations required to sort an array of max size 43 will be less
in Insertion sort as compared to the number of operations required in Merge sort.
• Analysis of Merge Sort:
A merge sort consists of several passes over the input. The first pass merges
segments of size 1, the second merges segments of size 2, and the i_{th} pass
merges segments of size 2i-1. Thus, the total number of passes is [log2n]. As
merge showed, we can merge two sorted segments in linear time, which means
that each pass takes O(n) time. Since there are [log2n] passes, the total computing
time is O(nlogn).
• T(N) = 2*T(N/2) + N RECCURENCE RELATION
Drawbacks of Merge Sort:
• Slower compared to the other sort algorithms for smaller tasks.
• The merge sort algorithm requires an additional memory space of 0(n) for the
temporary array.
• It goes through the whole process even if the array is sorted.
QUICK SORT
• QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot
and partitions the given array around the picked pivot
• When first element or last element is chosen as pivot, Quick Sort's worst
case occurs for the sorted arrays. In every step of quick sort, numbers are
divided as per the following recurrence. T(n) = T(n-1) + O(n)
• In worst case, the chosen pivot is always placed at a corner position and
recursive call is made for following. a) for subarray on left of pivot which is of
size n-1 in worst case. b) for subarray on right of pivot which is of size 0 in
worst case.
• Is QuickSort stable?
The default implementation is not stable. However any sorting algorithm can be
made stable by considering indexes as comparison parameter.
• Is QuickSort In-place?
As per the broad definition of in-place algorithm it qualifies as an in-place sorting
algorithm as it uses extra space only for storing recursive function calls but not
for manipulating the input.
• What is 3-Way QuickSort?
In simple QuickSort algorithm, we select an element as pivot, partition the array
around pivot and recur for subarrays on left and right of pivot.
Consider an array which has many redundant elements. For example, {1, 4, 2,
4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}. If 4 is picked as pivot in Simple
QuickSort, we fix only one 4 and recursively process remaining occurrences. In
3 Way QuickSort, an array arr[l..r] is divided in 3 parts:
• arr[l..i] elements less than pivot.
• arr[i+1..j-1] elements equal to pivot.
• arr[j..r] elements greater than pivot.
• T(N) = T(N-1)+ N WORST CASE RECCURENCE RELATION
• BEST CASE T(N) = 2*T(N/2) + N RECCURENCE RELATION
HEAP SORT

• Heap sort is a comparison-based sorting technique based on Binary Heap data


structure. It is like the selection sort where we first find the minimum element
and place the minimum element at the beginning. Repeat the same process for
the remaining elements.
• Heap sort is an in-place algorithm.
• Its typical implementation is not stable, but can be made stable (See this)
• Typically, 2-3 times slower than well-implemented QuickSort. The reason for
slowness is a lack of locality of reference
• T(N) = T(N-1)+log(N) RECCURENCE RELATION
• Efficiency – The time required to perform Heap sort increases logarithmically
while other algorithms may grow exponentially slower as the number of items to
sort increases. This sorting algorithm is very efficient.
• Memory Usage – Memory usage is minimal because apart from what is
necessary to hold the initial list of items to be sorted, it needs no additional
memory space to work
• Simplicity – It is simpler to understand than other equally efficient sorting
algorithms because it does not use advanced computer science concepts such
as recursion
• What is meant by Heapify?
Heapify is the process of creating a heap data structure from a binary tree
represented using an array. It is used to create Min-Heap or Max-heap. Start from
the first index of the non-leaf node whose index is given by n/2 – 1. Heapify uses
recursion
• What are the two phases of Heap Sort?
The heap sort algorithm consists of two phases. In the first phase the array is
converted into a max heap. And in the second phase the highest element is
removed (i.e., the one at the tree root) and the remaining elements are used to
create a new max heap.
• Why Heap Sort is not stable?
Heap sort algorithm is not a stable algorithm. This algorithm is not stable because
the operations that are performed in a heap can change the relative ordering of the
equivalent keys.
• Is Heap Sort an example of “Divide and Conquer” algorithm?
Heap sort is NOT at all a Divide and Conquer algorithm. It uses a heap data
structure to efficiently sort its element and not a “divide and conquer approach” to
sort the elements.
• Which sorting algorithm is better – Heap sort or Merge Sort?
The answer lies in the comparison of their time complexity and space requirement.
The Merge sort is slightly faster than the Heap sort. But on the other hand merge
sort takes extra memory. Depending on the requirement, one should choose which
one to use.
• Why Heap sort better than Selection sort?
Heap sort is like selection sort, but with a better way to get the maximum element.
It takes advantage of the heap data structure to get the maximum element in
constant time.
SHELL SORT

• Shell sort is mainly a variation of Insertion Sort.


• Replacement for insertion sort, where it takes long time to complete given
task.
• To call stack overhead we use shell sort.
• when recursion exceeds a particular limit we use shell sort.
• For medium to large-sized datasets.
• In insertion sort to reduce the number of operations .
TIME COMPLEXITY
INSERTION SELECTION BUBBLE MERGE HEAP QUICK SHELL
BEST O(N) O(N)^2 O(N) O(NlogN) O(N) O(NlogN) O(NlogN)
AVERAGE O(N)^2 O(N)^2 O(N)^2 O(NlogN) O(NlogN) O(NlogN) O(NlogN)
WORST O(N)^2 O(N)^2 O(N)^2 O(NlogN) O(NlogN) O(N)^2 O(N)^2
Space O(1) O(1) O(1) O(N) O(1) O(logn) O(1)
complexity

You might also like