# Difference between revisions of "Algorithms"

Line 66: | Line 66: | ||

---- | ---- | ||

− | '''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. | + | '''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 | EX: 1, 7, 27, 33, 45, 57 , 66, 77, 89, 99, 101, 129, 156, 234 | ||

Line 104: | Line 104: | ||

</syntaxhighlight> | </syntaxhighlight> | ||

+ | Python Binary Sort Example 2 | ||

+ | |||

+ | <syntaxhighlight lang="cpp"> | ||

## Revision as of 20:46, 13 December 2016

**Algorithms** are a type of technology used in multiple fields of study that dictate or alter streams of information (in this case we will most be referring to computer science). The code/pseuocode presented on here will be written in Python (### syntax in the code represents comments made in Python code).

Case Uses For Implementing Algorithms (Engineering and Computer Science) :

## Contents

### Common Computer Science Algorithms

Algorithms used in Computer Science are developed and implemented to alter the way data moves in network somehow ( the network representing the collection of data used to create a program/system )

The most common present:

**Insertion Sort**

**Insertion Sort** (sorting algorithm for small number of elements): This Algorithm rearranges a disordered list of integers and rearranges them in natural order.
This algorithm is typically used for small array of elements.

Sample Input : [ 8, 3, 9, 0, 2, 7, 4, 5 ]

Sample Output : [ 0, 2, 3, 4, 5, 7, 8, 9 ]

**Example Python Code**

Python Insertion Sort Example 1

<syntaxhighlight lang="cpp"> def insertionSort(alist):

for index in range(1,len(alist)):

currentvalue = alist[index] position = index

while position>0 and alist[position-1]>currentvalue: #print integers in natural order[-1] less than the previous number alist[position]=alist[position-1] position = position-1

alist[position]=currentvalue

alist = [54,26,93,17,77,31,44,55,20] ### this is the array of integers that will be sorted in natural order 0-infinity insertionSort(alist) print(alist) </syntaxhighlight>

Python Insertion Sort Example 2

<syntaxhighlight lang="cpp"> function insertionSort(array A)

for i from 1 to length[A]-1 do value := A[i] j := i-1 while j >= 0 and A[j] > value do A[j+1] := A[j] j := j-1 done A[j+1] = value done

- Syntax states to apply InsertionSort to Array of Integers named A. from 1 to any number -1
- from previous number in array, print in natural order.

</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">

**Heap Sort Algorithm**