SlideShare a Scribd company logo
Advanced Algorithms:
Sorting
Damian Gordon
Advanced Algorithms
• Sorting Algorithms
– Insertion Sort
– Shell Sort
– Merge Sort
– Quick Sort
We’ll remember …
PROGRAM BubbleSort:
Integer Age[8] <- {44,23,42,33,16,54,34,18};
FOR Outer-Index IN 0 TO N-1
DO FOR Index IN 0 TO N-2
DO IF (Age[Index+1] < Age[Index])
THEN Temp_Value <- Age[Index+1];
Age[Index+1] <- Age[Index];
Age[Index] <- Temp_Value;
ENDIF;
ENDFOR;
ENDFOR;
END.
Sorting: Bubble Sort
PROGRAM SelectionSort:
Integer Age[8] <- {44,23,42,33,16,54,34,18};
FOR Outer-Index IN 0 TO N-1
MinValueLocation <- Outer-Index;
FOR Index IN Outer-Index+1 TO N-1
DO IF (Age[Index] < Age[MinValueLocation])
THEN MinValueLocation <- Index;
ENDIF;
ENDFOR;
IF (MinValueLocation != Outer-Index)
THEN Swap(Age[Outer-Index], Age[MinValueLocation]);
ENDIF;
ENDFOR;
END.
Sorting: Selection Sort
Insertion Sort
Insertion Sort
• Insertion Sort works by taking the first two elements of the list,
sorting them, then taking the third one, and sorting that into
the first two, then taking the fourth element and sorting that
into the first three, etc.
Insertion Sort
• Let’s look at an example:
Insertion Sort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 44 42 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 44 42 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 44 42 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 42 44 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 42 44 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 42 44 33 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 33 42 44 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 33 42 44 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
23 33 42 44 16 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 34 42 44 54 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 34 42 44 54 18
0 1 2 3 4 5 6 7
Insertion Sort
16 23 33 34 42 44 54 18
0 1 2 3 4 5 6 7
Insertion Sort
16 18 23 33 34 42 44 54
0 1 2 3 4 5 6 7
Insertion Sort
16 18 23 33 34 42 44 54
0 1 2 3 4 5 6 7
Insertion Sort
16 18 23 33 34 42 44 54
0 1 2 3 4 5 6 7
Insertion Sort
• How do we move the elements into their correct position
(we’ll call this the “Insertion Sort move”)?
Insertion Sort
• We store 34 in CURRENT.
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Current: 34
Insertion Sort
• Next we move what value is in the previous position to 34 into
that location.
16 23 33 42 44 54 34 18
0 1 2 3 4 5 6 7
Current: 34
Insertion Sort
• Next we move what value is in the previous position to 34 into
that location.
16 23 33 42 44 54 54 18
0 1 2 3 4 5 6 7
Current: 34
Insertion Sort
• And we do that again.
16 23 33 42 44 44 54 18
0 1 2 3 4 5 6 7
Current: 34
Insertion Sort
• And again.
16 23 33 42 42 44 54 18
0 1 2 3 4 5 6 7
Current: 34
Insertion Sort
• And then we move CURRENT into the correct position.
16 23 33 42 42 44 54 18
0 1 2 3 4 5 6 7
Current: 34
Insertion Sort
• And then we move CURRENT into the correct position.
16 23 33 42 42 44 54 18
0 1 2 3 4 5 6 7
Current: 34
Insertion Sort
• And then we move CURRENT into the correct position.
16 23 33 34 42 44 54 18
0 1 2 3 4 5 6 7
Insertion Sort
• The element being added in each time is just to the left of the
sorted list.
• So, if the next element is called CURRENT, the largest element
in the sorted list is CURRENT – 1.
• So we’ll know if the next element is largest if it is bigger than
CURRENT – 1.
Sorted sub-list
Next
element
CURRENT
Insertion Sort
• Structured English:
FOR each element from the second TO the end of the list
DO Remember the current position and value
WHILE the previous element is bigger than current
DO Move the previous element into current’s position
END WHILE
We’ve reached the position in the list that current should be
Put it in
END FOR
NOTE: The Previous
Element is the end of
the sorted sub-list
PROGRAM InsertionSort:
Integer Array[8] <- {44,23,42,33,16,54,34,18};
FOR Index IN 1 TO N
DO current = Array[index];
pos = index;
WHILE (pos > 0 and Array[pos – 1] > current)
DO Array[pos] <- Array[pos - 1];
pos = pos - 1;
ENDWHILE;
Array[pos] = current;
ENDFOR;
END.
Insertion Sort
NOTE: If you have
reached the start of
the list, STOP!
Insertion Sort
• Complexity of Insertion Sort
– Best-case scenario complexity = O(N)
– Average complexity = O(N2)
– Worst-case scenario complexity = O(N2)
Shell Sort
Shell Sort
• ShellSort is an extension of InsertionSort that was developed
by Donald Shell.
• Shell observed that the process of doing the Insertion Sort
move, particularly if that have to be moved from one side of
the array to other, is computationally expensive.
Shell Sort
• Instead of sorting the whole list, ShellSort first picks ever Nth
element, sorts those elements (0, N, 2N, 3N,…), then sorts (1,
N+1, 2N+1, 3N+1,…), then (2, N+2, 2N+2, 3N+2,…), and so on.
• Once that’s done, let’s sort every Mth element (where M is N
DIV 2), so we’ll sort (0, M, 2M, 3M,…), then sorts (1, M+1,
2M+1, 3M+1,…), then (2, M+2, 2M+2, 3M+2,…), and so on.
• And keep doing this until we get to 1.
Donald L. Shell
• Born: March 1, 1924
• Died: November 2, 2015
• Shell, D.L. (1959) "A High-
Speed Sorting Procedure“,
Communications of the ACM,
2(7), pp. 30–32.
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
– Second group: 23, 54
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
– Second group: 23, 54
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
– Second group: 23, 54
– Third group: 42, 34
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
– Second group: 23, 54
– Third group: 42, 34
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
– Second group: 23, 54
– Third group: 42, 34
– Fourth group: 33, 16
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18
– Second group: 23, 54
– Third group: 42, 34
– Fourth group: 33, 16
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18 18, 44
– Second group: 23, 54 23, 54
– Third group: 42, 34 34, 42
– Fourth group: 33, 16 16, 33
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
• Let’s pick every 4th element:
– First group: 44, 18 18, 44
– Second group: 23, 54 23, 54
– Third group: 42, 34 34, 42
– Fourth group: 33, 16 16, 33
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Let’s pick every 4th element:
– First group: 44, 18 18, 44
– Second group: 23, 54 23, 54
– Third group: 42, 34 34, 42
– Fourth group: 33, 16 16, 33
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Let’s pick every 4th element:
– First group: 44, 18 18, 44
– Second group: 23, 54 23, 54
– Third group: 42, 34 34, 42
– Fourth group: 33, 16 16, 33
Sort these using
Insertion Sort
The data is not sorted, but a lot of the big numbers have been moved to the end of
the list, and a lot of the smaller numbers have been moved to the start.
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42
– Second Group: 23, 16, 54, 33
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42
– Second Group: 23, 16, 54, 33
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42 18, 34, 42, 44
– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42 18, 34, 42, 44
– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42 18, 34, 42, 44
– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these using
Insertion Sort
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Now let’s do every 2nd element:
– First Group: 18, 34, 44, 42 18, 34, 42, 44
– Second Group: 23, 16, 54, 33 16, 23, 33, 54
Sort these using
Insertion Sort
The data is almost completely sorted now.
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Finally do one more Insertion Sort with all elements
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Finally do one more Insertion Sort with all elements
• This will be very fast since the data is almost sorted
Shell Sort
• So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54].
• Finally do one more Insertion Sort with all elements
• This will be very fast since the data is almost sorted
• Age = [16, 18, 23, 33, 34, 42, 44, 54].
Shell Sort
• Let’s look at the PseudoCode in two parts:
– 1) The Insertion Sort code as before, but modified not to sort all of
the elements, but rather every Nth element
– 2) The Shell Sort that calls the Insertion Sort code for each Nth
elements, and then N/2, and N/4, and so on until 1.
MODULE GapInsertionSort(Array, StartPos, Gap):
FOR Index IN StartPos TO N INCREMENT BY Gap
DO current = Array[index];
pos = index;
WHILE (pos > Gap and Array[pos – Gap] > current)
DO Array[pos] <- Array[pos - Gap];
pos = pos - Gap;
ENDWHILE;
Array[pos] = current;
ENDFOR;
END.
Shell Sort
MODULE GapInsertionSort(Array, StartPos, Gap):
FOR Index IN StartPos TO N INCREMENT BY Gap
DO current = Array[index];
pos = index;
WHILE (pos > Gap and Array[pos – Gap] > current)
DO Array[pos] <- Array[pos - Gap];
pos = pos - Gap;
ENDWHILE;
Array[pos] = current;
ENDFOR;
END.
Shell Sort
MODULE ShellSort(Array):
GapSize <- Length(Array) DIV 2;
WHILE (GapSize > 0)
DO
FOR StartPos IN 0 TO GapSize
DO GapInsertionSort(Array, StartPos, GapSize);
ENDFOR;
GapSize <- GapSize DIV 2;
ENDWHILE;
END.
Shell Sort
We are reducing the
Gap in half each time
For each of the Nth
Element, each N+1th
Element, N+2th, etc.
The main loop will
keep going until the
Gap is 1.
Shell Sort
• Complexity of Shell Sort
– Best-case scenario complexity = O(N)
– Average complexity = O(N * log2(N))
– Worst-case scenario complexity = O(N * log2(N))
Merge Sort
Merge Sort
• Merge Sort was developed by John von Neumann in 1945.
John von Neumann
• Born: December 28, 1903
• Died: February 8, 1957
• Born in Budapest, Austria-Hungary
• A mathematician who made major
contributions to set theory,
functional analysis, quantum
mechanics, ergodic theory,
continuous geometry, economics
and game theory, computer
science, numerical analysis,
hydrodynamics and statistics.
Merge Sort
• Merge Sort used a “divide-and-conquer” strategy.
• It’s a two-step process:
– 1. Keep splitting the array in half until you end up with sub-arrays of
one item (which are sorted by definition).
– 2. Successively merge each sub-array together, and sort with each
merge.
Merge Sort
• Let’s look at an example:
Merge Sort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
Merge Sort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
Merge Sort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
Merge Sort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23
0 1
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23
0 1
23 44
0 1
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23
0 1
23 44
0 1
42 33
2 3
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23
0 1
23 44
0 1
42 33
2 3
33 42
2 3
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23
0 1
23 44
0 1
42 33
2 3
33 42
2 3
16 54
4 5
16 54
4 5
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23
0 1
23 44
0 1
42 33
2 3
33 42
2 3
16 54
4 5
16 54
4 5
34 18
6 7
Merge Sort
44 23 42 33
0 1 2 3
16 54 34 18
4 5 6 7
44 23
0 1
23 44
0 1
42 33
2 3
33 42
2 3
16 54
4 5
16 54
4 5
34 18
6 7
18 34
6 7
Merge Sort
23 44
0 1
33 42
2 3
16 54
4 5
18 34
6 7
Merge Sort
23 44
0 1
33 42
2 3
16 54
4 5
18 34
6 7
23 44
0 1
33 42
2 3
Merge Sort
23 44
0 1
33 42
2 3
16 54
4 5
18 34
6 7
23 44
0 1
33 42
2 3
23 33
0 1
42 44
2 3
Merge Sort
23 44
0 1
33 42
2 3
16 54
4 5
18 34
6 7
23 44
0 1
33 42
2 3
23 33
0 1
42 44
2 3
16 54
4 5
18 34
6 7
Merge Sort
23 44
0 1
33 42
2 3
16 54
4 5
18 34
6 7
23 44
0 1
33 42
2 3
23 33
0 1
42 44
2 3
16 54
4 5
18 34
6 7
16 18
4 5
34 54
6 7
Merge Sort
23 33
0 1
42 44
2 3
16 18
4 5
34 54
6 7
Merge Sort
23 33
0 1
42 44
2 3
16 18
4 5
34 54
6 7
23 33
0 1
42 44
2 3
16 18
4 5
34 54
6 7
Merge Sort
23 33
0 1
42 44
2 3
16 18
4 5
34 54
6 7
23 33
0 1
42 44
2 3
16 18
4 5
34 54
6 7
16 18
0 1
23 33
2 3
34 42
4 5
44 54
6 7
PROGRAM MainMergeSort:
Array = [44,23,42,33,16,54,34,18];
MergeSort(Array);
PRINT Array;
END.
Merge Sort
PROGRAM MergeSort(Array):
IF (length(Array) > 1)
THEN MidPoint = len(Age)//2
LeftHalf = Age[:MidPoint]
RightHalf = Age[MidPoint:]
Merge Sort
Keep recursively
splitting the array
until you get down
sub-arrays of one
element.
Continued 
MergeSort(LeftHalf)
MergeSort(RightHalf)
LCounter = 0
RCounter = 0
MainCounter = 0
Merge Sort
Recursively call
MergeSort for each half
of the array. After the
splitting gets down to
one element the
recursive calls will pop
off the stack to merge
the sub-arrays together.
Continued 
 Continued
