Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Binary Search Algorithm: Implementation and Sorting, Slides of Artificial Intelligence

An explanation of the binary search algorithm, including its implementation using javascript code and its application to sort a list using selection sort. The document also discusses the limitations of binary search and introduces insertion sort as an alternative.

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banani
banani 🇮🇳

4.3

(3)

91 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSCI 100
Think Like Computers
Lecture 23
Fall 2008
Last Time …
Lists (arrays)
Implementing algorithms
Sequential search
Binary search
Binary Search
binarySearch(list, target) returns index (if found) or -1
[“Charlie”, “Dave”, “Jack”, “Jerald”, “Jerroll”, “Laura”,
“Lois”, “Winnie”]
Use 3 index variables
left: the leftmost range index (inclusive)
right: the rightmost range index (exclusive)
mid: the middle index for the range (left, right)
Some things to remember:
left < right : the range contains value
Otherwise, nothing Æsearch fail
How Do We Implement It?
[“Charlie”, “Dave”, “Jack”, “Jerald”, “Jerroll”,
“Laura”, “Lois”, “Winnie”]
Compare array[mid] with target
If equal, we found it Æreturn mid
Otherwise, we shrink the range and starts with the
new left, right, mid values (another iteration of the
loop)
array[mid] > target : right = mid
array[mid] < target : left = mid+1;
New mid : Math.floor(( left + right )/2), as always
Example
[“Charlie”, “Dave”, “Jack”, “Jerald”,
“Jerroll”, “Laura”, “Lois”, “Winnie”]
Search for “Zoe”:
left right mid list[mid]
1st try 0 8 4 Jerroll
2nd try 5 8 6 Lois
3rd try 7 8 7 Winnie
4th try 8 8 Æno values, return -1
Exercise
[“Charlie”, “Dave”, “Jack”, “Jerald”,
“Jerroll”, “Laura”, “Lois”, “Winnie”]
Search for “Jones”:
left right mid list[mid]
1st try
2nd try
3rd try
Docsity.com
pf3
pf4

Partial preview of the text

Download Binary Search Algorithm: Implementation and Sorting and more Slides Artificial Intelligence in PDF only on Docsity!

CSCI 100

Think Like Computers

Lecture 23

Fall 2008

Last Time …

  • Lists (arrays)
  • Implementing algorithms
  • Sequential search
  • Binary search

Binary Search

  • binarySearch(list, target) returns index (if found) or -
  • [“Charlie”, “Dave”, “Jack”, “Jerald”, “Jerroll”, “Laura”, “Lois”, “Winnie”]
  • Use 3 index variables Š left: the leftmost range index (inclusive) Š right: the rightmost range index (exclusive) Š mid: the middle index for the range (left, right)
  • Some things to remember: Š left < right : the range contains value Š Otherwise, nothing Æ search fail

How Do We Implement It?

  • [“Charlie”, “Dave”, “Jack”, “Jerald”, “Jerroll”, “Laura”, “Lois”, “Winnie”]
  • Compare array[mid] with target Š If equal, we found it Æ return mid Š Otherwise, we shrink the range and starts with the new left, right, mid values (another iteration of the loop) ƒ array[mid] > target : right = mid ƒ array[mid] < target : left = mid+1; ƒ New mid : Math.floor(( left + right )/2), as always

Example

  • [“Charlie”, “Dave”, “Jack”, “Jerald”, “Jerroll”, “Laura”, “Lois”, “Winnie”]
  • Search for “Zoe”:
  • left right mid list[mid]
  • 1 st^ try 0 8 4 Jerroll
  • 2 nd^ try 5 8 6 Lois
  • 3 rd^ try 7 8 7 Winnie
  • 4 th^ try 8 8 Æ no values, return -

Exercise

  • [“Charlie”, “Dave”, “Jack”, “Jerald”, “Jerroll”, “Laura”, “Lois”, “Winnie”]
  • Search for “Jones”:
  • left right mid list[mid]
  • 1 st^ try
  • 2 nd^ try
  • 3 rd^ try

Writing the Code

  • Begin:
  • var left, right, mid;
  • left = 0; right = list.length;
  • mid = Math.floor( (left+right)/2 );
  • Loop:
  • while ( left < right ) { // items in range
  • }

Writing the Code

  • Inside the loop:
  • (1) compare list[mid] and target
  • if ( list[mid] == target ) return mid;
  • (2) // otherwise, update the range
  • else if ( list[mid] > target ) {
  • right = mid;
  • } else {
  • left = mid+1;
  • }
  • (3) don’t forget the mid:
  • mid = Math.floor( (left+right)/2 );

Writing the Code

  • If loop is finished (did not return during the loop)
  • Then target is not in the list!
  • return -1;
  • That’s it!

How to Sort?

  • Any idea?
  • list = [ 3, 20, 11, 13, 8, 10, 6, 2];

First, How to Swap?

  • You’ll need a temporary variable:
  • var temp;
  • temp = x;
  • x = y;
  • y = temp;
  • Notice the sequence: first save/write, then you can modify (and lose its original value)

Swap List Elements

  • Swap elements at index i and j:
  • var temp;
  • temp = list[i];
  • list[i] = list[j];
  • list[j] = temp;

Put it Together

  • k = 0;
  • while (! k == list.length-1 ) {
  • sequential search the min of list range (k, list.length) Æ index i
  • swap list elements at positions k and i.
  • k = k+1;
  • }

Use Functions for

Frequent Operations

  • Sequential search for min in list range:
  • minIndex(list, begin, end)
  • Swap list elements
  • swap(list, k, i)

Exercise: minIndex(…)

  • function minIndex(list, begin, end) {
  • // list: the input array
  • // begin: the first index of the range (inclusive)
  • // end: the last index of the range (exclusive)
  • // Assumes: end > begin
  • // Returns: the index of the min value
  • }

Better Sorting Algorithms?

  • Selection sort is quite slow.
  • Insertion sort is better…
  • Similar to arranging a hand of cards Š Pick up cards one by one Š Insert the current card at the right position Š The (partial) hand is always sorted Š The whole hand is sorted after inserting the last card

Insertion Sort

  • Partial hand: list range (0, k) Š Start with k= Š Want to grow k, until k==list.length
  • The next card to be picked: list[k]
  • Insert at the right position: Š Compare list[k] with list[k-1]: if less, swap k & k-1. Otherwise, done Š Compare list[k-1] with list[k-2]: if less, swap k-1 & k-2. Otherwise, done Š … Š This would also be a loop!

Insertion Sort

  • k=1;
  • while ( k < list.length ) {
  • j = k;
  • while ( j > 0 ) {
  • if ( list[j] < list [j-1] ) swap(list, j, j-1);
  • else break; // abort the loop since we are done!
  • j = j-1;
  • } // end of inner while loop
  • k = k+1;
  • } // end of outer while

Š This can be improved, because we don’t really need full swaps