280 likes | 433 Views
CSE1320-002-Fall 2014 Arrays. Dr. Sajib Datta CSE@UTA Sep 15, 2014. Binary Search. Can make it faster. However, unlike linear search, binary search has a precondition The sequence must be sorted. Binary Search. Strategy: Compare ‘x’ with the middle element. If equal, we found it
E N D
CSE1320-002-Fall 2014Arrays Dr. SajibDatta CSE@UTA Sep 15, 2014
Binary Search • Can make it faster. • However, unlike linear search, binary search has a precondition • The sequence must be sorted
Binary Search • Strategy: • Compare ‘x’ with the middle element. • If equal, we found it • Else If ‘x’ < middle element, we can discard the half of the elements; all elements that are greater than the middle element and middle element itself • Now search in the remaining half of the array • Similarly, if ‘x’ > middle element, we can discard the half of the elements; all elements that are smaller than the middle element and middle element itself
Online materials for next class • Array • http://www.java2s.com/Tutorial/C/0140__Array/Catalog0140__Array.htm • Sorting and searching • http://www.java2s.com/Tutorial/C/0280__Search-Sort/Catalog0280__Search-Sort.htm • Time complexity • http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=complexity1 • http://www.cs.toronto.edu/~vassos/teaching/c73/handouts/brief-complexity.pdf • http://www.csd.uwo.ca/courses/CS1037a/notes/topic13_AnalysisOfAlgs.pdf (advanced!)
#include <stdio.h> • #define ARRSIZE 7 • intmain(void) • { • intintarr[ARRSIZE], target, i, left, right, mid; • printf("Please input %d integers which are sorted in the ascending order and a target value.", ARRSIZE); • scanf("%d", &target); • for(i = 0; i<ARRSIZE; i++) • { • scanf("%d", &intarr[i]); • } • left = 0; • right = ARRSIZE-1; • while(left <= right) • { • mid = (left+right)/2; • if(intarr[mid] == target) • { • printf("The index of the target in the array is %d.\n", mid); • break; • } • else if (target > intarr[mid]) • { • left = mid + 1; • } • else • { • right = mid - 1; • } • } • if(left > right) • printf("The target is not in the array.\n"); • return 0; • }
Binary Search • How many comparisons on average? • Each time ‘x’ is not found, we are taking the half of the remaining array • If the array has size ‘n’, how many times we can slice it into half?
Assume you can compare at most ‘m’ times • After 1st half: array size n/2 • After 2nd half: array size n/4 • After 2rd half: array size n/8 • …. • …. • After m-th half: array size n/(2m) • Since, the minimum array size is 1 [there has to be at least 1 element] • n/(2m) = 1 => m = log2(n)
Performance • So, in binary search, there are log2(n) comparisons. • If you are given a sorted array of a million integers (220), • Linear search can make a million comparison operations • Binary search will make around 20 comparisons • You will learn more on performance in algorithm classes. This is very important aspect of programming.
Practice problem • You are given an array containing 9 integers, ranging from 1 to 10. Each number between 1 to 10 appears exactly once in the array, but unfortunately one of them is missing. Can you find the missing number?
Sort • Ordering elements in some way • For numeric data, ascending order is the most common • Lots of techniques for sorting • These techniques vary in performance both with runtime and usage of extra memory
Bubble sort • Given a sequence of numbers, it compares adjacent numbers and swap them if necessary • Repeats the above procedure until all numbers are in right place
An example 410 9 6 -1 Pick 4, compare with 10
An example 4 10 9 6 -1 Pick 10, compare with 9 Need to swap
An example 4 910 6 -1
An example 4 9 10 6 -1 Pick 10, compare with 6 Need to swap
An example 4 9 6 10 -1
An example 4 9 6 10-1 Pick 10, compare with -1
An example 4 9 6 -1 10 Notice, that 10 is at the right position Now, repeat from the beginning
An example 4 9 6 -110 Compare 1st element with 2nd element
An example 4 96-110 Compare 2nd element with 3rd element Swap needed
An example 4 6 9-110
An example 46 9 -1 10 Compare 3rd element with 4th element Swap needed
An example 46 -1 9 10
An example 46 -1 9 10 Now, do we need the compare 4th with the 5th? We already know 5th element is the largest. This implies, what we have as the 4th element, it must be the second largest, and is already in the correct place.
An example 46-1 910 Now, repeat from the beginning again. But remember, we only need to go up to 3rd this time.
What is happening inside… • 1st iteration places the largest number in the correct place • 2nd iteration places the second largest number in the correct place • ….. • …. • …. • i-thiteration places the i-th largest number in the correct place
Bubble sort #include<stdio.h> #define ARR_SIZE 8 int main(void) { intintarr[ARR_SIZE] = {23, 4, 12, 8, 22, 1, 54, 9}, i, j, tmp; for(i= 0; i<ARR_SIZE-1; i++) { for(j = 0; j<ARR_SIZE-i-1; j++) { if(intarr[j]>intarr[j+1]) { tmp= intarr[j]; intarr[j] = intarr[j+1]; intarr[j+1] = tmp; } } } printf("The array sorted by Bubble Sort: "); for(i = 0; i< ARR_SIZE; i++) printf("%d ", intarr[i]); return 0; }