WHILE (LCounter < len(LeftHalf) AND
RCounter < len(RightHalf))
DO IF LeftHalf[LCounter] < RightHalf[RCounter]
THEN Age[MainCounter] = LeftHalf[LCounter];
LCounter = LCounter + 1;
ELSE Age[MainCounter] = RightHalf[RCounter];
RCounter = RCounter + 1;
ENDIF;
MainCounter = MainCounter + 1;
ENDWHILE;
Merge Sort
Continued 
 Continued
Keep comparing each
element of the left and
right sub-array, writing
the smaller element
into the main array
WHILE LCounter < len(LeftHalf)
DO Age[MainCounter] = LeftHalf[LCounter];
LCounter = LCounter + 1;
MainCounter = MainCounter + 1;
ENDWHILE;
WHILE Rcounter < len(RightHalf)
DO Age[MainCounter] = RightHalf[RCounter]
RCounter = RCounter + 1
MainCounter = MainCounter + 1
ENDWHILE;
ENDIF;
Merge Sort
 Continued
After the comparisons
are done, write either
the rest of the left array
or the right array into
the main array that
Merge Sort
• Complexity of Merge Sort
– Best-case scenario complexity = O(N)
– Average complexity = O(N * log2(N))
– Worst-case scenario complexity = O(N * log2(N))
Quick Sort
QuickSort
• Quicksort was developed by Tony Hoare in 1959 and is still a
commonly used algorithm for sorting.
Charles Antony Richard Hoare
• Born: January 11, 1934
• Hoare's most significant work
includes: his sorting and selection
algorithm (Quicksort and
Quickselect), Hoare logic, the
formal language Communicating
Sequential Processes (CSP) used to
specify the interactions between
concurrent processes, structuring
computer operating systems using
the monitor concept.
QuickSort
• The key idea behind Quicksort is to pick a random value from
the array, and starting from either side of the array, swap
elements that are lower than the value in the right of the array
with elements of the left of the array that are larger than the
value, until we reach the point where the random value should
be, then we put the random value in its place.
• We recursively do this process with the sub-arrays on either
side of the random value ( called the “pivot”).
QuickSort
• Let’s look at an example:
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 54 34 18
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 18 34 54
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 18 34 54
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 18 34 54
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 18 34 54
0 1 2 3 4 5 6 7
QuickSort
44 23 42 33 16 18 34 54
0 1 2 3 4 5 6 7
QuickSort
34 23 42 33 16 18 44 54
0 1 2 3 4 5 6 7
QuickSort
34 23 42 33 16 18 44 54
0 1 2 3 4 5 6 7
Pivot value is
now in its correct
position.
QuickSort
34 23 42 33 16 18 44 54
0 1 2 3 4 5 6 7
Now repeat this
process with this
sub-array
QuickSort
34 23 42 33 16 18 44 54
0 1 2 3 4 5 6 7
Now repeat this
process with this
sub-array
…and this one
QuickSort
34 23 42 33 16 18 44 54
0 1 2 3 4 5 6 7
THIS IS ALREADY
SORTED!!!
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 42 33 16 18
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 18 33 16 42
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 18 33 16 42
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 18 33 16 42
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 18 33 16 42
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 18 33 16 42
0 1 2 3 4 5
44 54
6 7
QuickSort
34 23 18 33 16 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
Now repeat this
process with this
sub-array
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
Now repeat this
process with this
sub-array
…and this one
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
Now repeat this
process with this
sub-array THIS IS ALREADY
SORTED
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
No swaps will occur on this
pass, as 16 is in the right
place
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
…but as 16 is swapped into
itself, the rest of the sub-
array will be sorted.
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
…but as 16 is swapped into
itself, the rest of the sub-
array will be sorted.
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 23 18 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 18 23 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 18 23 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 18 23 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 18 23 33 34 42
0 1 2 3 4 5
44 54
6 7
QuickSort
16 18 23 33 34 42
0 1 2 3 4 5
44 54
6 7
SORTED!
PROGRAM QuickSort(Array, First, Last):
IF (First < Last)
THEN Pivot = Partition(Array, First, Last);
QuickSort(Array, First, Pivot - 1):
QuickSort(Array, Pivot + 1, Last):
ENDIF;
END.
QuickSort
PROGRAM Partition(Array, First, Last):
PivotVal = Array[First];
Finished = False;
LeftPointer = First + 1;
RightPointer = Last;
QuickSort
We randomly select
the pivot, in this case
we select the first
element. Since the
array isn’t sorted yet,
the value of the first
element could have
any value
Continued 
WHILE NOT(Finished)
DO
WHILE (LeftPointer <= RightPointer) AND
(Age[LeftPointer] <= PivotVal)
DO LeftPointer = LeftPointer + 1
ENDWHILE;
WHILE (Age[RightPointer] >= PivotVal) AND
(RightPointer >= LeftPointer)
DO RightPointer = RightPointer - 1
ENDWHILE;
QuickSort
Continued 
 Continued
