DS Unit 5
DS Unit 5
SEARCHING TECHNIQUES
Algorithm:
Linear Search (Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n, then jump to step 7
Step 3: if A[i] = x then jump to step 6
Step 4: Set i to i + 1
Step 5: Go to step 2
Step 6: Print element x found at index i and jump to step 8
Step 7: Print element not found
Step 8: Exit
WORKING ON LINEAR SEARCH
unsorted array:
Let the elements of array are -
The value of K, i.e., 41, is not matched with the first element of the array.
So, move to the next element. And follow the same process until the
respective element is found.
WORKING ON LINEAR SEARCH
Algorithm:
Step 1: set beg= lower_bound, end=upper_bound, pos=-1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
print "value is present in the array"
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
WORKING OF BINARY SEARCH
Sorted array:
So, swap 12 with 8. After the first iteration, 8 will appear at the first
position in the sorted array.
WORKING OF SELECTION SORT ALGORITHM
For the second position, where 29 is stored presently, we again sequentially scan the rest
of the items of unsorted array. After scanning, we find that 12 is the second lowest
element in the array that should be appeared at second position
Now, swap 29 with 12. After the second iteration, 12 will appear at the second
position in the sorted array. So, after two iterations, the two smallest values are
placed at the beginning in a sorted way.
WORKING OF SELECTION SORT ALGORITHM
The same process is applied to the rest of the array elements. Now, we
are showing a pictorial representation of the entire sorting process.
How it works:
• Divide the unsorted array into two sub-arrays, half
the size of the original.
• Continue to divide the sub-arrays as long as the
current piece of the array has more than one
element.
• Merge two sub-arrays together by always putting the
lowest value first.
• Keep merging until there are no sub-arrays left.
WORKING OF MERGE SORT ALGORITHM
WORKING OF MERGE SORT ALGORITHM
WORKING OF MERGE SORT ALGORITHM
WORKING OF MERGE SORT ALGORITHM
• Quicksort is a sorting algorithm based on the divide and conquer approach where an
array is divided into subarrays by selecting a pivot element (element selected from
the array).
• While dividing the array, the pivot element should be positioned in such a way that
elements less than pivot are kept on the left side and elements greater than pivot are
sorted array.
Working of Quick Sort Algorithm
Time Complexity
Best O(n*log n)
Worst O(n2)
Average O(n*log n)
• Space Complexity O(log n)
Quick Sort Algorithm
quickSort(array, leftmostIndex, rightmostIndex)
if (leftmostIndex < rightmostIndex)
pivotIndex <- partition(array,leftmostIndex, rightmostIndex)
quickSort(array, leftmostIndex, pivotIndex - 1)
quickSort(array, pivotIndex, rightmostIndex)
partition(array, leftmostIndex, rightmostIndex)
set rightmostIndex as pivotIndex
storeIndex <- leftmostIndex - 1
for i <- leftmostIndex + 1 to rightmostIndex
if element[i] < pivotElement
swap element[i] and element[storeIndex]
storeIndex++
swap pivotElement and element[storeIndex+1]
return storeIndex + 1
Insertion Sort
• Insertion sort is a simple sorting algorithm that iteratively constructs a sorted section
element of the array is first considered the sorted component, whereas the remaining
sorted segment, beginning at the end, and adjusts the bigger elements one position to
the right until the unsorted element is found in the correct location.
• After determining the proper location, the unsorted element is placed into the sorted
Algorithm
The simple steps of achieving the insertion sort are listed
as follows -
Step 1 - If the element is the first element, assume that it
is already sorted. Return 1.
Step2 - Pick the next element, and store it separately in
a key.
Step3 - Now, compare the key with all elements in the
sorted array.
Step 4 - If the element in the sorted array is smaller than
the current element, then move to the next element. Else,
shift greater elements in the array towards the right.
Pseudocode for Insertion Sort
Step 1 − Start
Step 2 − Initialize the value of gap size, say h.
Step 3 − Divide the list into smaller sub-part. Each must have equal
intervals to h.
Step 4 − Sort these sub-lists using insertion sort.
Step 5 – Repeat this step 2 until the list is sorted.
Step 6 – Print a sorted list.
Step 7 – Stop.
SHELL SORT EXAMPLE
In the second loop, elements are lying at the interval of 2 (n/4 = 2),
where n = 8.
Now, we are taking the interval of 2 to sort the rest of the array. With an
interval of 2, two sublists will be generated - {12, 25, 33, 40}, and {17,
8, 31, 42}.
SHELL SORT EXAMPLE
In the third loop, elements are lying at the interval of 1 (n/8 = 1), where n =
8. At last, we use the interval of value 1 to sort the rest of the array
elements. In this step, shell sort uses insertion sort to sort the array
elements.
• Radix sort is the linear sorting algorithm that is used for integers.
• In Radix sort, there is digit by digit sorting is performed that is started from
the least significant digit to the most significant digit.
• The key idea behind Radix Sort is to exploit the concept of place value. It
assumes that sorting numbers digit by digit will eventually result in a fully
sorted list.
• The process of radix sort works similar to the sorting of students names,
according to the alphabetical order.
• In this case, there are 26 radix formed due to the 26 alphabets in English. In
the first pass, the names of students are grouped according to the ascending
order of the first letter of their names.
• After that, in the second pass, their names are grouped according to the
ascending order of the second letter of their name. And the process
continues until we find the sorted list.
RADIX SORT ALGORITHM
Algorithm
1.Find the Maximum Element: Determine the number with the
maximum digits to define the number of passes required.
2. Sort by Each Digit:
Start from the least significant digit (LSD) to the most
significant digit (MSD).
Use a stable sorting algorithm like Counting Sort at each digit
level.
3.Update and Repeat: Reorder the list based on each subsequent
digit until all digits are processed.
RADIX SORT
Step 1: Find the largest element in the array, which is 802. It has
three digits, so we will iterate three times, once for each significant
place.
Step 2: Sort the elements based on the unit place digits (X=0). We
use a stable sorting technique, such as counting sort, to sort the
digits at each significant place.
RADIX SORT