Learning Outcomes
1. Explainworking of given method with example
2. Write an algorithm to search the given key using
binary search method
3. Write an algorithm to sort data using a sorting
method
4. Explain the working of given sorting method step by
step with an example
4.
What will weLearn
• Introduction to searching
• Searching Techniques (Algorithm & Examples)-Linear Search, Binary Search
•Introduction to sorting,Sorting Techniques (Algorithm & Examples)-Bubble
Sort,Insertion Sort,Selection Sort, Merge Sort, Radix Sort,Heap Sort,Shell Sort
• Comparison of Sorting Algorithm
5.
Searching
Finding a value
Types
◦Linear Search
◦ Binary Search
When to use which search technique?
Scrambled values in array – linear search
array is already sorted – Binary Search
6.
Linear Search
❖ Traversethe array using a for loop.
❖ In every iteration, compare the target value with the current value of the array.
◦ If the values match, return the current index of the array.
◦ If the values do not match, move on to the next array element.
❖ If no match is found, return -1
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go 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 go to step 8
Step 7: Print element not found
Step 8: Exit
7.
Program
for (i =0; i < n; i++)
{
if (array[i] == search) /* If required element is found */
{
printf("%d is present at location %d.n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.n", search);
8.
Complexity of LinearSearch Algorithm
Linear search executes in O(n) time where n is the number of elements in the array.
Best Case?
Obviously, the best case of linear search is when VAL is equal to the first element of the array. In
this case, only one comparison will be made.
Worst Case?
Likewise, the worst case will happen when either VAL is not present in the array or it is equal to
the last element of the array. In both the cases, n comparisons will have to be made.
How to Improve?
However, the performance of the linear search algorithm can be improved by using a sorted
array.
9.
Binary Search
Start withthe middle element:
◦ If the target value is equal to the middle element of the array, then return the index of the middle
element.
◦ If not, then compare the middle element with the target value,
◦ If the target value is greater than the number in the middle index, then pick the elements to the right of the middle index, and start
with Step 1.
◦ If the target value is less than the number in the middle index, then pick the elements to the left of the middle index, and start with
Step 1.
When a match is found, return the index of the element matched.
If no match is found, then return -1
Binary Search
A ←sorted array n ← size of array x ← value to be searched
Set lowerBound = 1 Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
Working
Insertion sort worksas follows:
The array of values to be sorted is divided into two sets. One that stores sorted values and
another that contains unsorted values.
The sorting algorithm will proceed until there are elements in the unsorted set.
Suppose there are n elements in the array. Initially, the element with index 0 (assuming LB = 0) is
in the sorted set. Rest of the elements are in the unsorted set.
The first element of the unsorted partition has array index 1 (if LB = 0).
During each iteration of the algorithm, the first element in the unsorted set is picked up and
inserted into the correct position in the sorted set.
17.
Selection Sort
Compares anelement against all the elements of the array until it finds the smallest, then they
swap places. Small elements get pushed to the beginning with each pass of the loop.
Heap Sort
Heap Sortis one of the best sorting methods being in-place and with no quadratic worst-case
running time. Heap sort involves building a Heap data structure from the given array and then
utilizing the Heap to sort the array.
1. Shape Property