Keep moving left
until we find a
value that is less
than the pivot, or
we reach the Right
Pointer.
Keep moving right
until we find a
value that is
greater than the
pivot, or we reach
the left Pointer.
IF (LeftPointer < RightPointer)
THEN Finished = False;
ELSE SWAP(Age[LeftPointer], Age[RightPointer]);
ENDIF;
SWAP(Age[First], Age[RightPointer]);
RETURN RightPointer;
END Partition.
QuickSort
 Continued
We’ve a value
greater than the
pivot to the left,
and one less to the
right, swap them
Put the pivot in its
correct position
QuickSort
• Complexity of Quick Sort
– Best-case scenario complexity = O(N * log2(N))
– Average complexity = O(N * log2(N))
– Worst-case scenario complexity = O(N2)
Advanced Algorithms
• Sorting Algorithms
– Insertion Sort
– Shell Sort
– Merge Sort
– Quick Sort
etc.

More Related Content

What's hot

Selection sort
Selection sortSelection sort
Selection sort
Abdelrahman Saleh
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Imdadul Himu
 
Quick sort
Quick sortQuick sort
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
nikunjandy
 
Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...
University of Science and Technology Chitttagong
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
Monalisa Patel
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
Dipayan Sarkar
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
Ummar Hayat
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
asmhemu
 
