0% found this document useful (0 votes)
19 views132 pages

AKJ - STD - Module-5 - Sorting - Searching - Hashing

The document discusses various sorting and searching algorithms like binary search, bubble sort, selection sort, insertion sort, merge sort, quicksort, and heapsort. It provides descriptions of how each algorithm works, examples to illustrate the steps, and analysis of time complexity. Divide and conquer algorithms like merge sort and quicksort are analyzed. Quicksort has average time complexity of O(nlogn) but can be O(n^2) in worst case, while merge sort always has O(nlogn) time complexity.

Uploaded by

goyalbabita77
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)
19 views132 pages

AKJ - STD - Module-5 - Sorting - Searching - Hashing

The document discusses various sorting and searching algorithms like binary search, bubble sort, selection sort, insertion sort, merge sort, quicksort, and heapsort. It provides descriptions of how each algorithm works, examples to illustrate the steps, and analysis of time complexity. Divide and conquer algorithms like merge sort and quicksort are analyzed. Quicksort has average time complexity of O(nlogn) but can be O(n^2) in worst case, while merge sort always has O(nlogn) time complexity.

Uploaded by

goyalbabita77
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/ 132

Module-V

Data Structure Using C


Sorting & Searching Algorithms and
Hashing techniques

Dr Anant. K. Jayswal
MTech(CSE), PhD(CSE)-JNU
BSc(CS),MSc(CS)-BHU
GATE(CS)
UGC-NET(CS)
Amity School of Engineering & Technology (CSE)

Binary Search

16
Amity School of Engineering & Technology (CSE)

Algorithm (Binary Search)

17
Amity School of Engineering & Technology (CSE)

Analysis of Binary Search (Iterative)

18
Amity School of Engineering & Technology (CSE)

Analysis of Binary Search


Method1:

19
Amity School of Engineering & Technology (CSE)

20
Amity School of Engineering & Technology (CSE)

Analysis of Binary Search(Recursive)

21
Linear Vs Binary Search
Amity School of Engineering & Technology (CSE)

22
Sorting Algorithms
⚫ One of the fundamental problems of computer science is ordering a list of items.
Some sorting algorithms are simple and intuitive, such as the bubble sort.
Others, such as the quick sort are extremely complicated, but produce
lightening-fast results.

• The most common sorting algorithms are:


➢ Bubble sort
➢ Insertion sort
➢ Selection Sort
➢ Merge sort
➢ Quick sort
➢ Heap sort
➢ Shell sort

23
Bubble Sort
⚫ Suppose the list of numbers are given as: A[1], A[2], A[3], ……., A[N]. The
Bubble sort algorithm works as follows:

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

