This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def binary_search(item, array) | |
| min = 0 | |
| max = array.length | |
| middle = (min + max) / 2 | |
| count = 0 | |
| while min <= max | |
| if array[middle] == item | |
| count += 1 | |
| p "count #{count}" | |
| p "Your item is at index: #{middle}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def linear_search(item, array) | |
| i = 0 | |
| count = 0 | |
| while i < array.length | |
| count += 1 | |
| p "count #{count}" | |
| if array[i] == item | |
| p "Your item is at index: #{i}" | |
| return i | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def linear_search(item, array) | |
| i = 0 | |
| steps = 0 | |
| while i < array.length | |
| steps += 1 | |
| p "steps: #{steps}" | |
| if array[i] == item | |
| p "Your item is at index: #{i}" | |
| return i | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| numbers = [ 1, 2, 3, 4, 5] | |
| numbers.each do |num| | |
| puts num | |
| end | |
| # Will log: | |
| # 1 | |
| # 2 | |
| # 3 |