1 / 28

CSE1320-002-Fall 2014 Arrays

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

aysel
Download Presentation

CSE1320-002-Fall 2014 Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE1320-002-Fall 2014Arrays Dr. SajibDatta CSE@UTA Sep 15, 2014

  2. Binary Search • Can make it faster. • However, unlike linear search, binary search has a precondition • The sequence must be sorted

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

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

  5. Binary Search

  6. #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; • }

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

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

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

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

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

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

  13. An example 410 9 6 -1 Pick 4, compare with 10

  14. An example 4 10 9 6 -1 Pick 10, compare with 9 Need to swap

  15. An example 4 910 6 -1

  16. An example 4 9 10 6 -1 Pick 10, compare with 6 Need to swap

  17. An example 4 9 6 10 -1

  18. An example 4 9 6 10-1 Pick 10, compare with -1

  19. An example 4 9 6 -1 10 Notice, that 10 is at the right position Now, repeat from the beginning

  20. An example 4 9 6 -110 Compare 1st element with 2nd element

  21. An example 4 96-110 Compare 2nd element with 3rd element Swap needed

  22. An example 4 6 9-110

  23. An example 46 9 -1 10 Compare 3rd element with 4th element Swap needed

  24. An example 46 -1 9 10

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

  26. An example 46-1 910 Now, repeat from the beginning again. But remember, we only need to go up to 3rd this time.

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

  28. 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; }

More Related