Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as...

Post on 18-Dec-2015

220 views 1 download

Transcript of Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as...

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.

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.

Linear search

FUNCTION LinearSearch(A[], n, x)FOR i = 1 TO n IF A[i] = x THEN RETURN iRETURN 0

This algorithm is unstructured.

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

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.

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

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.

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

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

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.

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

Improved binary search

FUNCTION ImprovedBinarySearch(A[], n, x)....IF (L <= n) AND (A[L] = x) THEN Pos = LELSE Pos = 0RETURN Pos

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.

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

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

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

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

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.

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

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.

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

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

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

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.

Merge sort

Merge sort

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

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

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]

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

Merge sort

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

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

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)

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

Bottom-up merge sort

PROCEDURE BottomUpMerge(A[], B[], L, Size, n)i = Lj = L + Sizemid = j – 1R = mid + Sizek = L – 1....

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

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

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.

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

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

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

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

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