DS Unit-2 SearchSort
DS Unit-2 SearchSort
SYLLABUS
UNIT 1: Data Structures and Algorithms Basics (8 Hours)
Introduction: Basic terminologies, elementary data organizations, data structure operations;
abstract data types (ADT) and their characteristics.
Algorithms: Definition, characteristics, analysis of an algorithm, asymptotic notations, time
and space trade-offs.
Array ADT: Definition, operations and representations – row-major and column- major.
1
20-08-2024
Application of Searching
LINEAR & BINARY SEARCH
Searching in an array or other data structure has two possible
outcomes:
• Successful search (value found)
• Unsuccessful search (value not found)
7 8
2
20-08-2024
Number 580625685
Each record in list has an associated key.
In this example, the keys are ID numbers.
9 10
No of Comparisons = 11
Comparison = 05
11 12
3
20-08-2024
13 14
15 16
4
20-08-2024
17 18
[0] [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53 3 6 7 11 32 33 53
5
20-08-2024
[0] [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53 3 6 7 11 32 33 53
[0] [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53 3 6 7 11 32 33 53
6
20-08-2024
[0] [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53 3 6 7 11 32 33 53
7
20-08-2024
How about the worst case for a list with 32000 items ?
29 30
8
20-08-2024
33 34
Difference between linear and binary search Advantages of Binary Search Algorithm
• It follows the technique of eliminating half the array elements and is, therefore, more
efficient than a linear search for large amounts of data.
• Lower time complexity, which means it takes less time to compile.
• This is a better option than linear search because it splits the array in half rather than
sequentially traversing through each element.
9
20-08-2024
37 38
1. Insertion Sort,
2. Shell Sort,
3. Quick Sort,
4. Merge Sort,
5. Heap Sort,
39 6. Counting Sort 40
10
20-08-2024
# of tape accesses
O(N2) O(N log N) O(N)
11
20-08-2024
23 78 45 8 32 56 Original List
After pass 1
23 78 45 8 32 56
23 45 78 8 32 56 After pass 2
After pass 3
8 23 45 78 32 56
After pass 4
8 23 32 45 78 56
After pass 5
45 8 23 32 45 56 78 46
12
20-08-2024
13
20-08-2024
Insertion
Sort
53 54
Shell Sort
Shell Sort function Shell_Sort(a, n) Shell Sort Algorithm
The steps or procedure for the shell sort
1. step=round(n/2) algorithm is as follows-
2. Repeat Step 3 to 10 while step>0 Step 1) Initialize the interval value, step = n/2.
3. Repeat Step 4 to 9 for i=step to n-1 (In this example, n is the size of the array)
4. Temp=a[i] Step 2) Put all the elements within a distance of
5. j=i the interval step in a sub-list.
6. Repeat Step 7 and 8 while j>=step Step 3) Sort those sub-lists using insertion sort.
and a[j-step]>temp Step 4) Set new interval, step=step/2.
Step 5) If step>0, go to step 2. Else go to step 6.
7. a[j]= a[j-step] Step 6) The resultant output will be the sorted
8. j=j-step // End of Step 6 array.
9. a[j]=temp // End of Step 3
10. step = (step/2) //End of Step 2
55 56
14
20-08-2024
divide 5 4 divide
6 3 9 1 7 2 20 40 10 80 50 60 7 30
15
20-08-2024
3. Access next item from that input sequence Repeat while I <= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX + 1, SET I = I + 1
[END OF LOOP]
3. Copy any remaining from first sequence to output [END OF IF]
Step 4: [Copy the contents of TEMP back to ARR] SET K=
Step 5: Repeat while K < INDEX
4. Copy any remaining from second to output SET ARR[K] = TEMP[K]
SET K = K + 1
[END OF LOOP]
Step 6: END
61 62
63 64
16
20-08-2024
65 66
17
20-08-2024
Step IV: Store cumulative sum of the elements of the count array.
It helps in placing the elements into the correct index of the sorted array.
71 Step VI: After placing each element at its correct position, decrease its count by 1.72
18
20-08-2024
73 74
75 76
19
20-08-2024
77 78
• Step 1: Declare an auxiliary array Aux[] of size max(Arr[])+1 and initialize it with 0.
• Step 2: Traverse array Arr[] and map each element of Arr[] as an index of Aux[] array, i.e.,
execute Aux[Arr[i]]++ for 0 <= i < N.
• Step 5: Traverse array Arr[] from right to left and update sortedArr[] as
sortedArr[ Aux[ Arr[i] ] - 1] - Arr[i]. Also, update Aux[] as Aux[ Arr[i] ]--.
As we are using the index of an array to map elements of array Arr[], if the difference between the
maximum element and minimum element is huge, counting sort will not be feasible.
79 80
20
20-08-2024
21
20-08-2024
• Then it finds the second smallest element from the remaining elements in the
After pass 1
array and places it in the second position of the array and so on. 8 78 45 23 32 56
• Two loops are used in the Selection Sort algorithm.
• The outer loop moves from the first element in the array to the next to last 8 23 45 78 32 56 After pass 2
element; the inner loop moves from the second element of the array to the
After pass 3
last element, looking for values that are smaller than the element currently 8 23 32 78 45 56
being pointed at by the outer loop.
• After each iteration of the inner loop, the most minimum value in the array is 8 23 32 45 78 56
After pass 4
85
8 23 32 45 56 78 86
22
20-08-2024
WORST CASE
BUBBLE O(n) O(n2) O(n2) O(1) constant Stable Exchange
The worst case occurs when the array A is in reverse order and the inner while loop must use the maximum
number (n – 1) of comparisons. Hence
SELECTION O(n) O(n2) O(n2) O(1) constant Stable Selection
f(n) = (n – 1) + ....... 2 + 1 = (n (n – 1))/2 = O(n2).
INSERTION O(n) O(n2) O(n2) O(1) constant Stable Insertion
AVERAGE CASE
On the average case there will be approximately (n – 1)/2 comparisons in the inner while loop. Hence the
SHELL O(n) O(n log n) O(n log n) O(1) constant Instable Insertion
average case
QUICK O(n log n) O(n log n) O(n2) O(1) constant Stable Partitioning
f (n) = (n – 1)/2 + ...... + 2/2 +1/2 = n (n – 1)/4 = O(n2).
RADIX O(n log n) O(n log n) O(n2) Depends Stable Partitioning
BEST CASE
Partitioning
The best case occurs when the array A is in sorted order and the outer for loop will iterate for (n – 1) times. MERGE O(n log n) O(n log n) O(n log n) Depends Instable
+ Merging
And the inner while loop will not execute because the given array is a sorted array, i.e.,
f (n) = O(n). HEAP O(n log n) O(n log n) O(n log n) O(1) constant Instable Selection
89 COUNTING O(N+M) O(N+M) O(N+M) O(1) constant Stable Counting 90
91 92
23
20-08-2024
93
24