Searching an Array: Linear and Binary Search Spring 2013Programming and Data Structure1.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSE Lecture 3 – Algorithms I
Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.
Searching and Sorting Algorithms Based on D. S
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
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.
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Sorting Algorithms Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
(c) , University of Washington
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.
Functions A Problem Suppose we are writing a program that displays messages on the screen. We want to display rows of ======================== =======
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Searching and Sorting Gary Wong.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
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.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
CSC 211 Data Structures Lecture 13
EFFICIENCY & SORTING II CITS Scope of this lecture Quicksort and mergesort Performance comparison.
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.
C Programming Week 10 Sorting Algorithms 1. Sorting In many queries we handle a list of values and want a specific value. We can go through all the list.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
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.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
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)
1 Sorting. 2 Sorting Data Items Consider a set of data items  Each item may have more than one field Example: a student record with name, roll no, CGPA,…
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
ARRAYS Lecture void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp;
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
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.
Recursion Riley Chapter 14 Sections 14.1 – 14.5 (14.6 optional)
Programming and Data Structures
Quick Sort and Merge Sort
Sorting and Searching Sudeshna Sarkar 7th Feb 2017.
Teach A level Computing: Algorithms and Data Structures
Unit IV : Searching and sorting Techniques
ARRAYS Lecture
CSC215 Lecture Algorithms.
Chapter 4.
UNIT – IV Searching – Linear search - binary search Sorting
Workshop for CS-AP Teachers
Presentation transcript:

Searching an Array: Linear and Binary Search Spring 2013Programming and Data Structure1

Searching Check if a given element (key) occurs in the array. Two methods to be discussed: – If the array elements are unsorted. Linear search – If the array elements are sorted. Binary search Spring 2013Programming and Data Structure2

Linear Search Basic idea: – Start at the beginning of the array. – Inspect every element to see if it matches the key. Time complexity: – A measure of how long an algorithm takes to run. – If there are n elements in the array: Best case: match found in first element (1 search operation) Worst case: no match found, or match found in the last element (n search operations) Average: (n + 1) / 2 search operations Spring 2013Programming and Data Structure3

Contd. /* If key appears in a[0..size-1], return its location, pos, s.t. a[pos] == key. If key is not found, return -1 */ int linear_search (int a[], int size, int key) { int pos = 0; while ((pos < size) && (a[pos] != key)) pos++; if (pos<n) return pos; /* Return the position of match */ return -1; /* No match found */ } Spring 2013Programming and Data Structure4

Contd. int x[ ]= {12, -3, 78, 67, 6, 50, 19, 10} ; Trace the following calls : search (x, 8, 6) ; search (x, 8, 5) ; Spring 2013Programming and Data Structure5 Returns 4 Returns -1

Binary Search Binary search works if the array is sorted. – Look for the target in the middle. – If you don’t find it, you can ignore half of the array, and repeat the process with the other half. In every step, we reduce the number of elements to search in by half. Spring 2013Programming and Data Structure6

The Basic Strategy What do we want? – Look at [(L+R)/2]. Move L or R to the middle depending on test. – Repeat search operation in the reduced interval. Spring 2013Programming and Data Structure7 0 x: n-1 LR x[m]>key no yes Elements in Ascending order m <=key L R >key R L

Contd. /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( ____________ ) { __________________; } _________________ ; } Spring 2013Programming and Data Structure8

The basic search iteration /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( ____________ ) { mid = (L + R) / 2; if (x[mid] > key) R = mid; else L = mid; } _________________ ; } Spring 2013Programming and Data Structure9

Loop termination /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( L+1 != R ) { mid = (L + R) / 2; if (x[mid] <= key) L = mid; else R = mid; } _________________ ; } Spring 2013Programming and Data Structure10

Return result /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( L+1 != R ) { mid = (L + R) / 2; if (x[mid] <= key) L = mid; else R = mid; } if (L >= 0 && x[L] = = key) return L; else return -1; } Spring 2013Programming and Data Structure11

Initialization /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; L = -1; R = size; while ( L+1 != R ) { mid = (L + R) / 2; if (x[mid] <= key) L = mid; else R = mid; } if (L >= 0 && x[L] = = key) return L; else return -1; } Spring 2013Programming and Data Structure12

Binary Search Examples Spring 2013Programming and Data Structure Trace : binsearch (x, 9, 3); binsearch (x, 9, 145); binsearch (x, 9, 45); Sorted array L= -1; R= 9; x[4]=12; L= -1; R=4; x[1]= -5; L= 1; R=4; x[2]=3; L=2; R=4; x[3]=6; L=2; R=3; return L; We may modify the algorithm by checking equality with x[mid].

Is it worth the trouble ? Suppose there are 1000 elements. Ordinary search – If key is a member of x, it would require 500 comparisons on the average. Binary search – after 1st compare, left with 500 elements. – after 2nd compare, left with 250 elements. – After at most 10 steps, you are done. Spring 2013Programming and Data Structure14