Bucket sort
Bucket sortBucket sort
Bucket sort
Hemant Chetwani
 
Quick_sort1.pptx
Quick_sort1.pptxQuick_sort1.pptx
Quick_sort1.pptx
sandeep54552
 
Selection sort
Selection sortSelection sort
Selection sortJay Patel
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Counting Sort
Counting SortCounting Sort
Counting Sort
Faiza Saleem
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
Krish_ver2
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 

What's hot (20)

Selection sort
Selection sortSelection sort
Selection sort
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Quick sort
Quick sortQuick sort
Quick sort
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
 
Bucket sort
Bucket sortBucket sort
Bucket sort
 
Quick_sort1.pptx
Quick_sort1.pptxQuick_sort1.pptx
Quick_sort1.pptx
 
Selection sort
Selection sortSelection sort
Selection sort
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Merge sort
Merge sortMerge sort
Merge sort
 

Viewers also liked

Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
Brett Duncan
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
aditya raj
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
Gail Carmichael
 
Data Structure Insertion sort
Data Structure Insertion sort Data Structure Insertion sort
Data Structure Insertion sort
Mahesh Dheravath
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
Andres Mendez-Vazquez
 
06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear Time
Andres Mendez-Vazquez
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Vicente García Díaz
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting TechniquesRafay Farooq
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Rajendran
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertionirdginfo
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Atiqotun Niswah
 
Merge sort
Merge sortMerge sort
Merge sortKumar
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
Yoshi Watanabe
 
Intro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortIntro to Sorting + Insertion Sort
Intro to Sorting + Insertion Sort
Nicholas Case
 
Insertion sort
Insertion sortInsertion sort
Insertion sortMYER301
 

Viewers also liked (20)

Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
Data Structure Insertion sort
Data Structure Insertion sort Data Structure Insertion sort
Data Structure Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected05 Analysis of Algorithms: Heap and Quick Sort - Corrected
05 Analysis of Algorithms: Heap and Quick Sort - Corrected
 
06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear Time
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Elementary Sort
Elementary SortElementary Sort
Elementary Sort
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertion
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
 
Intro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortIntro to Sorting + Insertion Sort
Intro to Sorting + Insertion Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 

Similar to Advanced Sorting Algorithms

lecture14
lecture14lecture14
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
PRIANKA R
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
Sorting
SortingSorting
Sorting
Samsil Arefin
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
Abdullah Al-hazmy
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
AnSHiKa187943
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
CS253: Distribution-based Sort (2019)
CS253: Distribution-based Sort (2019)CS253: Distribution-based Sort (2019)
CS253: Distribution-based Sort (2019)
Jinho Choi
 
unit-3-permutation_combination.pptx
unit-3-permutation_combination.pptxunit-3-permutation_combination.pptx
unit-3-permutation_combination.pptx
Pradip738766
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
Zia Ush Shamszaman
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
Motaleb Hossen Manik
 
Array operations
Array operationsArray operations
Array operations
Razzaaa
 
Correlation Product Moment Method
Correlation Product Moment Method Correlation Product Moment Method
Correlation Product Moment Method
abhisrivastava11
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
Jinho Choi
 
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
mrhabib10
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
rasheed747195
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Edward Blurock
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
Ankur Srivastava
 

Similar to Advanced Sorting Algorithms (20)

lecture14
lecture14lecture14
lecture14
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Sorting
SortingSorting
Sorting
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
CS253: Distribution-based Sort (2019)
CS253: Distribution-based Sort (2019)CS253: Distribution-based Sort (2019)
CS253: Distribution-based Sort (2019)
 
unit-3-permutation_combination.pptx
unit-3-permutation_combination.pptxunit-3-permutation_combination.pptx
unit-3-permutation_combination.pptx
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
Array operations
Array operationsArray operations
Array operations
 
Correlation Product Moment Method
Correlation Product Moment Method Correlation Product Moment Method
Correlation Product Moment Method
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
 
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 

More from Damian T. Gordon

Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.
Damian T. Gordon
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
Damian T. Gordon
 
REST and RESTful Services
REST and RESTful ServicesREST and RESTful Services
REST and RESTful Services
Damian T. Gordon
 
Serverless Computing
Serverless ComputingServerless Computing
Serverless Computing
Damian T. Gordon
 
Cloud Identity Management
Cloud Identity ManagementCloud Identity Management
Cloud Identity Management
Damian T. Gordon
 
Containers and Docker
Containers and DockerContainers and Docker
Containers and Docker
Damian T. Gordon
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
Damian T. Gordon
 
Introduction to ChatGPT
Introduction to ChatGPTIntroduction to ChatGPT
Introduction to ChatGPT
Damian T. Gordon
 
How to Argue Logically
How to Argue LogicallyHow to Argue Logically
How to Argue Logically
Damian T. Gordon
 
Evaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONSEvaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONS
Damian T. Gordon
 
Evaluating Teaching: MERLOT
Evaluating Teaching: MERLOTEvaluating Teaching: MERLOT
Evaluating Teaching: MERLOT
Damian T. Gordon
 
Evaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson RubricEvaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson Rubric
Damian T. Gordon
 
Evaluating Teaching: LORI
Evaluating Teaching: LORIEvaluating Teaching: LORI
Evaluating Teaching: LORI
Damian T. Gordon
 
Designing Teaching: Pause Procedure
Designing Teaching: Pause ProcedureDesigning Teaching: Pause Procedure
Designing Teaching: Pause Procedure
Damian T. Gordon
 
