Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

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

3 Linear search FUNCTION LinearSearch(A[], n, x) FOR i = 1 TO n IF A[i] = x THEN RETURN i RETURN 0 This algorithm is unstructured.

4 Linear search FUNCTION LinearSearch(A[], n, x) Pos = 0 i = 1 WHILE (Pos = 0) AND (i <= n) IF A[i] = x THEN Pos = i ELSE i = i + 1 RETURN Pos

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

6 Improved linear search FUNCTION ImprovedLinearSearch(A[], n, x) i = 1 WHILE (i <= n) AND (A[i] < x) i = i + 1 IF (i <= n) AND (A[i] = x) THEN Pos = i ELSE Pos = 0 RETURN Pos

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

8 Binary search FUNCTION BinarySearch(A[], n, x) Pos = 0 L = 1 R = n WHILE (Pos = 0) AND (L <= R) mid = (L + R) div 2 IF A[mid] = x THEN Pos = mid....

9 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 half RETURN Pos

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

11 Improved binary search FUNCTION ImprovedBinarySearch(A[], n, x) L = 1 R = n + 1 WHILE L < R mid = L + ((R – L) div 2) IF A[mid] < x THEN L = mid + 1 ELSE R = mid

12 Improved binary search FUNCTION ImprovedBinarySearch(A[], n, x).... IF (L <= n) AND (A[L] = x) THEN Pos = L ELSE Pos = 0 RETURN Pos

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

14 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> xx... ≤ xx> x...

15 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] = value RETURN

16 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).

17 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

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

19 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 UNTIL NOT Swapped RETURN

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

21 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 – 1 UNTIL NOT Swapped RETURN

22 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

23 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

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

25 Merge sort

26 Merge sort works efficiently in sorting a linked list, but needs temporary memory of size n while sorting an array.

27 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

28 Merge sort PROCEDURE Merge(A[], L, mid, R) i = L j = mid + 1 k = 0 WHILE (i <= mid) AND (j <= R) k = k + 1 IF A[i] <= A[j] THEN B[k] = A[i] ELSE B[k] = A[j]

29 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]....

30 Merge sort PROCEDURE Merge(A[], L, mid, R).... FOR i = 1 TO k A[L + i – 1] = B[i] RETURN

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

32 Bottom-up merge sort PROCEDURE BottomUpMergeSort(A[], n) UseTemp = False Size = 1 WHILE Size < n L = 1 WHILE L < n IF UseTemp THEN BottomUpMerge(B, A, L, Size, n) ELSE BottomUpMerge(A, B, L, Size, n)

33 Bottom-up merge sort PROCEDURE BottomUpMergeSort(A[], n).... L = L + Size * 2 Size = Size * 2 UseTemp = NOT UseTemp IF UseTemp THEN FOR i = 1 TO n A[i] = B[i] RETURN

34 Bottom-up merge sort PROCEDURE BottomUpMerge(A[], B[], L, Size, n) i = L j = L + Size mid = j – 1 R = mid + Size k = L – 1....

35 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]....

36 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

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

38 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

39 Quick sort FUNCTION Partition(A[], L, R, PivotIndex) Pivot = A[PivotIndex] swap(A[PivotIndex], A[R]) StoreIndex = L FOR i = L TO R – 1 IF A[i] <= Pivot THEN swap(A[i], A[StoreIndex]) StoreIndex = StoreIndex + 1 swap(A[StoreIndex], A[R]) RETURN StoreIndex < PivotPivot> Pivot

40 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

41 More efficient quick sort FUNCTION AlternativePartition(A[], L, R, Pivot) i = L j = R REPEAT WHILE A[i] < Pivot i = i + 1 WHILE A[j] > Pivot j = j – 1.... ≤ Pivot≥ Pivot

42 More efficient quick sort FUNCTION AlternativePartition(A[], L, R, Pivot).... IF i <= j THEN swap(A[i], A[j]) i = i + 1 j = j – 1 UNTIL i > j RETURN j


Download ppt "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."

Similar presentations


Ads by Google