Time Complexity If there are n elements in the array. – Number of searches required: log 2 n For n = 64 (say). – Initially, list size = 64. – After first compare, list size = 32. – After second compare, list size = 16. – After third compare, list size = 8. – ……. – After sixth compare, list size = 1. Spring 2013Programming and Data Structure15 log 2 64 = 6 2 k = n, Where k is the number of steps.

Sorting : the basic problem Given an array x[0], x[1],..., x[size-1] reorder entries so that x[0] <= x[1] <=... <= x[size-1] List is in non-decreasing order. We can also sort a list of elements in non- increasing order. Spring 2013Programming and Data Structure16

Example Original list: – 10, 30, 20, 80, 70, 10, 60, 40, 70 Sorted in non-decreasing order: – 10, 10, 20, 30, 40, 60, 70, 70, 80 Sorted in non-increasing order: – 80, 70, 70, 60, 40, 30, 20, 10, 10 Spring 2013Programming and Data Structure17

Sorting Problem What we want : Data sorted in order Spring 2013Programming and Data Structure18 Unsorted list x: 0 size-1 Sorted list

Selection Sort General situation : Spring 2013Programming and Data Structure19 remainder, unsortedsmallest elements, sorted 0 size-1 k Step : Find smallest element, mval, in x[k..size-1] Swap smallest element with x[k], then increase k. x: 0 ksize-1mval swap

Subproblem /* Yield location of smallest element in x[k.. size-1];*/ int min_loc (int x[ ], int k, int size) { int j, pos; /* x[pos] is the smallest element found so far */ pos = k; for (j=k+1; j<size; j++) if (x[j] < x[pos]) pos = j; return pos; } Spring 2013Programming and Data Structure20

