Algo - Mod3 and Mod4 - Extended - Evon
Algo - Mod3 and Mod4 - Extended - Evon
Theory (1901715 )
Evon Abu-Taieh, Associate Professor
[email protected]
Issam AlHadid, Assistant Professor
[email protected]
(Module 3)
Meeting
• Sunday 4-7
• Semester: Spring 2017/2018
• Ref.
• Cormen, Leiserson, Rivest, Stein,
"Introduction to Algorithm", PHI.
• A V Aho, J E Hopcroft, J D Ullman, "Design
and Analysis of Algorithms", Addison-Wesley
Publishing.
Tentative Grading Scale
35- 50- 55- 60- 65- 70- 75- *: 80- 85- 90- 95-
0 34 49 54 59 64 69 74 79 84 89 94 100
F D- D D+ C- C C+ B- B B+ A- A
Grades
• Advantages:
Efficient in terms of time if the input array is
already sorted (best-case scenario).
Efficient in terms of memory space (in-place).
Easy to design - only two nested loops.
• Disadvantages:
Not efficient in terms of running time (worst-case
and average-case scenarios).
Divide-and-Conquer Method
• General Method
• Merge Sort Algorithm
• Quicksort Algorithm
General Method
• Recursive in structure:
• Divide the problem into several smaller sub-
problems that are similar to the original but
smaller in size.
• Conquer the sub-problems by solving them
recursively. If they are small enough, just solve
them in a straightforward manner.
• Combine the solutions to create a solution to
the original problem.
1. Merge Sort
Merge Sort Algorithm
• Advantages:
Time is O(n log n).
Easy to code, since it uses divide-and-conquer
method.
• Disadvantages:
Needs more memory space.
3. Quick sort
Quick sort
Another divide-and-conquer algorithm:
• Divide: A[p…r] is partitioned (rearranged) into two
nonempty subarraysA[p…q-1] and A[q+1…r] such
that each element of A[p…q-1] is less than or equal to
each element of A[q+1…r]. Index q is computed here,
called pivot.
• Conquer: two subarrays are sorted by recursive calls
to quicksort (main function).
• Combine: unlike merge sort, no work needed since
the subarrays are sorted in place already.
Quick sort
• The basic algorithm to sort an array A consists of the
following four easy steps:
Small instance has n ≤ 1
o Every small instance is a sorted instance.
To sort a large instance:
o select a pivot element from out of the n elements.
Partition the n elements into 3 groups left, middle and
right:
o The middle group contains only the pivot element.
o All elements in the left group are ≤ pivot.
o All elements in the right group are > pivot.
Sort left and right groups recursively.
Quick sort
The steps are:
• Pick an element, called a pivot (leftmost), from the array.
• Partitioning: reorder the array so that all elements with values
less than the pivot come before the pivot, while all elements
with values greater than the pivot come after it (equal values
can go either way). After this partitioning, the pivot is in its
final position. This is called the partition operation.
• Recursively apply the above steps to the sub-array of elements
with smaller values and separately to the sub-array of elements
with greater values.
Example 1
Pivot (leftmost)
Example 2
Quick sort Algorithm
HW: write the code
Quick Sort Code…
Partition Function
• Worst-case scenario.
• Best-case scenario.
• Average-case scenario.
Worst-Case Scenario
http://bigocheatsheet.com/
https://he-
s3.s3.amazonaws.com/media/uploads/c950295.png
4. Bubble sort
Bubble sort animation
Example 2- First Pass
(14258) (14258)
(12458) (12458)
(12458) (12458)
Now, the array is already sorted, but the algorithm does not know if it is
completed. The algorithm needs one whole pass without any swap to
know it is sorted.
Third Pass
(12458) (12458)
(12458) (12458)
(12458) (12458)
(12458) (12458)
5. Heap Sort
Heap Sort
10 2
5 4
10
4 2
5 4
5 2
4 4
• Sort
Steps
• Take the least significant digit (or group of bits, both
being examples of radices) of each key.
• Group the keys based on that digit, but otherwise
keep the original order of keys. (This is what makes
the LSD radix sort a stable sort.)
• Repeat the grouping process with each more
significant digit.
Same example
Original, unsorted list:
170, 45, 75, 90, 802, 2, 24, 66
Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66
Notice that we keep 802 before 2, because 802 occurred before 2 in the
original list, and similarly for pairs 170 & 90 and 45 & 75.
Sorting by next digit (10s place) gives:
802, 2, 24, 45, 66, 170, 75, 90
Notice that 802 again comes before 2 as 802 comes before 2 in the previous
list.
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802
Another view
1> The integers are enqueued into an array of ten separate queues
based on their digits from right to left. Computers often represent
integers internally as fixed-length binary digits. Here, we will do
something analogous with fixed-length decimal digits. So, using the
numbers from the previous example, the queues for the 1st pass
would be:
0: 170, 090
1: none
2: 802, 002
3: none
4: 024
5: 045, 075
6: 066
7–9: none
2. The queues are dequeued back into an array of
integers, in increasing order. Using the same
numbers, the array will look like this after the first
pass:
170, 090, 802, 002, 024, 045, 075, 066
For the second pass
For the second pass:
Queues:
0: 802, 002
1: none
2: 024
3: none
4: 045
5: none
6: 066
7: 170, 075
8: none
9: 090
Array:
802, 002, 024, 045, 066, 170, 075, 090
(note that at this point only 802 and 170 are out of order
Third Pass
For the third pass:
Queues:
0: 002, 024, 045, 066, 075, 090
1: 170
2–7: none
8: 802
9: none
Array:
002, 024, 045, 066, 075, 090, 170, 802 (sorted)
Spaghetti sorting
Spaghetti sorting
• For simplicity, assume we are sorting a list of natural numbers. The sorting method
is illustrated using uncooked rods of spaghetti:
• For each number x in the list, obtain a rod of length x. (One practical way of
choosing the unit is to let the largest number m in the list correspond to one full rod
of spaghetti. In this case, the full rod equals m spaghetti units. To get a rod of
length x, break a rod in two so that one piece is of length x units; discard the other
piece.)
• Once you have all your spaghetti rods, take them loosely in your fist and lower
them to the table, so that they all stand upright, resting on the table surface. Now,
for each rod, lower your other hand from above until it meets with a rod—this one
is clearly the longest. Remove this rod and insert it into the front of the (initially
empty) output list (or equivalently, place it in the last unused slot of the output
array). Repeat until all rods have been removed.
Shell Sort
Example: sort the
following
16 4 3 13 5 6 8 9 10 11 12 17 15 18 19 7 1 2 14 20
16 4 3 13 5 6 8
9 10 11 12 17 15 18
19 7 1 2 14 20
• Once we have performed this slicing we can sort each slice:
• First we sort the first column ( 16 9 and 19 ) to give 9 16 19
Next the second column ( 4 10 and 7 ) to give 4 7 10
• Column three ( 3 11 and 1 ) to give 1 3 11
• Column four ( 13 12 and 2 ) to give 2 12 13
• Column five ( 5 17 and 14 ) to give 5 14 17
• Column six ( 6 15 and 20 ) to give 6 15 20
and column seven ( 8 and 18 ) to give 8 18
16 4 3 13 5 6 8
9 10 11 12 17 15 18
19 7 1 2 14 20
9 4 1 2 5 6 8 16 7 3 12 14 15 18 19 10 11 13 17 20
Shell Sort Example: Part 2
9 4 1 2 5 6 8 16 7 3 12 14 15 18 19 10 11 13 17 20