Programming-Foundations-Algorithms
Algorithms purpose
to solve a specific proplem with a sequential sets of steps for instance : if you need to add different shaped in groups you can use loop by iterate on each shape if its belong to this group or not
Algorithms charachteristics
- algorithms complixity
- space complixity
- time complixity
- input and output
- classification
- serial/parallel
- exact/approximate
common algorithms
- searching algo find a specific data from a structure
- sorting algo take a set of data and apply a sort order to it
- computational algo given a ser of data calculate another (calculator)
- collection algo work with collection of data : manipulating and navigating amoung sets of data that are sorted (count a specific items) exerciese for an algorithm
def greatest common denomonator (a,b)
while (b != 0)
t=a
a=b
b=t%b
return a
print(20,8)
Algorithm performance
- how an algorithm will be have based on the size of input set data
- big-O to describe algorithm performance as the size of input grows over time it usually describe the worst case senario
Time complixity rank
- O(1) operation in question doesnt depend on the number of elements in the given data set (calculating the number is even or odd)
- O(log n) finding a specific value in a sorted array using a bionary search so if the number of elements grow it takes logarithmic time relation to find any given item
- O(n) searching for an item in an unsorted array as number of items increase it take the corrosponding linear time to complete the search
- O(nlogn) sorting algorithm like stack and merge sort
- O(n2) as the number of data increase the time it take is squared
Overview on Data structure
1: Array
it has either one dimention or multiple , you can calculate
- item index O(1)
- insert or delete at beginning or middle O(n)
- insert or delete at end : O(1)
2: Linked lists(nodes)
- linear collection of data elements each node has a field that refer to the next element in the list
- the benifit of it over arrays is that its fast and easy to add and remove items from the list
- its not necessary to recognize the essintial memory that hold the data because the individual nodes doesnt have to be stored adjecently like arrays
- the linked lists cant do canstant time random access to any item in the list like the array
its
- very simple to understand and implement
- performance O(n2)
- for loops inside of for loops are usually n2
- other sorting algorithms are generally much better
2: merge sort
by
- divide and conquer algorithm
- breaks a dataset into individual pieces and merges them
- uses recursion to operate on datasets its
- performs well on large sets of data
- generally has O(nlogn) performance
3: Quicksort
- divide and conquer algorithm
- uses recursion to operate on datasets
- generally has O(nlogn) performance
- operate in place on the data
- worst case is O(n2) when data is mostly sorted already
searching data
- unordered list search
- ordered list search
- determine if alist is sorted
other algorithms
- filtering hash table
- counting value with hash table
- find max value recusively