Anonymous

Changes

From Pumping Station One
2,609 bytes added ,  07:03, 14 December 2016
no edit summary
Line 17: Line 17:     
Sample Input : [ 8, 3, 9, 0, 2, 7, 4, 5 ]
 
Sample Input : [ 8, 3, 9, 0, 2, 7, 4, 5 ]
 +
 
Sample Output : [ 0, 2, 3, 4, 5, 7, 8, 9 ]
 
Sample Output : [ 0, 2, 3, 4, 5, 7, 8, 9 ]
   Line 61: Line 62:  
###from previous number in array, print in natural order.
 
###from previous number in array, print in natural order.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
'''Binary Sort Algorithm'''
 +
----
 +
 +
'''Binary Sort''' (aka half-interval search/logarithmic search) is a search algorithm that finds a target value within a sorted array by comparing the target to the middle element, if unequal, the half of integers where the target cannot lie is unlimited. This process is continued unless target is identified. In other words, this function will look for an item in an array by dividing the list in half every time it searches.
 +
 +
EX:  1, 7, 27, 33, 45,  57 , 66, 77, 89, 99, 101, 129, 156, 234
 +
  Find: Number 77
 +
      a. Identify the middle value of the sorted array : 66
 +
      b. Eliminate the lower half of the array integers [>66] since the integer we are looking for is 77.
 +
      c. Continue this median allocation and deletion until correct integer in the array is isolated and selected.
 +
 +
 +
 +
'''Example Python Code'''
 +
----
 +
 +
 +
Python Binary Sort Example 1
 +
 +
<syntaxhighlight lang="cpp">
 +
def binarySearch(alist, item):  ### defines the variable binarysearch and states the array alist
 +
  first = 0    ### states the first element in the array
 +
  last = len(alist)- 1  ### states the last element in the array by subtracting 1 from the last integer of the array
 +
  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:  ### this is a while loop that runs the logarithmic structure of the heap sort until the indicated element is found
 +
      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 the midpoint from the previous equation equates to the indicated integer, then element is found
 +
        found = True
 +
 +
      else:
 +
        if item < alist[midpoint]:    ### this is the while loop. if the item is less then the midpoint array
 +
          last = midpoint - 1    ### delete the elements of the array that are less than the midpoint element
 +
        else:
 +
          first = midpoint + 1 ### run search through the elements greater in value than the midpoint.
 +
 +
  return found ### continue this until the element indicated is found.
 +
 +
</syntaxhighlight>
 +
 +
Python Binary Sort Example 2
 +
 +
<syntaxhighlight lang="cpp">
 +
 +
</syntaxhighlight>
 +
 +
'''Heap Sort Algorithm'''
 +
----
    
=== Which Algorithms Are Best Suited for Which Tasks ===
 
=== Which Algorithms Are Best Suited for Which Tasks ===
Line 68: Line 119:     
=== Writing Your Own Algorithms ===
 
=== Writing Your Own Algorithms ===
 +
 +
 +
 +
 +
Reference:
 +
-----
 +
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-854j-advanced-algorithms-fall-2005/
Cookies help us deliver our services. By using our services, you agree to our use of cookies.