❑ 1st for loop for no of passes and 2nd for loop for no of comparison in each iterations. 25
Bubble Sort (Modified version1)-
-number of comparison in subsequent iteration should be reduced (pass1: (n-1) comparison, pass2: n-2
comparisons, and so on.

26
Bubble Sort (optimized version)-
- Sometimes not all passes are required to sort the given array of n-elements.
For example, an array of 10-elements may be sorted after 4 passes,
Or
If array is already sorted, no swapping is done in any pass then not all
passes are required in this case.

27
Bubble Sort (optimized version)-
- Some times not all passes are required to sort the given array of n-elements. For
example an array of 10-elements may be sorted after 4 passes. Or If array is already
sorted, no swapping is done in any pass then not all passes are required in this case.
𝑓𝑜𝑟 𝑖 = 0; 𝑖 < 𝑛 − 1; 𝑖 + +
{
𝒇𝒍𝒂𝒈 = 𝟎;
𝑓𝑜𝑟(𝑗 = 0; 𝑖 < 𝑛 − 1 − 𝑖; 𝑗 + +)
{
𝑖𝑓(𝐴[𝑗] > 𝐴[𝑗 + 1]
{
𝑡𝑒𝑚𝑝 = 𝐴 𝑗 ;
𝐴 𝑗 =𝐴 𝑗+1 ;
𝐴 𝑗 + 1 = 𝑡𝑒𝑚𝑝;
𝒇𝒍𝒂𝒈 = 𝟏;
}
} //end of inner for loop
𝑖𝑓 (𝑓𝑙𝑎𝑔 == 0)
𝑏𝑟𝑒𝑎𝑘;
}
28
Sorting Algorithms
Selection Sort:
Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the
smallest (or largest) element from the unsorted portion of the list and moving it to the sorted
portion of the list.
The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the
list and swaps it with the first element of the unsorted part. This process is repeated for the
remaining unsorted portion until the entire list is sorted.

31
Selection Sort Algorithms…

32
Sorting Algorithms
Selection Sort:
How does Selection Sort Algorithm work?
Let's consider the following array as an example: arr[] = {64, 25, 12, 22, 11}

33
Sorting Algorithms…
Selection Sort:

34
Sorting Algorithms…
Selection Sort:

35
Sorting Algorithms…
Selection Sort:

36
Sorting Algorithms…
Selection Sort:

37
𝒇𝒐𝒓 𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏; 𝒊 + +
{
𝒊𝒏𝒕 𝒎𝒊𝒏 = 𝒊;
𝒇𝒐𝒓(𝒋 = 𝒊 + 𝟏 ; 𝒊 < 𝒏; 𝒋 + +)
{
𝒊𝒇(𝑨 𝒋 < 𝑨[𝒎𝒊𝒏]);
{
𝒎𝒊𝒏 = 𝒋;
}
}
𝒊𝒇 (𝒎𝒊𝒏!=i)
{
𝒔𝒘𝒂𝒑(𝑨 𝒊 𝒘𝒊𝒕𝒉 𝑨 𝒎𝒊𝒏 );
}

Time complexity:
In pass 1: (n-1) comparison to find the smallest elements, in
Pass2: (n-2) comparisons and so on..
So, f(n)=(n-1)+(n-2)+……._+2+1=O(n^2)
𝒇𝒐𝒓 𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏; 𝒊 + +
{
𝒊𝒏𝒕 𝒎𝒊𝒏 = 𝒊;
𝒇𝒐𝒓(𝒋 = 𝒊 + 𝟏 ; 𝒊 < 𝒏; 𝒋 + +)
{
𝒊𝒇(𝑨 𝒋 < 𝑨[𝒎𝒊𝒏]);
{
𝒎𝒊𝒏 = 𝒋;
}
}
𝒊𝒇 (𝒎𝒊𝒏!=i)
{
𝒔𝒘𝒂𝒑(𝑨 𝒊 𝒘𝒊𝒕𝒉 𝑨 𝒎𝒊𝒏 );
}
Sorting Algorithms…
Insertion Sort:
Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct
position in the sorted part

40
Amity School of Engineering & Technology (CSE)

Insertion Sort

41
Amity School of Engineering & Technology (CSE)

Insertion-Sort(A) Example: Let A=[5,2,4,6,1,3]

42
Amity School of Engineering & Technology (CSE)

Example: Insertion Sort

43
Amity School of Engineering & Technology (CSE)

Example: Insertion Sort

44
Amity School of Engineering & Technology (CSE)

Example2: A=[5,1,6,2,4,3]

45
Amity School of Engineering & Technology (CSE)

Time complexity (Insertion sort)


Best Case:(When array is sorted in increasing order) :
O(n)

Worst Case: (When array is sorted in decreasing order)


O(n^2)

46
Quick Sort and Merge Sort
Divide and Conquer Approach
Divide-and- Amity School of Engineering & Technology (CSE)

conquer

49
Amity School of Engineering & Technology (CSE)

Divide and conquer Algorithm

Algorithm Recurrence Relation Time Complexity


Binary Search 𝑇(𝑛) = 𝑇(𝑛/2) + 𝑘 𝑂(𝑙𝑜𝑔𝑛)
Quicksort Best Case: 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑂(𝑛) 𝑂(𝑛𝑙𝑜𝑔𝑛)
Worst Case: 𝑇(𝑛) = 𝑇(𝑛 − 1) + 𝑇(0) + 𝑂(𝑛) 𝑂(𝑛^2)
Average case: 𝑇(𝑛) = 𝑇(𝑛/10) + 𝑇(9𝑛/10) + 𝑂(𝑛)
𝑂(𝑛𝑙𝑜𝑔𝑛)
Merge-sort Best, Average, Worst: 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑂(𝑛) 𝑂(𝑛𝑙𝑜𝑔𝑛)
Heapsort Best, Average, Worst 𝑂(𝑛𝑙𝑜𝑔𝑛)

50
Quick Sort
Amity School of Engineering & Technology (CSE)

51
Amity School of Engineering & Technology (CSE)

Quick Sort

52
Amity School of Engineering & Technology (CSE)

Quick Sort

53
Amity School of Engineering & Technology (CSE)

Quick Sort

54
Amity School of Engineering & Technology (CSE)

Quick Sort

55
Amity School of Engineering & Technology (CSE)

Quick Sort

56
Amity School of Engineering & Technology (CSE)

57
Amity School of Engineering & Technology (CSE)

Performance of Quick Sort

The running time of QUICK SORT depends on whether the partitioning


is balanced or unbalanced.

Partitioning of the subarrays depends on the input data we receive for


sorting.

58
Amity School of Engineering & Technology (CSE)

Quick Sort-
Best case

If the input data is not sorted, then the partitioning


of subarray is balanced; in this case the algorithm
runs asymptotically as fast as merge-sort (i.e.
𝑂(𝑛𝑙𝑜𝑔𝑛).

59
[When array elements
Amity School of are unsorted
Engineering ] (CSE)
& Technology

The best-case behavior of Quicksort


Recurrence
algorithm occurs when the
Relation(Best partitioning procedure produces two
case) regions of size ≈ (n/2) elements. In
this case, Recurrence can be written
as:

𝑻(𝒏) = 𝟐𝑻(𝒏/𝟐) + 𝑶(𝒏)


Solution:

60
Amity School of Engineering & Technology (CSE)

Quick Sort-worst case


If the given input array is already sorted or almost sorted,
then the partitioning of the subarray is unbalanced. In this
case the algorithm runs asymptotically as slow as bubble
sort (i.e. Θ(𝑛2 )).

61
[When input array is already sorted]
Amity School of Engineering & Technology (CSE)

Recurrence The worst-case behavior for Quicksort


Relation(Worst- occurs, when the partitioning procedures
case) produces one region with (n-1) elements
and one with 0-elements or vice-versa.
That is, completely unbalanced
partitioning.
In this case:
𝑻 𝒏 = 𝑻 𝒏 − 𝟏 + 𝑻 𝟎 + 𝑶(𝒏)
Solution:

62
Amity School of Engineering & Technology (CSE)

Improving
Worst case
Behavior of
Take pivot element as Median.
Quicksort

63
Amity School of Engineering & Technology (CSE)

Improving
Worst case
Behavior of
Quicksort

64
Amity School of Engineering & Technology (CSE)

Quick Sort-Average Case


Except best case or worst case.

In this case, Recurrence relation:


𝑇(𝑛) = 𝑇(𝑛/10) + 𝑇(9𝑛/10) + 𝑂(𝑛)

65
Amity School of Engineering & Technology (CSE)

Recurrence Relation(Average-case)
𝑇(𝑛) = 𝑇(𝑛/10) + 𝑇(9𝑛/10) + 𝑂(𝑛)

66
Amity School of Engineering and Technology

Merge Sort

67
Amity School of Engineering & Technology (CSE)

Merge Sort: Idea

Divide into
A FirstPart SecondPart
two halves
Recursively
sort
FirstPart SecondPart

Merge

A is sorted!

68
Amity School of Engineering & Technology (CSE)

Merge (Concept)
Two sorted array A[ ], B[ ] is given, Lets see how to merge these two sorted
array into one sorted array C[ ].
𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚 𝑀𝑒𝑟𝑔𝑒 𝐴, 𝐵, 𝑚, 𝑛
{
𝑖 = 1, 𝑗 = 1, 𝑘 = 1;
wℎ𝑖𝑙𝑒 𝑖 <= 𝑚 && 𝑗 <= 𝑛
{
𝑖𝑓(𝐴[𝑖] < 𝐵[𝑗])
𝐶 𝑘++ =𝐴 𝑖++ ;
e𝑙𝑠𝑒
𝐶 𝑘++ =𝐵 𝑗++ ;
}
wℎ𝑖𝑙𝑒 𝑖 <= 𝑚
{
𝐶 𝑘 = 𝐴 𝑖 ; 𝑖 + +; 𝑘 + +;
}
wℎ𝑖𝑙𝑒 𝑗 <= 𝑚
{
𝐶 𝑘 = 𝐵 𝑗 ; 𝑗 + +; 𝑘 + +;
} 69
Amity School of Engineering & Technology (CSE)

Merge-sort (Example)

70
Merge Sort: Algorithm Amity School of Engineering & Technology (CSE)

Merge-Sort (A, left, right)


if left ≥ right return
else
mid ← |_(left+right)/2 Recursive Call

Merge-Sort(A, left, mid)


Merge-Sort(A, mid+1, right)
Merge(A, left, mid, right)

71
Amity School of Engineering & Technology (CSE)

Merge-sort
(Example2)
Amity School of Engineering & Technology (CSE)
Here p=0, r=6

Or simply write

73
Merge Sort: Algorithm
Amity School of Engineering & Technology (CSE)
Two sorted array A[ ], B[ ] is given, Lets see how to merge these two sorted array into one
sorted array C[ ].

𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚 𝑀𝑒𝑟𝑔𝑒 𝐴, 𝒍𝒆𝒇𝒕, 𝒎𝒊𝒅, 𝒓𝒊𝒈𝒉𝒕


{
𝑖 = 𝒍𝒆𝒇𝒕, 𝑗 = 𝒎𝒊𝒅 + 𝟏, 𝑘 = 𝒍𝒆𝒇𝒕;
wℎ𝑖𝑙𝑒 𝑖 <= 𝑚𝒊𝒅 &&𝑗 ≤ 𝒓𝒊𝒈𝒉𝒕
{
𝑖𝑓(𝐴[𝑖] < 𝐵[𝑗])
𝐶 𝑘++ =𝐴 𝑖++ ;
e𝑙𝑠𝑒
𝐶 𝑘++ =𝐵 𝑗++ ;
}
wℎ𝑖𝑙𝑒 𝑖 <= 𝑚𝒊𝒅
{
𝐶 𝑘 = 𝐴 𝑖 ; 𝑖 + +; 𝑘 + +;
}
wℎ𝑖𝑙𝑒 𝑗 ≤ 𝒓𝒊𝒈𝒉𝒕
74
{
𝐶 𝑘 = 𝐵 𝑗 ; 𝑗 + +; 𝑘 + +;
}
Merge-Sort: Merge
Amity School of Engineering & Technology (CSE)

Sorted

A:

merge
Sorted Sorted
FirstPart SecondPart

A:

A[left] A[middle] A[right] 75


Amity School of Engineering & Technology (CSE)

76
Amity School of Engineering & Technology (CSE)

77
Amity School of Engineering & Technology (CSE)

78
Amity School of Engineering & Technology (CSE)

79
Amity School of Engineering & Technology (CSE)

Merge-Sort Analysis
n cn

n/2 n/2 2 × cn/2 = cn

log n levels
n/4 n/4 n/4 n/4 4 × cn/4 = cn

n/2 × 2c = cn
2 2 2

Total: cn log n

• Total running time: (nlogn)


• Total Space:  (n)
80
Amity School of Engineering & Technology (CSE)

Merge-Sort Summary
Approach: divide and conquer
Time
– Most of the work is in the merging
– Total time: (n log n)
Space:
– (n), more space than other sorts.

81
Amity School of Engineering and Technology
Binary Heaps:
The (binary) heap data structure is an array object that can be viewed as a complete binary tree, as shown in Figure.
Each node of the tree corresponds to an element of the array that stores the value in the node. The tree is completely
filled on all levels except possibly the lowest, which is filled from the left up to a point. An array A that represents a
heap is an object with two attributes: length[A], which is the number of elements in the array, and heap-size[A], the
number of elements in the heap stored within array A. The root of the tree is A[1], and given the index i of a node, the
indices of its parent PARENT(i), left child LEFT(i), and right child RIGHT(i) can be computed simply:

82
Amity School of Engineering and Technology

85
Amity School of Engineering and Technology

86
Amity School of Engineering and Technology

87
Amity School of Engineering and Technology

88
Amity School of Engineering and Technology

89
Amity School of Engineering and Technology

93
Heapsort Algorithm
Amity School of Engineering and Technology

Analysis:
BUILD_ MAX-HEAP: O(n)
For Loop: n-1 times
Exchange Element: O(1)
Max_Heapify: O(logn)

So, Total Time: O(nlogn)

103
Amity School of Engineering and Technology

HASHING
• In searching techniques, search time depends on the number of elements (say n) in the array.
• A Hashing (or Hash addressing) is independent on the number of elements in an array and
expected search time is O(1).
• In hashing techniques, we use a Hash Function, which gives a hash address (location), where
the data (or key) is stored in the hash table.
• Generally we are taking a set of very large number and mapping into a set of small number.

115
Amity School of Engineering and Technology

Collision:
The hashing process generates a small number for a big key, so there is a possibility that two
keys could produce the same value. The situation where the newly inserted key maps to an
already occupied, and it must be handled using some collision handling technology, that is
h(k1)=h(k2)

116
Amity School of Engineering and Technology

117
Amity School of Engineering and Technology

Hash Function:

118
Amity School of Engineering and Technology

119
Amity School of Engineering and Technology

120
Amity School of Engineering and Technology

121
Amity School of Engineering and Technology

122
Amity School of Engineering and Technology

123
Amity School of Engineering and Technology

124
Amity School of Engineering and Technology

125
Amity School of Engineering and Technology

126
Amity School of Engineering and Technology

Problem with liner probing


1. Worst case searching time is O(n). Like keys: 21,31,41,51,91,….
2. Linear probing suffers from Primary Clustering (long runs of occupied
sequence to get free slot). For example: insert 31
3. In general, we avoid deletion operation in liner probing method, for
example, suppose we delete 44, and then searching 11, then we start from
index 1(11 not found), then index 2(11 not found), then index 3(11 not
found), then index 4 (blank cell), search stop and say unsuccessful search.
But 11 is present in the table.

127
Amity School of Engineering and Technology

Not able to insert 90, in hash table.


128
Amity School of Engineering and Technology

129
Amity School of Engineering and Technology

Module V completed

131

You might also like