Designing Teaching: ADDIE
Designing Teaching: ADDIEDesigning Teaching: ADDIE
Designing Teaching: ADDIE
Damian T. Gordon
 
Designing Teaching: ASSURE
Designing Teaching: ASSUREDesigning Teaching: ASSURE
Designing Teaching: ASSURE
Damian T. Gordon
 
Designing Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning TypesDesigning Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning Types
Damian T. Gordon
 
Designing Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of InstructionDesigning Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of Instruction
Damian T. Gordon
 
Designing Teaching: Elaboration Theory
Designing Teaching: Elaboration TheoryDesigning Teaching: Elaboration Theory
Designing Teaching: Elaboration Theory
Damian T. Gordon
 
Universally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some ConsiderationsUniversally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some Considerations
Damian T. Gordon
 

More from Damian T. Gordon (20)

Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
REST and RESTful Services
REST and RESTful ServicesREST and RESTful Services
REST and RESTful Services
 
Serverless Computing
Serverless ComputingServerless Computing
Serverless Computing
 
Cloud Identity Management
Cloud Identity ManagementCloud Identity Management
Cloud Identity Management
 
Containers and Docker
Containers and DockerContainers and Docker
Containers and Docker
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
 
Introduction to ChatGPT
Introduction to ChatGPTIntroduction to ChatGPT
Introduction to ChatGPT
 
How to Argue Logically
How to Argue LogicallyHow to Argue Logically
How to Argue Logically
 
Evaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONSEvaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONS
 
Evaluating Teaching: MERLOT
Evaluating Teaching: MERLOTEvaluating Teaching: MERLOT
Evaluating Teaching: MERLOT
 
Evaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson RubricEvaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson Rubric
 
Evaluating Teaching: LORI
Evaluating Teaching: LORIEvaluating Teaching: LORI
Evaluating Teaching: LORI
 
Designing Teaching: Pause Procedure
Designing Teaching: Pause ProcedureDesigning Teaching: Pause Procedure
Designing Teaching: Pause Procedure
 
Designing Teaching: ADDIE
Designing Teaching: ADDIEDesigning Teaching: ADDIE
Designing Teaching: ADDIE
 
Designing Teaching: ASSURE
Designing Teaching: ASSUREDesigning Teaching: ASSURE
Designing Teaching: ASSURE
 
Designing Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning TypesDesigning Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning Types
 
Designing Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of InstructionDesigning Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of Instruction
 
Designing Teaching: Elaboration Theory
Designing Teaching: Elaboration TheoryDesigning Teaching: Elaboration Theory
Designing Teaching: Elaboration Theory
 
Universally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some ConsiderationsUniversally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some Considerations
 

Recently uploaded

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
datarid22
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 

Recently uploaded (20)

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 

Advanced Sorting Algorithms

  • 2. Advanced Algorithms • Sorting Algorithms – Insertion Sort – Shell Sort – Merge Sort – Quick Sort
  • 4. PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO N-2 DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; ENDIF; ENDFOR; ENDFOR; END. Sorting: Bubble Sort
  • 5. PROGRAM SelectionSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; FOR Outer-Index IN 0 TO N-1 MinValueLocation <- Outer-Index; FOR Index IN Outer-Index+1 TO N-1 DO IF (Age[Index] < Age[MinValueLocation]) THEN MinValueLocation <- Index; ENDIF; ENDFOR; IF (MinValueLocation != Outer-Index) THEN Swap(Age[Outer-Index], Age[MinValueLocation]); ENDIF; ENDFOR; END. Sorting: Selection Sort
  • 7. Insertion Sort • Insertion Sort works by taking the first two elements of the list, sorting them, then taking the third one, and sorting that into the first two, then taking the fourth element and sorting that into the first three, etc.
  • 8. Insertion Sort • Let’s look at an example:
  • 9. Insertion Sort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 10. Insertion Sort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 11. Insertion Sort 23 44 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 12. Insertion Sort 23 44 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 13. Insertion Sort 23 44 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 14. Insertion Sort 23 42 44 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 15. Insertion Sort 23 42 44 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 16. Insertion Sort 23 42 44 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 17. Insertion Sort 23 33 42 44 16 54 34 18 0 1 2 3 4 5 6 7
  • 18. Insertion Sort 23 33 42 44 16 54 34 18 0 1 2 3 4 5 6 7
  • 19. Insertion Sort 23 33 42 44 16 54 34 18 0 1 2 3 4 5 6 7
  • 20. Insertion Sort 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7
  • 21. Insertion Sort 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7
  • 22. Insertion Sort 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7
  • 23. Insertion Sort 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7
  • 24. Insertion Sort 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7
  • 25. Insertion Sort 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7
  • 26. Insertion Sort 16 23 33 34 42 44 54 18 0 1 2 3 4 5 6 7
  • 27. Insertion Sort 16 23 33 34 42 44 54 18 0 1 2 3 4 5 6 7
  • 28. Insertion Sort 16 23 33 34 42 44 54 18 0 1 2 3 4 5 6 7
  • 29. Insertion Sort 16 18 23 33 34 42 44 54 0 1 2 3 4 5 6 7
  • 30. Insertion Sort 16 18 23 33 34 42 44 54 0 1 2 3 4 5 6 7
  • 31. Insertion Sort 16 18 23 33 34 42 44 54 0 1 2 3 4 5 6 7
  • 32. Insertion Sort • How do we move the elements into their correct position (we’ll call this the “Insertion Sort move”)?
  • 33. Insertion Sort • We store 34 in CURRENT. 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7 Current: 34
  • 34. Insertion Sort • Next we move what value is in the previous position to 34 into that location. 16 23 33 42 44 54 34 18 0 1 2 3 4 5 6 7 Current: 34
  • 35. Insertion Sort • Next we move what value is in the previous position to 34 into that location. 16 23 33 42 44 54 54 18 0 1 2 3 4 5 6 7 Current: 34
  • 36. Insertion Sort • And we do that again. 16 23 33 42 44 44 54 18 0 1 2 3 4 5 6 7 Current: 34
  • 37. Insertion Sort • And again. 16 23 33 42 42 44 54 18 0 1 2 3 4 5 6 7 Current: 34
  • 38. Insertion Sort • And then we move CURRENT into the correct position. 16 23 33 42 42 44 54 18 0 1 2 3 4 5 6 7 Current: 34
  • 39. Insertion Sort • And then we move CURRENT into the correct position. 16 23 33 42 42 44 54 18 0 1 2 3 4 5 6 7 Current: 34
  • 40. Insertion Sort • And then we move CURRENT into the correct position. 16 23 33 34 42 44 54 18 0 1 2 3 4 5 6 7
  • 41. Insertion Sort • The element being added in each time is just to the left of the sorted list. • So, if the next element is called CURRENT, the largest element in the sorted list is CURRENT – 1. • So we’ll know if the next element is largest if it is bigger than CURRENT – 1. Sorted sub-list Next element CURRENT
  • 42. Insertion Sort • Structured English: FOR each element from the second TO the end of the list DO Remember the current position and value WHILE the previous element is bigger than current DO Move the previous element into current’s position END WHILE We’ve reached the position in the list that current should be Put it in END FOR NOTE: The Previous Element is the end of the sorted sub-list
  • 43. PROGRAM InsertionSort: Integer Array[8] <- {44,23,42,33,16,54,34,18}; FOR Index IN 1 TO N DO current = Array[index]; pos = index; WHILE (pos > 0 and Array[pos – 1] > current) DO Array[pos] <- Array[pos - 1]; pos = pos - 1; ENDWHILE; Array[pos] = current; ENDFOR; END. Insertion Sort NOTE: If you have reached the start of the list, STOP!
  • 44. Insertion Sort • Complexity of Insertion Sort – Best-case scenario complexity = O(N) – Average complexity = O(N2) – Worst-case scenario complexity = O(N2)
  • 46. Shell Sort • ShellSort is an extension of InsertionSort that was developed by Donald Shell. • Shell observed that the process of doing the Insertion Sort move, particularly if that have to be moved from one side of the array to other, is computationally expensive.
  • 47. Shell Sort • Instead of sorting the whole list, ShellSort first picks ever Nth element, sorts those elements (0, N, 2N, 3N,…), then sorts (1, N+1, 2N+1, 3N+1,…), then (2, N+2, 2N+2, 3N+2,…), and so on. • Once that’s done, let’s sort every Mth element (where M is N DIV 2), so we’ll sort (0, M, 2M, 3M,…), then sorts (1, M+1, 2M+1, 3M+1,…), then (2, M+2, 2M+2, 3M+2,…), and so on. • And keep doing this until we get to 1.
  • 48. Donald L. Shell • Born: March 1, 1924 • Died: November 2, 2015 • Shell, D.L. (1959) "A High- Speed Sorting Procedure“, Communications of the ACM, 2(7), pp. 30–32.
  • 49. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16].
  • 50. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element:
  • 51. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element:
  • 52. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18
  • 53. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18
  • 54. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 – Second group: 23, 54
  • 55. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 – Second group: 23, 54
  • 56. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 – Second group: 23, 54 – Third group: 42, 34
  • 57. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 – Second group: 23, 54 – Third group: 42, 34
  • 58. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 – Second group: 23, 54 – Third group: 42, 34 – Fourth group: 33, 16
  • 59. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 – Second group: 23, 54 – Third group: 42, 34 – Fourth group: 33, 16 Sort these using Insertion Sort
  • 60. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 18, 44 – Second group: 23, 54 23, 54 – Third group: 42, 34 34, 42 – Fourth group: 33, 16 16, 33 Sort these using Insertion Sort
  • 61. Shell Sort • So, for example. Age = [44, 23, 42, 33, 18, 54, 34, 16]. • Let’s pick every 4th element: – First group: 44, 18 18, 44 – Second group: 23, 54 23, 54 – Third group: 42, 34 34, 42 – Fourth group: 33, 16 16, 33 Sort these using Insertion Sort
  • 62. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Let’s pick every 4th element: – First group: 44, 18 18, 44 – Second group: 23, 54 23, 54 – Third group: 42, 34 34, 42 – Fourth group: 33, 16 16, 33 Sort these using Insertion Sort
  • 63. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Let’s pick every 4th element: – First group: 44, 18 18, 44 – Second group: 23, 54 23, 54 – Third group: 42, 34 34, 42 – Fourth group: 33, 16 16, 33 Sort these using Insertion Sort The data is not sorted, but a lot of the big numbers have been moved to the end of the list, and a lot of the smaller numbers have been moved to the start.
  • 64. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element:
  • 65. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element:
  • 66. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42
  • 67. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42
  • 68. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42 – Second Group: 23, 16, 54, 33
  • 69. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42 – Second Group: 23, 16, 54, 33 Sort these using Insertion Sort
  • 70. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42 18, 34, 42, 44 – Second Group: 23, 16, 54, 33 16, 23, 33, 54 Sort these using Insertion Sort
  • 71. Shell Sort • So, for example. Age = [18, 23, 34, 16, 44, 54, 42, 33]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42 18, 34, 42, 44 – Second Group: 23, 16, 54, 33 16, 23, 33, 54 Sort these using Insertion Sort
  • 72. Shell Sort • So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42 18, 34, 42, 44 – Second Group: 23, 16, 54, 33 16, 23, 33, 54 Sort these using Insertion Sort
  • 73. Shell Sort • So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54]. • Now let’s do every 2nd element: – First Group: 18, 34, 44, 42 18, 34, 42, 44 – Second Group: 23, 16, 54, 33 16, 23, 33, 54 Sort these using Insertion Sort The data is almost completely sorted now.
  • 74. Shell Sort • So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54]. • Finally do one more Insertion Sort with all elements
  • 75. Shell Sort • So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54]. • Finally do one more Insertion Sort with all elements • This will be very fast since the data is almost sorted
  • 76. Shell Sort • So, for example. Age = [18, 16, 34, 23, 42, 33, 44, 54]. • Finally do one more Insertion Sort with all elements • This will be very fast since the data is almost sorted • Age = [16, 18, 23, 33, 34, 42, 44, 54].
  • 77. Shell Sort • Let’s look at the PseudoCode in two parts: – 1) The Insertion Sort code as before, but modified not to sort all of the elements, but rather every Nth element – 2) The Shell Sort that calls the Insertion Sort code for each Nth elements, and then N/2, and N/4, and so on until 1.
  • 78. MODULE GapInsertionSort(Array, StartPos, Gap): FOR Index IN StartPos TO N INCREMENT BY Gap DO current = Array[index]; pos = index; WHILE (pos > Gap and Array[pos – Gap] > current) DO Array[pos] <- Array[pos - Gap]; pos = pos - Gap; ENDWHILE; Array[pos] = current; ENDFOR; END. Shell Sort
  • 79. MODULE GapInsertionSort(Array, StartPos, Gap): FOR Index IN StartPos TO N INCREMENT BY Gap DO current = Array[index]; pos = index; WHILE (pos > Gap and Array[pos – Gap] > current) DO Array[pos] <- Array[pos - Gap]; pos = pos - Gap; ENDWHILE; Array[pos] = current; ENDFOR; END. Shell Sort
  • 80. MODULE ShellSort(Array): GapSize <- Length(Array) DIV 2; WHILE (GapSize > 0) DO FOR StartPos IN 0 TO GapSize DO GapInsertionSort(Array, StartPos, GapSize); ENDFOR; GapSize <- GapSize DIV 2; ENDWHILE; END. Shell Sort We are reducing the Gap in half each time For each of the Nth Element, each N+1th Element, N+2th, etc. The main loop will keep going until the Gap is 1.
  • 81. Shell Sort • Complexity of Shell Sort – Best-case scenario complexity = O(N) – Average complexity = O(N * log2(N)) – Worst-case scenario complexity = O(N * log2(N))
  • 83. Merge Sort • Merge Sort was developed by John von Neumann in 1945.
  • 84. John von Neumann • Born: December 28, 1903 • Died: February 8, 1957 • Born in Budapest, Austria-Hungary • A mathematician who made major contributions to set theory, functional analysis, quantum mechanics, ergodic theory, continuous geometry, economics and game theory, computer science, numerical analysis, hydrodynamics and statistics.
  • 85. Merge Sort • Merge Sort used a “divide-and-conquer” strategy. • It’s a two-step process: – 1. Keep splitting the array in half until you end up with sub-arrays of one item (which are sorted by definition). – 2. Successively merge each sub-array together, and sort with each merge.
  • 86. Merge Sort • Let’s look at an example:
  • 87. Merge Sort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 88. Merge Sort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 89. Merge Sort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7
  • 90. Merge Sort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7
  • 91. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7
  • 92. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 0 1
  • 93. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 0 1 23 44 0 1
  • 94. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 0 1 23 44 0 1 42 33 2 3
  • 95. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 0 1 23 44 0 1 42 33 2 3 33 42 2 3
  • 96. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 0 1 23 44 0 1 42 33 2 3 33 42 2 3 16 54 4 5 16 54 4 5
  • 97. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 0 1 23 44 0 1 42 33 2 3 33 42 2 3 16 54 4 5 16 54 4 5 34 18 6 7
  • 98. Merge Sort 44 23 42 33 0 1 2 3 16 54 34 18 4 5 6 7 44 23 0 1 23 44 0 1 42 33 2 3 33 42 2 3 16 54 4 5 16 54 4 5 34 18 6 7 18 34 6 7
  • 99. Merge Sort 23 44 0 1 33 42 2 3 16 54 4 5 18 34 6 7
  • 100. Merge Sort 23 44 0 1 33 42 2 3 16 54 4 5 18 34 6 7 23 44 0 1 33 42 2 3
  • 101. Merge Sort 23 44 0 1 33 42 2 3 16 54 4 5 18 34 6 7 23 44 0 1 33 42 2 3 23 33 0 1 42 44 2 3
  • 102. Merge Sort 23 44 0 1 33 42 2 3 16 54 4 5 18 34 6 7 23 44 0 1 33 42 2 3 23 33 0 1 42 44 2 3 16 54 4 5 18 34 6 7
  • 103. Merge Sort 23 44 0 1 33 42 2 3 16 54 4 5 18 34 6 7 23 44 0 1 33 42 2 3 23 33 0 1 42 44 2 3 16 54 4 5 18 34 6 7 16 18 4 5 34 54 6 7
  • 104. Merge Sort 23 33 0 1 42 44 2 3 16 18 4 5 34 54 6 7
  • 105. Merge Sort 23 33 0 1 42 44 2 3 16 18 4 5 34 54 6 7 23 33 0 1 42 44 2 3 16 18 4 5 34 54 6 7
  • 106. Merge Sort 23 33 0 1 42 44 2 3 16 18 4 5 34 54 6 7 23 33 0 1 42 44 2 3 16 18 4 5 34 54 6 7 16 18 0 1 23 33 2 3 34 42 4 5 44 54 6 7
  • 107. PROGRAM MainMergeSort: Array = [44,23,42,33,16,54,34,18]; MergeSort(Array); PRINT Array; END. Merge Sort
  • 108. PROGRAM MergeSort(Array): IF (length(Array) > 1) THEN MidPoint = len(Age)//2 LeftHalf = Age[:MidPoint] RightHalf = Age[MidPoint:] Merge Sort Keep recursively splitting the array until you get down sub-arrays of one element. Continued 
  • 109. MergeSort(LeftHalf) MergeSort(RightHalf) LCounter = 0 RCounter = 0 MainCounter = 0 Merge Sort Recursively call MergeSort for each half of the array. After the splitting gets down to one element the recursive calls will pop off the stack to merge the sub-arrays together. Continued   Continued
  • 110. WHILE (LCounter < len(LeftHalf) AND RCounter < len(RightHalf)) DO IF LeftHalf[LCounter] < RightHalf[RCounter] THEN Age[MainCounter] = LeftHalf[LCounter]; LCounter = LCounter + 1; ELSE Age[MainCounter] = RightHalf[RCounter]; RCounter = RCounter + 1; ENDIF; MainCounter = MainCounter + 1; ENDWHILE; Merge Sort Continued   Continued Keep comparing each element of the left and right sub-array, writing the smaller element into the main array
  • 111. WHILE LCounter < len(LeftHalf) DO Age[MainCounter] = LeftHalf[LCounter]; LCounter = LCounter + 1; MainCounter = MainCounter + 1; ENDWHILE; WHILE Rcounter < len(RightHalf) DO Age[MainCounter] = RightHalf[RCounter] RCounter = RCounter + 1 MainCounter = MainCounter + 1 ENDWHILE; ENDIF; Merge Sort  Continued After the comparisons are done, write either the rest of the left array or the right array into the main array that
  • 112. Merge Sort • Complexity of Merge Sort – Best-case scenario complexity = O(N) – Average complexity = O(N * log2(N)) – Worst-case scenario complexity = O(N * log2(N))
  • 114. QuickSort • Quicksort was developed by Tony Hoare in 1959 and is still a commonly used algorithm for sorting.
  • 115. Charles Antony Richard Hoare • Born: January 11, 1934 • Hoare's most significant work includes: his sorting and selection algorithm (Quicksort and Quickselect), Hoare logic, the formal language Communicating Sequential Processes (CSP) used to specify the interactions between concurrent processes, structuring computer operating systems using the monitor concept.
  • 116. QuickSort • The key idea behind Quicksort is to pick a random value from the array, and starting from either side of the array, swap elements that are lower than the value in the right of the array with elements of the left of the array that are larger than the value, until we reach the point where the random value should be, then we put the random value in its place. • We recursively do this process with the sub-arrays on either side of the random value ( called the “pivot”).
  • 117. QuickSort • Let’s look at an example:
  • 118. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 119. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 120. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 121. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 122. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 123. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 124. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 125. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 126. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 127. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 128. QuickSort 44 23 42 33 16 54 34 18 0 1 2 3 4 5 6 7
  • 129. QuickSort 44 23 42 33 16 18 34 54 0 1 2 3 4 5 6 7
  • 130. QuickSort 44 23 42 33 16 18 34 54 0 1 2 3 4 5 6 7
  • 131. QuickSort 44 23 42 33 16 18 34 54 0 1 2 3 4 5 6 7
  • 132. QuickSort 44 23 42 33 16 18 34 54 0 1 2 3 4 5 6 7
  • 133. QuickSort 44 23 42 33 16 18 34 54 0 1 2 3 4 5 6 7
  • 134. QuickSort 34 23 42 33 16 18 44 54 0 1 2 3 4 5 6 7
  • 135. QuickSort 34 23 42 33 16 18 44 54 0 1 2 3 4 5 6 7 Pivot value is now in its correct position.
  • 136. QuickSort 34 23 42 33 16 18 44 54 0 1 2 3 4 5 6 7 Now repeat this process with this sub-array
  • 137. QuickSort 34 23 42 33 16 18 44 54 0 1 2 3 4 5 6 7 Now repeat this process with this sub-array …and this one
  • 138. QuickSort 34 23 42 33 16 18 44 54 0 1 2 3 4 5 6 7 THIS IS ALREADY SORTED!!!
  • 139. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 140. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 141. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 142. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 143. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 144. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 145. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 146. QuickSort 34 23 42 33 16 18 0 1 2 3 4 5 44 54 6 7
  • 147. QuickSort 34 23 18 33 16 42 0 1 2 3 4 5 44 54 6 7
  • 148. QuickSort 34 23 18 33 16 42 0 1 2 3 4 5 44 54 6 7
  • 149. QuickSort 34 23 18 33 16 42 0 1 2 3 4 5 44 54 6 7
  • 150. QuickSort 34 23 18 33 16 42 0 1 2 3 4 5 44 54 6 7
  • 151. QuickSort 34 23 18 33 16 42 0 1 2 3 4 5 44 54 6 7
  • 152. QuickSort 34 23 18 33 16 42 0 1 2 3 4 5 44 54 6 7
  • 153. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 154. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 155. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 Now repeat this process with this sub-array 44 54 6 7
  • 156. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 Now repeat this process with this sub-array …and this one 44 54 6 7
  • 157. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 Now repeat this process with this sub-array THIS IS ALREADY SORTED 44 54 6 7
  • 158. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 159. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 160. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 161. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7 No swaps will occur on this pass, as 16 is in the right place
  • 162. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7 …but as 16 is swapped into itself, the rest of the sub- array will be sorted.
  • 163. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7 …but as 16 is swapped into itself, the rest of the sub- array will be sorted.
  • 164. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 165. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 166. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 167. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 168. QuickSort 16 23 18 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 169. QuickSort 16 18 23 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 170. QuickSort 16 18 23 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 171. QuickSort 16 18 23 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 172. QuickSort 16 18 23 33 34 42 0 1 2 3 4 5 44 54 6 7
  • 173. QuickSort 16 18 23 33 34 42 0 1 2 3 4 5 44 54 6 7 SORTED!
  • 174. PROGRAM QuickSort(Array, First, Last): IF (First < Last) THEN Pivot = Partition(Array, First, Last); QuickSort(Array, First, Pivot - 1): QuickSort(Array, Pivot + 1, Last): ENDIF; END. QuickSort
  • 175. PROGRAM Partition(Array, First, Last): PivotVal = Array[First]; Finished = False; LeftPointer = First + 1; RightPointer = Last; QuickSort We randomly select the pivot, in this case we select the first element. Since the array isn’t sorted yet, the value of the first element could have any value Continued 
  • 176. WHILE NOT(Finished) DO WHILE (LeftPointer <= RightPointer) AND (Age[LeftPointer] <= PivotVal) DO LeftPointer = LeftPointer + 1 ENDWHILE; WHILE (Age[RightPointer] >= PivotVal) AND (RightPointer >= LeftPointer) DO RightPointer = RightPointer - 1 ENDWHILE; QuickSort Continued   Continued Keep moving left until we find a value that is less than the pivot, or we reach the Right Pointer. Keep moving right until we find a value that is greater than the pivot, or we reach the left Pointer.
  • 177. IF (LeftPointer < RightPointer) THEN Finished = False; ELSE SWAP(Age[LeftPointer], Age[RightPointer]); ENDIF; SWAP(Age[First], Age[RightPointer]); RETURN RightPointer; END Partition. QuickSort  Continued We’ve a value greater than the pivot to the left, and one less to the right, swap them Put the pivot in its correct position
  • 178. QuickSort • Complexity of Quick Sort – Best-case scenario complexity = O(N * log2(N)) – Average complexity = O(N * log2(N)) – Worst-case scenario complexity = O(N2)
  • 179. Advanced Algorithms • Sorting Algorithms – Insertion Sort – Shell Sort – Merge Sort – Quick Sort
  • 180. etc.