Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as...
-
Upload
edwin-jenkins -
Category
Documents
-
view
220 -
download
1
Transcript of Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as...
![Page 1: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/1.jpg)
Search algorithm
In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after evaluating a number of possible solutions.
One example of the problems is searching for an item within an array of items, and the output for this problem is the position of the item in the array.
![Page 2: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/2.jpg)
Linear search
It is a simplest search algorithm. It is based on brute force method. Also known as sequential search. It operates by checking every item of an
array one at a time in sequence until a match is found.
![Page 3: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/3.jpg)
Linear search
FUNCTION LinearSearch(A[], n, x)FOR i = 1 TO n IF A[i] = x THEN RETURN iRETURN 0
This algorithm is unstructured.
![Page 4: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/4.jpg)
Linear search
FUNCTION LinearSearch(A[], n, x)Pos = 0i = 1WHILE (Pos = 0) AND (i <= n) IF A[i] = x THEN Pos = i ELSE i = i + 1RETURN Pos
![Page 5: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/5.jpg)
Improved linear search
The average performance of linear search can be improved by using it on an ordered array.
In the case of no matching item, the search can terminate at the first item which is greater than (lesser than) the unmatched target item, rather than examining the entire array.
![Page 6: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/6.jpg)
Improved linear search
FUNCTION ImprovedLinearSearch(A[], n, x)i = 1WHILE (i <= n) AND (A[i] < x) i = i + 1IF (i <= n) AND (A[i] = x) THEN Pos = iELSE Pos = 0RETURN Pos
![Page 7: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/7.jpg)
Binary search
It is better than linear search or improved linear search.
It uses an ordered array. It operates by checking the middle,
eliminating half of the array from consideration, and then performing the search on the remaining half.
![Page 8: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/8.jpg)
Binary search
FUNCTION BinarySearch(A[], n, x)Pos = 0L = 1R = nWHILE (Pos = 0) AND (L <= R) mid = (L + R) div 2 IF A[mid] = x THEN Pos = mid ....
![Page 9: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/9.jpg)
Binary search
FUNCTION BinarySearch(A[], n, x) .... ELSE IF A[mid] < x THEN L = mid + 1 ' search in the right half ELSE R = mid – 1 ' search in the left halfRETURN Pos
![Page 10: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/10.jpg)
Improved binary search
The last algorithm of binary search uses two comparisons per iteration.
For improvement, the two comparisons can be replaced by a single comparison.
![Page 11: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/11.jpg)
Improved binary search
FUNCTION ImprovedBinarySearch(A[], n, x)L = 1R = n + 1WHILE L < R mid = L + ((R – L) div 2) IF A[mid] < x THEN L = mid + 1 ELSE R = mid
![Page 12: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/12.jpg)
Improved binary search
FUNCTION ImprovedBinarySearch(A[], n, x)....IF (L <= n) AND (A[L] = x) THEN Pos = LELSE Pos = 0RETURN Pos
![Page 13: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/13.jpg)
Sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts items of a list in a certain order.
List of items can be an array of items, or a linked list of items.
Three simple sorting algorithms are insertion sort, selection sort, and bubble sort.
![Page 14: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/14.jpg)
Insertion sort
Every iteration of insertion sort removes an item from the input data, inserting it to the correct position in the already-sorted list, until no input items remain.
becomes
≤ x > x x ...
≤ x x > x ...
![Page 15: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/15.jpg)
Insertion sort
PROCEDURE InsertionSort(A[], n)FOR i = 2 TO n value = A[i] j = i – 1 WHILE (j >= 1) AND (A[j] > value) A[j + 1] = A[j] j = j – 1 A[j + 1] = valueRETURN
![Page 16: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/16.jpg)
Selection sort
The algorithm works as follows: Find the minimum value in the list. Swap it with the value in the first position. Repeat the steps above for the remainder of
the list (starting at the second position and advancing each time).
![Page 17: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/17.jpg)
Selection sort
PROCEDURE SelectionSort(A[], n)FOR i = 1 TO n – 1 minIndex = i FOR j = i + 1 TO n IF A[j] < A[minIndex] THEN minIndex = j swap(i, minIndex)RETURN
![Page 18: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/18.jpg)
Bubble sort
The algorithm works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order.
The pass through the list is repeatedly until no swaps are needed, which indicates that the list is sorted.
The algorithm gets its name from the way smaller items “bubble” to the top of the list.
![Page 19: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/19.jpg)
Bubble sort
PROCEDURE BubbleSort(A[], n)REPEAT Swapped = False FOR i = 1 TO n – 1 IF A[i] > A[i + 1] THEN swap(i, i + 1) Swapped = TrueUNTIL NOT SwappedRETURN
![Page 20: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/20.jpg)
Improved bubble sort
The performance of bubble sort can be improved marginally in the following manner.
First observed that, after each comparison (and contingent swap), the largest item encountered in the current pass will reside in the last position traversed.
![Page 21: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/21.jpg)
Improved bubble sort
PROCEDURE BubbleSort(A[], n)REPEAT Swapped = False FOR i = 1 TO n – 1 IF A[i] > A[i + 1] THEN swap(i, i + 1) Swapped = True n = n – 1UNTIL NOT SwappedRETURN
![Page 22: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/22.jpg)
Simplified improved bubble sort
PROCEDURE BubbleSort(A[], n)FOR i = 1 TO n – 1 FOR j = 1 TO n – i IF A[j] > A[j + 1] THEN swap(j, j + 1)RETURN
![Page 23: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/23.jpg)
Faster sorting algorithm
There are two common sorting algorithm that have faster performance than three simple algorithm before, especially in large data list.
Merge sort Quick sort
![Page 24: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/24.jpg)
Merge sort
Conceptually, a merge sort works as follows: If the list of length 0 or 1, then it is already
sorted. Otherwise: Divide the unsorted list into two sublists of
about half the size. Sort each sublist recursively by reapplying
merge sort. Merge the two sublists back into one sorted
list.
![Page 25: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/25.jpg)
Merge sort
![Page 26: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/26.jpg)
Merge sort
Merge sort works efficiently in sorting a linked list, but needs temporary memory of size n while sorting an array.
![Page 27: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/27.jpg)
Merge sort
FUNCTION MergeSort(A[], L, R)IF L < R THEN mid = (L + R) div 2 MergeSort(A, L, mid) MergeSort(A, mid + 1, R) Merge(A, L, mid, R)RETURN
![Page 28: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/28.jpg)
Merge sort
PROCEDURE Merge(A[], L, mid, R)i = Lj = mid + 1k = 0WHILE (i <= mid) AND (j <= R) k = k + 1 IF A[i] <= A[j] THEN B[k] = A[i] ELSE B[k] = A[j]
![Page 29: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/29.jpg)
Merge sort
PROCEDURE Merge(A[], L, mid, R)....WHILE i <= mid k = k + 1 B[k] = A[i]WHILE j <= R k = k + 1 B[k] = A[j]....
![Page 30: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/30.jpg)
Merge sort
PROCEDURE Merge(A[], L, mid, R)....FOR i = 1 TO k A[L + i – 1] = B[i]RETURN
![Page 31: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/31.jpg)
Bottom-up merge sort
It is an improvement of recursive merge sort. The bottom-up merge sort works iteratively,
merge each two consecutive pieces repeatedly.
For first iteration, each piece has size 1. For second iteration, each piece has size 2. For third iteration, each piece has size 4. and so on...
![Page 32: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/32.jpg)
Bottom-up merge sort
PROCEDURE BottomUpMergeSort(A[], n)UseTemp = FalseSize = 1WHILE Size < n L = 1 WHILE L < n IF UseTemp THEN BottomUpMerge(B, A, L, Size, n) ELSE BottomUpMerge(A, B, L, Size, n)
![Page 33: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/33.jpg)
Bottom-up merge sort
PROCEDURE BottomUpMergeSort(A[], n) .... L = L + Size * 2 Size = Size * 2 UseTemp = NOT UseTempIF UseTemp THEN FOR i = 1 TO n A[i] = B[i]RETURN
![Page 34: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/34.jpg)
Bottom-up merge sort
PROCEDURE BottomUpMerge(A[], B[], L, Size, n)i = Lj = L + Sizemid = j – 1R = mid + Sizek = L – 1....
![Page 35: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/35.jpg)
Bottom-up merge sort
PROCEDURE BottomUpMerge(A[], B[], L, Size, n)....WHILE (i <= mid) AND (j <= R) AND (j <= n) k = k + 1 IF A[i] < A[j] THEN B[k] = A[i] ELSE B[k] = A[j]....
![Page 36: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/36.jpg)
Bottom-up merge sort
PROCEDURE BottomUpMerge(A[], B[], L, Size, n)....WHILE i <= mid k = k + 1 B[k] = A[i]WHILE (j <= R) AND (j <= n) k = k + 1 B[k] = A[j]RETURN
![Page 37: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/37.jpg)
Quick sort
The steps of the algorithm are: Pick an item, called a pivot, from the list. Reorder the list so that all items which are
less than the pivot come before the pivot and so that all items greater than the pivot come after it (equal values can go either way). This step is called partition operation.
Recursively sort the sub-list of lesser items and the sub-list of greater items.
![Page 38: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/38.jpg)
Quick sort
PROCEDURE QuickSort(A[], L, R)IF L < R THEN PivotIndex = Partition(A, L, R, (L + R) div 2) QuickSort(A, L, PivotIndex – 1) QuickSort(A, PivotIndex + 1, R)RETURN
![Page 39: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/39.jpg)
Quick sort
FUNCTION Partition(A[], L, R, PivotIndex)Pivot = A[PivotIndex]swap(A[PivotIndex], A[R])StoreIndex = LFOR i = L TO R – 1 IF A[i] <= Pivot THEN swap(A[i], A[StoreIndex]) StoreIndex = StoreIndex + 1swap(A[StoreIndex], A[R])RETURN StoreIndex
< Pivot Pivot > Pivot
![Page 40: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/40.jpg)
More efficient quick sort
PROCEDURE QuickSort(A[], L, R)IF L < R THEN Pivot = A[(L + R) div 2] mid = AlternativePartition(A, L, R, Pivot) QuickSort(A, L, mid) QuickSort(A, mid + 1, R)RETURN
![Page 41: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/41.jpg)
More efficient quick sort
FUNCTION AlternativePartition(A[], L, R, Pivot)i = Lj = RREPEAT WHILE A[i] < Pivot i = i + 1 WHILE A[j] > Pivot j = j – 1 ....
≤ Pivot ≥ Pivot
![Page 42: Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.](https://reader034.fdocuments.in/reader034/viewer/2022050714/56649d215503460f949f6017/html5/thumbnails/42.jpg)
More efficient quick sort
FUNCTION AlternativePartition(A[], L, R, Pivot) .... IF i <= j THEN swap(A[i], A[j]) i = i + 1 j = j – 1UNTIL i > jRETURN j