1. Data Structures and Algorithms – Alexander Borsuk – October 2016
Contents
Sorting..............................................................................................................................................4
Quick Sort......................................................................................................................................5
Insertion Sort..................................................................................................................................7
Merge Sort.....................................................................................................................................8
Bubble Sort..................................................................................................................................10
Heap...............................................................................................................................................11
Heapify .......................................................................................................................................14
Test Heapify ...............................................................................................................................15
Heap Sort .......................................................................................................................................16
Merge Two Heaps......................................................................................................................20
Search............................................................................................................................................20
Binary Search................................................................................................................................21
Fibonacci Search...........................................................................................................................21
Binary Search Tree........................................................................................................................22
Search BST................................................................................................................................24
Print Binary Search Tree............................................................................................................25
Delete Node in BST ...................................................................................................................26
Priority Queue ................................................................................................................................27
Testing Priority Queue ...............................................................................................................29
AVL Tree ........................................................................................................................................30
Find Max Min Values .....................................................................................................................31
Using Heap.................................................................................................................................31
Using Recursion.........................................................................................................................32
Using BST ..................................................................................................................................33
Tree Traversal................................................................................................................................34
Depth First Traversals: Preorder Traversal / In order Traversal / Post order
Traversal.....................................................................................................................................34
In-Order...................................................................................................................................34
Post-Order..............................................................................................................................35
Pre-Order................................................................................................................................36
Reverse In-Order (Max to Min)...............................................................................................37
Breadth-First Traversal of a Tree...............................................................................................37
2. Data Structures and Algorithms – Alexander Borsuk – October 2016
Graphs (Vertex / Nodes, Edges) ...................................................................................................39
Dijkstra’s [Daekstras] Algorithm.................................................................................................42
Dijkstra’s [Daekstras] Algorithm Implementation (C#)...............................................................46
Create Graph with Adjacency List .............................................................................................47
Depth First Traversal..................................................................................................................50
Breadth First Traversal...............................................................................................................52
How Many Routes between N people? ..............................................................................................54
Linked List......................................................................................................................................55
Doubly linked vs. singly linked ...................................................................................................55
Circularly linked vs. linearly linked .............................................................................................55
Speeding up search................................................................................................................57
Hash Table.....................................................................................................................................58
Collision Resolution.......................................................................................................................59
Array Algorithms ..............................................................................................................................60
Shift Array Right............................................................................................................................60
Shift Array Left..............................................................................................................................60
BitArray C#...................................................................................................................................60
Removing Duplicates from Array....................................................................................................61
Find smallest andlargest number in Array......................................................................................61
Two Arrays Intersectionsfor SORTED ARRAY..................................................................................62
Print Union of two SORTED ARRAYS...............................................................................................62
Find Union and Intersection of two unsorted arrays........................................................................63
Print all possible sub arrays of array...............................................................................................64
Print all permutation of the array...................................................................................................64
FindAll Palindromes of Array.........................................................................................................65
Find all Duplicates in Array ............................................................................................................66
Matrix Algorithms.............................................................................................................................67
Rotate Matrix 90 Degrees Clockwise..............................................................................................67
Convert Bases (Hex->Oct->Bin->Dec) .................................................................................................67
Algorithm White Boarding Review.....................................................................................................69
SOLID...............................................................................................................................................72
5. Data Structures and Algorithms – Alexander Borsuk – October 2016
Quick Sort
https://www.tutorialspoint.com/data_structures_algorithms/quick_sort_algorithm.htm
In-place, don't need additional storage, Worst case: O (n2), Best Case: O (log n)
7. Data Structures and Algorithms – Alexander Borsuk – October 2016
Insertion Sort
8. Data Structures and Algorithms – Alexander Borsuk – October 2016
Merge Sort
Best case: O (n log n), Best case: O (log n), Merge Sort is stable, It won't reorder
elements that are equal
9. Data Structures and Algorithms – Alexander Borsuk – October 2016
1. Divide arrayby 2 recursively
2. Merge (all sub arrays are sorted
alreadycause startedfromsize 1:
a. BuildTempSORTED array
b. If First array smallerthan
secondjustput the restof
secondto temparray
c. If opposite soopposite
d. Aftertempsortedarray build
-> put everythingbackto
original array
10. Data Structures and Algorithms – Alexander Borsuk – October 2016
Bubble Sort
11. Data Structures and Algorithms – Alexander Borsuk – October 2016
Heap
When a heap is a complete binary tree, it has a smallest possible height—a heap with N nodes
always has log N height. A heap is a useful data structure when you need to remove the object with
the highest (or lowest) priority.
HEAP MUST BE BUILD FROM LEFT TO RIGHT, NO EMPTY SLOTS ALLOWED
12. Data Structures and Algorithms – Alexander Borsuk – October 2016
Build Heap from array
13. Data Structures and Algorithms – Alexander Borsuk – October 2016
MinHeapify from last parent node: n / 2
11 / 2 = 5
Loop for ALL parents calculate children: n*2, n*2 + 1 (starts fromindex 1)
{
Find Max Value fromBoth Children
{
If(Min Value not a Parent)
{
SWAP(Parent, Min Value);
Call Heapify Again fromLargest;
}
}
}
We got Min Heap Array:
20. Data Structures and Algorithms – Alexander Borsuk – October 2016
Merge Two Heaps
If both heaps are empty, return an empty heap. If one heap is empty but the other is not,
return the nonempty heap. If both heaps are nonempty, convert them into lists, combine the
lists, and heapify the resulting list. If you're clever about how you do that combination, this
can be very efficient.
Search
21. Data Structures and Algorithms – Alexander Borsuk – October 2016
Binary Search
O(Log(n))
Justif data issorted
Fibonacci Search
O(Log(n))
The Fibonacci searchtechnique is a methodof searchinga sortedarrayusinga divide andconquer algorithmthat narrows
down possible locations withthe aid ofFibonacci numbers. Compared to binarysearch, Fibonacci searchexamines locations
whose addresses have lower dispersion. Therefore, whenthe elements being searched have non-uniformaccessmemory
storage (i.e., the time neededto accessa storage locationvaries dependingon the location previouslyaccessed), the Fibonacci
search hasanadvantage over binarysearch inslightlyreducing the average time neededto access a storage location.
22. Data Structures and Algorithms – Alexander Borsuk – October 2016
Binary Search Tree
Looking up a node in a binary tree is O(log(n)) because the tree has log(n) levels (each level
holds twice as much as the level above it). Therefore to create/insert n elements into a
binary tree it's O(nlog(n)).
While Building BST we can rememberMAX or MIN to get this values
faster.We can use HASH table (or CACHE) to cache lastnode to
access immediately.
23. Data Structures and Algorithms – Alexander Borsuk – October 2016
Clean Tree
Add Node and Create Tree
25. Data Structures and Algorithms – Alexander Borsuk – October 2016
Print Binary Search Tree
Deleting from original array and re-build the tree
26. Data Structures and Algorithms – Alexander Borsuk – October 2016
Delete Node in BST
27. Data Structures and Algorithms – Alexander Borsuk – October 2016
Priority Queue
Priority Queue is more specialized data structure than Queue. Like ordinary queue, priority
queue has same method but with a major difference. In Priority queue items are ordered by
key value so that item with the lowest value of key is at front and item with the highest value
of key is at rear or vice versa. So we're assigned priority to item based on its key value.
Lower the value, higher the priority. Following are the principal methods of a Priority Queue.
29. Data Structures and Algorithms – Alexander Borsuk – October 2016
Testing Priority Queue
30. Data Structures and Algorithms – Alexander Borsuk – October 2016
AVL Tree
Named after their inventor Adelson, Velski & Landis, AVL trees are height balancing
binary search tree. AVL tree checks the height of the left and the right sub-trees and
assures that the difference is not more than 1. This difference is called the Balance
Factor.
Here we see that the first tree is balanced and the next two trees are not balanced −
In the second tree, the left subtree of C has height 2 and the right subtree has height 0,
so the difference is 2. In the third tree, the right subtree of A has height 2 and the left is
missing, so it is 0, and the difference is 2 again. AVL tree permits difference (balance
factor) to be only 1.
31. Data Structures and Algorithms – Alexander Borsuk – October 2016
Find Max Min Values
Using Heap
1. Build Heap – O(n)
2. Heapify – O(log n)
3. Print Min – O(1)
32. Data Structures and Algorithms – Alexander Borsuk – October 2016
Using Recursion
33. Data Structures and Algorithms – Alexander Borsuk – October 2016
Using BST
1. Build Tree O(log n)
2. Print Min / Max if Cached O(1)
3. If not Cached Find Min / Max O(n)
! Min Value may be create and cached once building the tree
34. Data Structures and Algorithms – Alexander Borsuk – October 2016
Tree Traversal
All four traversals require O (n) time as they visit every node exactly once.
Depth First Traversals: Preorder Traversal / In order Traversal / Post
order Traversal
In-Order
35. Data Structures and Algorithms – Alexander Borsuk – October 2016
! If Tree is BST it will print sorted array:
Post-Order
54. Data Structures and Algorithms – Alexander Borsuk – October 2016
How Many Routes between N people?
55. Data Structures and Algorithms – Alexander Borsuk – October 2016
Linked List
Doubly linked vs. singly linked
Double-linked lists require more space per node (unless one uses XOR-linking), and their elementary
operations are more expensive; but they are often easier to manipulate because they allow fast and easy
sequential access to the list in both directions. In a doubly linked list, one can insert or delete a node in a
constant number of operations given only that node's address. To do the same in a singly linked list, one
must have the address of the pointer to that node, which is either the handle for the whole list (in case of
the first node) or the link field in the previous node. Some algorithms require access in both directions. On
the other hand, doubly linked lists do not allow tail-sharing and cannot be used as persistent data
structures.
Circularly linked vs. linearly linked
A circularly linked list may be a natural option to represent arrays that are naturally circular, e.g. the
corners of a polygon, a pool of buffers that are used and released in FIFO ("first in, first out") order, or a
set of processes that should be time-shared in round-robin order. In these applications, a pointer to any
node serves as a handle to the whole list.
With a circular list, a pointer to the last node gives easy access also to the first node, by following one
link. Thus, in applications that require access to both ends of the list (e.g., in the implementation of a
queue), a circular structure allows one to handle the structure by a single pointer, instead of two.
A circular list can be split into two circular lists, in constant time, by giving the addresses of the last node
of each piece. The operation consists in swapping the contents of the link fields of those two nodes.
Applying the same operation to any two nodes in two distinct lists joins the two list into one. This property
greatly simplifies some algorithms and data structures, such as the quad-edge and face-edge.
The simplest representation for an empty circular list (when such a thing makes sense) is a null pointer,
indicating that the list has no nodes. Without this choice, many algorithms have to test for this special
case, and handle it separately. By contrast, the use of null to denote an empty linear list is more natural
and often creates fewer special cases.
57. Data Structures and Algorithms – Alexander Borsuk – October 2016
Speeding up search
Finding a specific element in a linked list, even if it is sorted, normally requires O(n) time (linear search).
This is one of the primary disadvantages of linked lists over other data structures. In addition to the
variants discussed above, below are two simple ways to improve search time.
In an unordered list, one simple heuristic for decreasing average search time is the move-to-front
heuristic, which simply moves an element to the beginning of the list once it is found. This scheme, handy
for creating simple caches, ensures that the most recently used items are also the quickest to find again.
Another common approach is to "index" a linked list using a more efficient external data structure. For
example, one can build a red-black tree or hash table whose elements are references to the linked list
nodes. Multiple such indexes can be built on a single list. The disadvantage is that these indexes may
need to be updated each time a node is added or removed (or at least, before that index is used again).
58. Data Structures and Algorithms – Alexander Borsuk – October 2016
Hash Table
In previous sections we were able to make improvements in our search algorithms by taking advantage of information about w here
items are stored in the collection w ith respect to one another. For example, by know ing that a list w as ordered, we could search in
logarithmic time using a binary search. In this section w e w illattempt to go one step further by building a data structure that can be
searched in (O(1)) time. This concept is referred to as hashing.
In order to do this, w e willneed to know even more about w here the items might be w hen we go to look for them in the collection. If
every item is w here it should be, then the search can use a single comparison to discover the presence of an item. We w illsee,
how ever, that this is typically not the case.
A hash table is a collection of items w hich are stored in such a w ay as to make it easy to find them later. Each position of the hash
table, often called a slot, can hold an item and is named by an integer value starting at 0. For example, w e willhave a slot named 0,
a slot named 1, a slot named 2, and so on. Initially, the hash table contains no items so every slot is empty. We can implement a
hash table by using a list w ith each element initialized to the special Python value None. Figure 4 show sa hash table of
size (m=11). In other w ords,there are m slots in the table, named 0 through 10.
The mapping betw een an item and the slot w here that item belongs in the hash table is called the hash function.The hash function
w illtake any item in the collection and return an integer in the range of slot names, betw een 0 and m-1. Assume that w e have the
set of integer items 54, 26, 93, 17, 77, and 31. Our first hash function, sometimes referred to as the “remainder method,” simply
takes an item and divides it by the table size, returning the remainder as its hash value ((h(item)=item % 11)). Table 4 gives all of
the hash values for our example items. Note that this remainder method (modulo arithmetic) w illtypically be present in some form in
all hash functions, since the result must be in the range of slot names.
Item Hash Value
54 10
26 4
93 5
17 6
77 0
31 9
Once the hash values have been computed, w e can insert each item into the hash table at the designated position as show n
in Figure 5. Note that 6 of the 11 slots are now occupied. This is referred to as the load factor, and is commonly denoted
by (lambda = frac {numberofitems}{tablesize}). For this example, (lambda = frac {6}{11}).
59. Data Structures and Algorithms – Alexander Borsuk – October 2016
Collision Resolution
One method for resolving collisions looks into the hash table and tries to find another open slot to hold the item that caused the
collision. A simple w ay to do this is to start at the original hash value position and then move in a sequential manner through the
slots until w e encounter the first slot that is empty. Note that w e may need to go backto the first slot (circularly) to cover the entire
hash table. This collision resolution process is referred to as openaddressingin that it tries to find the next open slot or address in
the hash table. By systematically visiting each slot one at a time, w e are performing an open addressing technique called linear
probing.
Figure 8 show san extended set of integer items under the simple remainder method hash function
(54,26,93,17,77,31,44,55,20). Table 4 above show sthe hash values for the original items. Figure 5show s the originalcontents.
When w e attempt to place 44 into slot 0, a collision occurs. Under linear probing, w e looksequentially, slot by slot, until w e find an
open position. In this case, w e find slot 1.
Again, 55 should go in slot 0 but must be placed in slot 2 since it is the next open position. The final value of 20 hashes to slot 9.
Since slot 9 is full, w e begin to do linear probing. We visit slots 10, 0, 1, and 2, and finally find an empty slot at position 3.
60. Data Structures and Algorithms – Alexander Borsuk – October 2016
Array Algorithms
Shift Array Right
Shift Array Left
BitArray C#
61. Data Structures and Algorithms – Alexander Borsuk – October 2016
Removing Duplicates from Array
Find smallest and largest number in Array
62. Data Structures and Algorithms – Alexander Borsuk – October 2016
Two Arrays Intersections for SORTED ARRAY
[1 2 3 4 5] and [1 6 7 8 2] = [1 2]
Time Complexity: O (m+n)
Print Union of two SORTED ARRAYS
Time Complexity: O (m+n)
63. Data Structures and Algorithms – Alexander Borsuk – October 2016
Find Union and Intersection of two unsorted arrays
64. Data Structures and Algorithms – Alexander Borsuk – October 2016
Print all possible sub arrays of array
ABC: A, B, C, AB,AC, BC
Print all permutation of the array
ABC: ABC,ACB,CBA,BAC, BCA,CAB
65. Data Structures and Algorithms – Alexander Borsuk – October 2016
Find All Palindromes of Array
1. Use create al substringof stringmethod andthenuse followsone tofindall palindromes:
66. Data Structures and Algorithms – Alexander Borsuk – October 2016
Find all Duplicates in Array
67. Data Structures and Algorithms – Alexander Borsuk – October 2016
Matrix Algorithms
Rotate Matrix 90 Degrees Clockwise
Convert Bases (Hex->Oct->Bin->Dec)