The main sorting function /* Sort x[0..size-1] in non-decreasing order */ int selsort (int x[], int size) { int k, m; for (k=0; k<size-1; k++) { m = min_loc(x, k, size); temp = a[k]; a[k] = a[m]; a[m] = temp; } Spring 2013Programming and Data Structure21

Example Spring 2013Programming and Data Structure x: x: x: x: x: x: x: x:

Analysis How many steps are needed to sort n things ? – Total number of steps proportional to n 2 – No. of comparisons? – Worst Case? Best Case? Average Case? Spring 2013Programming and Data Structure23 (n-1)+(n-2)+……+1= n(n-1)/2 Of the order of n 2

Insertion Sort General situation : Spring 2013Programming and Data Structure24 0 size-1 i remainder, unsortedsmallest elements, sorted 0 size-1 i x: i j Compare and Shift till x[i] is larger.

Insertion Sorting void InsertSort (int list[], int size) { for (i=1; i<size; i++) { item = list[i] ; for (j=i-1; (j>=0)&& (list[j] > i); j--) list[j+1] = list[j]; list[j+1] = item ; } Spring 2013Programming and Data Structure25

Insertion Sort #define MAXN 100 void InsertSort (int list[MAXN], int size) ; main (){ int index, size; int numbers[MAXN]; /* Get Input */ size = readarray (numbers) ; printarray (numbers, size) ; InsertSort (numbers, size) ; printarray (numbers, size) ; } Spring 2013Programming and Data Structure26

Time Complexity Number of comparisons and shifting: o Worst Case? …… +(n-1) = n(n-1)/2 o Best Case? 1+1+…… (n-1) times = (n-1) Spring 2013Programming and Data Structure27

Bubble Sort Spring 2013Programming and Data Structure28 In every iteration heaviest element drops at the bottom. The bottom moves upward.

Bubble Sort Spring 2013Programming and Data Structure29 #include void swap(int *x,int *y) { int tmp=*x; *x=*y; *y=tmp; } void bubble_sort(int x[],int n) { int i,j; for(i=n-1;i>0;i--) for(j=0;j<i;j++) if(x[j]>x[j+1]) swap(&x[j],&x[j+1]); }

Contd. Spring 2013Programming and Data Structure30 int main(int argc, char *argv[]) { int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50}; int i; for(i=0;i<12;i++) printf("%d ",x[i]); printf("\n"); bubble_sort(x,12); for(i=0;i<12;i++) printf("%d ",x[i]); printf("\n"); }

Time Complexity Number of comparisons : o Worst Case? …… +(n-1) = n(n-1)/2 o Best Case? same. How do you make best case with (n-1) comparisons only? Spring 2013Programming and Data Structure31

Common pitfalls with arrays in C Exceeding the array bounds int array[10]; for (i=0; i<=10; i++) array[i] = 0; C does not support array declarations with variable expressions. void fun (int array, int size) { int temp[size] ;... } Spring 2013Programming and Data Structure32

Some Efficient Sorting Algorithms Two of the most popular sorting algorithms are based on divide-and-conquer approach. – Quick sort – Merge sort Basic concept: sort (list) { if the list has length greater than 1 { Partition the list into lowlist and highlist; sort (lowlist); sort (highlist); combine (lowlist, highlist); } } Spring 2013Programming and Data Structure33

Quicksort At every step, we select a pivot element in the list (usually the first element). – We put the pivot element in the final position of the sorted list. – All the elements less than or equal to the pivot element are to the left. – All the elements greater than the pivot element are to the right. Spring 2013Programming and Data Structure34

Partitioning Spring 2013Programming and Data Structure35 0 size-1 x: pivot Values smallerValues greater Perform partitioning Perform partitioning

Quick Sort: Example Spring 2013Programming and Data Structure36 #include void print(int x[],int low,int high) { int i; for(i=low;i<=high;i++) printf(" %d", x[i]); printf("\n"); } void swap(int *a,int *b) { int tmp=*a; *a=*b; *b=tmp; }

Contd. Spring 2013Programming and Data Structure37 void partition(int x[],int low,int high) { int i=low+1,j=high; int pivot=x[low]; if(low>=high) return; while(i<j) { while ((x[i]<pivot) && (i<high)) i++; while ((x[j]>=pivot) && (j>low)) j--; if(i<j) swap(&x[i],&x[j]); } if (j==high) { swap(&x[j],&x[low]); partition(x,low,high-1); } else if (i==low+1) partition(x,low+1,high); else { swap(&x[j],&x[low]); partition(x,low,j-1); partition(x,j+1,high); }

Contd: Spring 2013Programming and Data Structure38 int main(int argc,char *argv[]) { int *x; int i=0; int num; num=argc-1; x=(int *) malloc(num * sizeof(int)); for(i=0;i<num; i++) x[i]=atoi(argv[i+1]); printf("Input: "); for(i=0; i<num; i++) printf(" %d", x[i]); printf("\n"); partition(x,0,num-1); printf("Output: "); for(i=0; i<num; i++) printf(" %d", x[i]); printf("\n"); }

Trace of Partitioning Spring 2013Programming and Data Structure39./a.out Input: Output:

Time Complexity Partitioning with n elements. - No. of comparisons: n-1 Worst Case Performance: (n-1)+(n-2)+(n-3)+………. +1= n(n-1)/2 Best Case performance: (n-1)+2((n-1)/2-1)+4(((n-1)/2-1)-1)/2-1).. k steps = O(n. log(n)) Spring 2013Programming and Data Structure40 2 k =n Choice of pivot element affects the time complexity.

Merge Sort Spring 2013Programming and Data Structure41 Input Array Part-I Part-II Part-I Part-II Part-I Part-II Split Merge Sorted arrays

Merging two sorted arrays Spring 2013Programming and Data Structure42 0 Sorted Array 0 lm a b Merged sorted array 0 l+m-1 c papa pbpb Move and copy elements pointed by p a if its value is smaller than the element pointed by p b in (l+m-1) operations and otherwise.

Merge Sort Spring 2013Programming and Data Structure43 #include main() { int i, num; int a[ ]={-56,23,43,-5,-3,0,123,-35,87,56,75,80}; for(i=0;i<12;i++) printf("%d ",a[i]); printf("\n"); merge_sort(a, 12); printf(“\n The sorted sequence follows \n"); for(i=0;i<12;i++) printf("%d ",a[i]); printf("\n"); }

/* Recursive function for dividing an array a[0..n-1] into two halves and sort them and merge them subsequently. */ Spring 2013Programming and Data Structure44 void merge_sort(int *a,int n) { int i,j,l,m; int *b, *c; if(n>1){ l=n/2; m=n-l; b=(int *) calloc(l,sizeof(int)); c=(int *) calloc(m,sizeof(int)); for(i=0;i<l;i++) b[i]=a[i]; for(j=l;j<n;j++) c[j-l]=a[j]; merge_sort(b,l); merge_sort(c,m); merge(b,c,a,l,m); free(b); free(c); }

/* Merging of two sorted arrays a[0..m-1] and b[0..n-1] into a single array c[0..m+n-1] */ Spring 2013Programming and Data Structure45 void merge(int *a,int *b,int *c, int m,int n) {int i,j,k,l; i=j=k=0; do{ if(a[i]<b[j]){ c[k]=a[i]; i=i+1; } else{ c[k]=b[j]; j=j+1;} k++; } while((i<m)&&(j<n)); if(i==m){ for(l=j;l<n;l++){ c[k]=b[l]; k++;} } else{ for(l=i;l<m;l++){c[k]=a[l]; k++;} } Pointer movement and copy operations.

Splitting Trace Spring 2013Programming and Data Structure Worst Case: O(n.log(n))