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.

Slides:



Advertisements
Similar presentations
CS4413 Divide-and-Conquer
Advertisements

Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
CMPS1371 Introduction to Computing for Engineers SORTING.
Computation for Physics 計算物理概論 Algorithm. An algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing,
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
CHAPTER 11 Sorting.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Computer Programming Sorting and Sorting Algorithms 1.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Analysis of Algorithms CS 477/677
Searching Arrays Linear search Binary search small arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Computer Science Searching & Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
CSC 211 Data Structures Lecture 13
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Data Structure Introduction.
Sort Algorithms.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
1 Overview Divide and Conquer Merge Sort Quick Sort.
The Linear and Binary Search and more Lecture Notes 9.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Searching and Sorting Algorithms
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Teach A level Computing: Algorithms and Data Structures
Adapted from slides by Marty Stepp and Stuart Reges
Algorithms Chapter 3 With Question/Answer Animations
Bubble, Selection & Insertion sort
CSc 110, Spring 2017 Lecture 39: searching.
CSC215 Lecture Algorithms.
CSc 110, Spring 2017 Lecture 39: searching and sorting
Adapted from slides by Marty Stepp and Stuart Reges
Search,Sort,Recursion.
CSE 373 Data Structures and Algorithms
Presentation transcript:

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 i RETURN 0 This algorithm is unstructured.

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

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

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 = 0 L = 1 R = n WHILE (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 half RETURN 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 = 1 R = n + 1 WHILE 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 = L ELSE Pos = 0 RETURN 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> xx... ≤ xx> 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] = value RETURN

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

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

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

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

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

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

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

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 = L j = R REPEAT WHILE A[i] < Pivot i = i + 1 WHILE A[j] > Pivot j = j – ≤ 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 – 1 UNTIL i > j RETURN j