05 Handout 1
05 Handout 1
A problem of size n
Sub-problem 1 of Sub-problem 2 of
size n/2 size n/2
A solution to A solution to
sub-problem 1 sub-problem 2
A solution to
the original problem
Example:
Initial Hand:
0 1 2 3 4 5 6
2 Q J 7 A 10 5
1st Split:
0 1 2 3 4 5 6
2 Q J 7 A 10 5
2 Q J 7 A 10 5
2nd Split:
0 1 2 3 4 5 6
2 Q J 7 A 10 5
0 1 2 3 4 5 6
2 Q J 7 A 10 5
2 Q J 7 A 10 5
3 Split:
rd
0 1 2 3 4 5 6
2 Q J 7 A 10 5
2 Q J 7 A 10 5
2 Q J 7 A 10 5
.2 .Q .J 7. .A 10. .5
1st to 3rd Merge:
0 1 2 3 4 5 6
2 Q J 7 A 10 5
2 Q J 7 A 10 5
2 Q J 7 A 10 .5
.2 .Q J. .7 .A 10. 5.
.2 .Q 7. J. 10 A. . 5.
.2 . 7. . J. Q. 5.. . 10 . A.
2 5 . 7. . 10 . J. Q .A
Quick Sort is a type of sorting algorithm in which the main idea is to partition the array into two (2) regions: small items are
moved to the left side of the array and large items are moved to the right side. After partitioning, the sort is repeated on the left
and right sides.
Example:
Determine pivot
• Median of left: • Median of right: • Median of the center:
After partitioning
Values
Index
Values
Index
Values
Index
Values
Index
Values
Index
Values
Index
Binary search is a type of search algorithm that is used to locate an element in an ordered array. It is advisable to use it whenever
the list starts to become larger.
The steps of the Binary search starts by testing the target. If the target is equal to the middle item, quit. Otherwise:
1. Divide the array into two sub-arrays about half as large. If the target is smaller than the middle item, choose the left sub-
array. If the target is larger than the middle item, choose the right sub-array.
2. Divide (Solve) the sub-array by determining whether the target is in that sub-array. Unless the sub-array is sufficiently
small, use recursion to do this.
3. Obtain the solution to the array from the solution to the sub-array.
Example:
Initial Hand:
0 1 2 3 4 5 6 7 8 9 10 11 12
1 5 15 19 25 27 29 31 33 45 55 88 100
Target: 19
Step 1:
Step 2:
Repeat Step 1:
Repeat Step 2:
Repeat Step 1:
Repeat Step 2:
Binary Tree is a type of tree data structure in which no node can have more than two (2) subtrees. In other words, a node can
have zero, one, or two subtrees. These subtrees are designated as the left subtree and right subtree.
Binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence.
The two (2) general approaches to the traversal sequence are as follows:
• Depth-first traversal is where the processing proceeds along a path from the root through one child to the most distant
descendent of that first child before processing a second child. In other words, in the depth-first search traversal, all of
the descendants of a child are processed before going on to the next child.
In the preorder traversal, the root is In the inorder traversal, the left node In the postorder traversal, the
visited first and successively moving is visited first, and subsequently visits leftmost leaf in the tree is visited first,
to the left subtree until a leaf is the parent of that node. It then goes then going up to the parent and down
reached, visiting each node on the to the child on the right and finds the the second leftmost leaf in the same
way there. Once there are no more next leftmost node in the tree to visit. branch, and so on until the parent is
children on the left of a node, the the last node to be visited within a
children on the right subtree are branch.
visited.
• Breadth-first traversal is where the processing proceeds horizontally from the root to all of its children, then to its
children’s children, and so forth until all nodes have been processed. In other words, in the breadth-first traversal, each
level is completely processed before the next level is started.
References:
05 Divide and Conquer.ppt (2012) Retrieved from: http://ix.cs.uoregon.edu/~conery/eic/ruby/slides/Powerpoint/05%20Divide%20and%20Conquer.ppt
2.2 Mergesort (2018) Retrieved from: https://algs4.cs.princeton.edu/22mergesort/
5.4. The Binary Search (n. d.) Retrieved from: https://runestone.academy/runestone/books/published/pythonds/SortSearch/TheBinarySearch.html
14. All Sorts of Everything (Sort Algorithms) (n. d.) Retrieved from: http://www.eecs.qmul.ac.uk/~pc/research/education/puzzles/reading/cwcch14.pdf