Line 83: |
Line 83: |
| | | |
| <syntaxhighlight lang="cpp"> | | <syntaxhighlight lang="cpp"> |
− | def binarySearch(alist, item): | + | def binarySearch(alist, item): ### defines the variable binarysearch and states the array alist |
− | first = 0 | + | first = 0 ### states the first element in the array |
− | last = len(alist)- 1 | + | last = len(alist)- 1 ### states the last element in the array by subtracting 1 from the last integer of the array |
− | found = False | + | found = False ### if the element being identified is found before the last element of the array, the last element is found as false |
| | | |
− | while first <=last and not found: | + | while first <=last and not found: ### this is a while loop that runs the logarithmic structure of the heap sort until the indicated element is found |
− | midpoint = (first + last)// 2 | + | midpoint = (first + last)// 2 ### this is the logic equation of the python code to run, this finds the midpoint element of the array by adding the first and last integer of that array and diving it by 2 |
− | if alist[midpoint] == item | + | if alist[midpoint] == item ### if the midpoint from the previous equation equates to the indicated integer, then element is found |
| found = True | | found = True |
| | | |
− | else: | + | else: |
− | if item < alist[midpoint]: | + | if item < alist[midpoint]: ### this is the while loop. if the item is less then the midpoint array |
− | last = midpoint - 1 | + | last = midpoint - 1 ### delete the elements of the array that are less than the midpoint element |
| else: | | else: |
− | first = midpoint + 1 | + | first = midpoint + 1 ### run search through the elements greater in value than the midpoint. |
| | | |
− | return found | + | return found ### continue this until the element indicated is found. |
| | | |
| </syntaxhighlight> | | </syntaxhighlight> |