0% found this document useful (0 votes)
10 views

Sorting Algorithm

The document provides an overview of various sorting algorithms, including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort, and Heap Sort, detailing their mechanisms, time complexities, and characteristics. It emphasizes the differences in performance and use cases for each algorithm, such as stability and in-place sorting. Additionally, it includes practice questions and resources for further learning on searching and sorting techniques.

Uploaded by

rohitgaikwad6157
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)
10 views

Sorting Algorithm

The document provides an overview of various sorting algorithms, including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort, and Heap Sort, detailing their mechanisms, time complexities, and characteristics. It emphasizes the differences in performance and use cases for each algorithm, such as stability and in-place sorting. Additionally, it includes practice questions and resources for further learning on searching and sorting techniques.

Uploaded by

rohitgaikwad6157
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/ 34

UNIT-VI

SEARCHING AND
SORTING

Algorithms
Bubble Sort:
Bubble Sort is a comparison based Sorting Algorithm.
It works by checking its adjacent element whether, it is in sorted order or not.
It is an In-place Sorting algorithm as we don’t need any extra data structure while sorting.
It is stable (the sequence of repeating elements does not change).

Working of Bubble Sort:

No of Required Passes = (n-1)


Total Comparisons = (n-1) + (n-2) + (n-3) + ………….1 = ( (n-1) * n )/2 = O(n²) { Every Case}
Total Swaps :
minimum = 0 {elements are in sorted order}
maximum = (n-1) + (n-2) + (n-3) + ………….1 = ( (n-1) * n )/2 = O(n²)
Time Complexity = maximum(Comparison, Swaps)
= O(n²) {Worst and average Case} and O(n) in Best case.
Space Complexity : O(1)
Further Optimization:
The Time Complexity of Bubble Sort can be optimized for Best Case.i.e When all the elements of array
are in sorted order, then there will be no swaps, So we can use it to identify whether our array is sorted
or not.

We can use a flag variable with initial value = 0,


If in any pass, there is no swap, just break the loop and we will get sorted elements.
TC: O(N) {Best Case}, O(n²) in {Average, Worst Case}
Insertion Sort:

Insertion Sort is another Comparison Based Sorting Algorithm.


It works as similar as the way we sort playing cards. i.e. we have unsorted and sorted portions
of the cards, and we use to place the unsorted card in its correct position.
Insertion Sort works similar to that.
It is also in-place Sorting algorithm.
It is Stable Sorting Algorithm.
It can be useful when number of elements are very less.
It can be also useful when elements are in almost sorted order.
Insertion Sort is useful in Best Case Only : TC = O(N) {Best Case}
Total Comparisons : N-1 in Best Case
If Array is almost Sorted Insertion Sort will take O(N)
No of swaps = No of Inversions.
Worst Case of Insertion Sort can be when elements are in Descending Order.
Total Comparisons, swaps in Worst Case = 1+2+3+4+5+……..n-1 = O(n²)
Average Case (Half Ascending + Half Descending) = n/2 BC + n/2 WC = O(n²)
Practice Questions:

Q 1.) No of swaps performed by insertion sort in the given array:


1 2 3 5 0 4 10 8 7

Ans.) 8 swaps.

Q 2.) No of comparisons required to perform insertion sort


25 75 95 125 80 5 10
Ans.) 17 comparisons.
Selection Sort:
Selection Sort is another Comparison based Sorting Algorithm.
It also uses the concept of Sorted and Unsorted Subarray.
At each pass, we sort one element by finding minimum element from unsorted array
It is Not Stable as the sequence of repeating element can be change.
It is In-place as it doesn’t require any extra space.

Selection Sort Requires N-1 Passes.


Total Comparisons = ( (N-1) * N ) /2 = O(n²) { Every Case},
Total Swaps = N-1 { Every Case}
Time Complexity = O(n²) {because comparisons are always O(n²) }
Selection Sort speciality is No of swaps are N-1 in every case.
Quick Sort:
It is a Divide and Conquer Algorithm It is also a type of comparison based Sorting Algorithm.
The Partition Algorithm is the heart of Quick Sort Algorithm.
We choose an element as pivot and perform partition algorithm to the pivot algorithm.
The idea to put pivot element at it’s correct position, which means the left side elements of pivot
elements will be less than or equal to pivot element and the right side elements will be greater than
pivot element.
NOTE: Left and Right side elements need not to be in Sorted Order, to Sort them, we again call Quick
Sort for both the partitions.
It is considered to be in-place algorithm as it only used recursive space.
It is not Stable.

Pivot Selection can be done in Many ways:


Always Select first element as pivot
Always Select last element as pivot
Pick Random element as pivot
Pick Median as pivot.

Let’s choose first element as pivot


1.) When first element is selected as pivot:

Take i = p = index of first element.


Take second element index as j.
Check if a[j]<= a[p], if yes, increment i, and swap a[j] & a[i].
At the end of the pass swap a[i] and a[p].

2. When last element is selected as pivot:


Take i = index of first element.
Take p = index of last element.
Take j = index of first element, run j loop from j=0 to n-1.
if a[i]<=a[p], swap a[i] and a[j], and then increment i by 1.
At the end, swap a[i] and a[p].
Time Complexity of Quick Sort:

T(n) = T(K-p) + T(q-m) + n + O(1),


where K is the pivot element index after partition, p is the starting index, q is the
last index, O(n) is the time required for partition algorithm, O(1) for choosing pivot
element.

Best Case = T(n/2) + T(n/2) + n, when after partition, there will be equal elements
on both sides.
Worst Case + T(n-1) + n, when after partition, the elements are skewed on one
side only.
Merge Sort:
It is another comparison based sorting algorithm.
It is based on the Divide and conquer technique.
It is Outplace algorithm as we need an additional array to store the temp
output of the sorted array.
It is Stable Sort.
Time Complexity of Merge algorithm is : O(N) for outplace,
O(n/2*n/2) = O(n2) for in-place.

Recurrence Relation
:1.) for Outplace:
Heap Sort:

Heap Sort is a comparison-based Sorting Technique.


It is based on Binary Heap.
Heap sort is an in-place algorithm.
It is not stable, because the heap operations can change the relative ordering of the elements.
We use heapify method in Heap Sort.
It is similar to selection sort, but we can maximum element from heap in constant time.

1. If Complete Binary tree contains K-levels, Total Nodes =


2. Total leaf nodes in CBT
3. Total Internal nodes in CBT =

Representation of Binary Tree:


1.Array
2. Linked List

NOTE: If node is stored in 1st place of the array then


1.) Parent of i =
2.) Left child of i = 2 * i
3.) Right child of i = (2*1) + 1

NOTE: Array is better for complete and almost complete Binary Tree.
Linked List is better idea for any gap in Nodes.
Max-Heap: Root is maximum or equal to children at every level.
for ith max, Total comparison = i(i-1)/2 = O(i2).

In Bubble Sort, every pass is costly O(n), but in heap sort, every comparison cost is O(1).

Insertion in Max-Heap (Bottom to top Approach):


Deletion in Max Heap: (Top to Bottom Approach):

NOTE: To delete an element , we can simply put the deleted element at last place, and reduce the loop to one place (from 1-8).
As soon as we deleted the element, we will get the elements in ascending order (because we are placing max element
at last place.)
Heap Sort:
For Insertion in a max heap or min heap, O(logn) times requires for an single element.
for n elements = O(nlogn).
for Deletion in a max or min heap, O(logn) times require for an single element.
for deletion of n elements = O(nlogn)
For heap sort, creation and deletion requires for n elements = O(2nlogn).

NOTE: This method takes O(nlogn) to create a max heap, but there is another method, which is heapify
method and it takes O(n) to create a Build Heap.

Heapify Method: We apply Heapify Method for non-Leaf Nodes.

Heapify Method takes O(logn) time.


Resources:
SR. TITLE VIDEO RESOURCES WEBSITES
NO.
1 Lenear Search Click Here Click Here
2 Binary Search Click Here Click Here
3 Introduction to Sorting Click Here Click Here
4 Divide & Conquer Algo Click Here Click Here
5 Tower of Hanoi Click Here Click Here
6 Bubble Sort CLICK HERE Click Here
7 Insertion Sort CLICK HERE Click Here
8 Selection Sort CLICK HERE Click Here
9 Merge Sort PART1 Click Here
PART2
PART3
10 Quick Sort CLICK HERE Click Here
11 Heap Sort Introduction to Heap Tree Click Here
Insertion in Heap Tree
Build Heap
Deletion in Heap Tree
Heap Sort
12 Radix Sort Radix Sort Click Here

YouTube Playlist: 1) https://youtube.com/playlist?list=PLxCzCOWd7aiHcmS4i14bI0VrMbZTUvlTa


2) https://youtube.com/playlist?list=PLu0W_9lII9ahIappRPN0MCAgtOu3lQjQi
3) https://youtube.com/playlist?list=PLYwpaL_SFmcDJ4M2i83kIwwnPit1aIOhQ

Imp Questions:

1. Write a function in C/C++ to implement binary search method. Show the step wise
execution of the binary search algo to search an element 48 in following list.
1, 5, 8, 13, 19, 20, 23, 44, 48, 58, 61.
2. Write an algo for Bubble Sort and calculate its time complexity.
3. Write a pseudo-C code for Merge Sort. What is its time complexity.
4. Write a pseudo-C code for Binary search and state advantages of binary search over linear
search.
5. Explain Quick sort algorithm sorts a sequence S using Divide and Conquer approach.
6. Which searching technique is best in data structure? Why?
7. List out the types of sorting available in Data Structures.
8. What is the purpose of quick sort and advantage?
9. Write a program to implement bubble sort concept.
10. Explain Selection Algo with a proper example.
11. Write a pseudo-C code for Quick sort algorithm.
12. Write a C/C++ non-recursive and recursive function for binary search.
13. Show the stepwise execution of the Bubble sort algorithm for the following list. Give the
time complexity of algorithm.
23, 8, 44, 1, 61, 13, 58, 20, 48, 19
14. What is Max Heap and Min Heap. What is Heapify process.
15. Explain Heap Sort with a proper example. And explain the time complexity of Heap Sort.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy