First Year BA (IT)
CT1120 Algorithms
Lecture 14
Dr. Zia Ush Shamszaman
z.shamszaman1@nuigalway.ie
1
School of Computer Science, College of Science and Engineering
24-01-2020
Overview
•  Review Linear search and binary search
•  Sorting
•  Feedback and Assessment
224-01-2020
Sorting
24-01-2020 3
Sorting
•  Sorting is a process in which records are
arranged in ascending or descending order
512354277 101
1 2 3 4 5
5 12 35 42 77 101
1 2 3 4 5
6
24-01-2020 4
Why study sorting?
When an input is sorted, many problems
become easy e.g.,
searching, min, max, k-th smallest
24-01-2020 5
Some Applications of Sorting
•  Uniqueness testing
•  Deleting duplicates
•  Prioritizing events
•  Frequency counting
•  Reconstructing the original order
•  Set intersection/union
•  Efficient searching
24-01-2020 6
Types of sorting
•  Selection sort
•  Insertion sort
•  Bubble sort
•  Merge sort
•  Quick sort
•  Heap sort
•  Shell sort
24-01-2020 7
Types of sorting
•  Selection sort
•  Insertion sort
•  Bubble sort
•  Merge sort
•  Quick sort
•  Heap sort
•  Shell sort
24-01-2020 8
Selection Sort
•  Selection sort is a sorting algorithm which
works as follows:
–  Find the minimum value in the list
–  Swap it with the value in the first
position
–  Repeat the steps above for remainder of
the list (starting at the second position)
24-01-2020 9
Example: Selection Sort
•  26 33 43 100 46 88 52 17 53 77
•  17 | 33 43 100 46 88 52 26 53 77
•  17 26 | 43 100 46 88 52 33 53 77
•  17 26 33 | 100 46 88 52 43 53 77
•  17 26 33 43 | 46 88 52 100 53 77
•  17 26 33 43 46 | 88 52 100 53 77
•  17 26 33 43 46 52 | 88 100 53 77
•  17 26 33 43 46 52 53 | 100 88 77
•  17 26 33 43 46 52 53 77 | 88 100
•  17 26 33 43 46 52 53 77 88 | 100
24-01-2020 10
Selection Sort Algorithm
•  Step 1 − Set MIN to location 0
•  Step 2 − Search the minimum element in the list
•  Step 3 − Swap with value at location MIN
•  Step 4 − Increment MIN to point to next element
•  Step 5 − Repeat until list is sorted
24-01-2020 11
Selection Sort Pseudocode
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
/* check the element to be minimum */
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
/* swap the minimum element with the current element*/
if indexMin != i then
swap list[min] and list[i]
end if
end for
end procedure
24-01-2020 12
Insertion Sort
24-01-2020 13
Insertion Sort
•  In insertion sort, each successive element in
the array to be sorted is inserted into its
proper place with respect to the other, already
sorted elements.
•  We divide our array in a sorted and an unsorted
array.
•  Initially the sorted portion contains only one
element: the first element in the array.
•  We take the second element in the array, and
put it into its correct place.
24-01-2020 14
Insertion Sort
•  That is, array[0] and array[1] are in order
with respect to each other.
•  Then the value in array[2] is put into its
proper place, so array [0]…. array[2] is sorted
and so on.
36
24
10
6
12
36
24
10
6
12
10
24
36
6
12
6
10
24
36
12
6
10
12
24
36
24
36
10
6
12
24-01-2020 15
Insertion Sort
•  Our strategy is to search for insertion
point from the beginning of the array and
shift the element down to make room for
new element
•  We compare the item at array[current] to
one before it, and swap if it is less.
•  We then compare array[current-1] to one
before it and swap if necessary.
24-01-2020 16
Example: Insertion Sort
•  99 | 55 4 66 28 31 36 52 38 72
•  55 99 | 4 66 28 31 36 52 38 72
•  4 55 99 | 66 28 31 36 52 38 72
•  4 55 66 99 | 28 31 36 52 38 72
•  4 28 55 66 99 | 31 36 52 38 72
•  4 28 31 55 66 99 | 36 52 38 72
•  4 28 31 36 55 66 99 | 52 38 72
•  4 28 31 36 52 55 66 99 | 38 72
•  4 28 31 36 38 52 55 66 99 | 72
•  4 28 31 36 38 52 55 66 72 99 |
24-01-2020 17
Insertion Sort Algorithm
•  Step 1 − If it is the first element, it is already
sorted. return 1;
•  Step 2 − Pick next element
•  Step 3 − Compare with all elements in the sorted
sub-list
•  Step 4 − Shift all the elements in the sorted sub-
list that is greater than the
•  value to be sorted
•  Step 5 − Insert the value
•  Step 6 − Repeat until list is sorted
24-01-2020 18
Insertion Sort Pseudocode
procedure insertionSort( A : array of items )
int holePosition
int valueToInsert
for i = 1 to length(A) inclusive do:
/* select value to be inserted */
valueToInsert = A[i]
holePosition = i
/*locate hole position for the element to be inserted */
while holePosition > 0 and A[holePosition-1] > valueToInsert do:
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while
/* insert the number at hole position */
A[holePosition] = valueToInsert
end for
end procedure
24-01-2020 19
Bubble Sort
24-01-2020 20
Bubble Sort
•  Bubble sort is similar to selection sort in the
sense that it repeatedly finds the largest/
smallest value in the unprocessed portion of
the array and puts it back.
•  However, finding the largest value is not done
by selection this time.
•  We "bubble" up the largest value instead.
24-01-2020 21
Bubble Sort
•  Compares adjacent items and exchanges
them if they are out of order.
•  Comprises of several passes.
•  In one pass, the largest value has been
“bubbled” to its proper position.
•  In second pass, the last value does not
need to be compared.
24-01-2020 22
Bubble Sort
•  Traverse a collection of elements
–  Move from the front to the end
–  “Bubble” the largest value to the end
using pair-wise comparisons and swapping
512354277 101
1 2 3 4 5
24-01-2020 23
Bubble Sort
•  Traverse a collection of elements
–  Move from the front to the end
–  “Bubble” the largest value to the end
using pair-wise comparisons and swapping
512354277 101
1 2 3 4 5
Swap42 77
24-01-2020 24
Bubble Sort
•  Traverse a collection of elements
–  Move from the front to the end
–  “Bubble” the largest value to the end
using pair-wise comparisons and swapping
512357742 101
1 2 3 4 5
Swap35 77
24-01-2020 25
Bubble Sort
•  Traverse a collection of elements
–  Move from the front to the end
–  “Bubble” the largest value to the end
using pair-wise comparisons and swapping
512773542 101
1 2 3 4 5
Swap12 77
24-01-2020 26
Bubble Sort
•  Traverse a collection of elements
–  Move from the front to the end
–  “Bubble” the largest value to the end
using pair-wise comparisons and swapping
577123542 101
1 2 3 4 5
No need to swap
24-01-2020 27
Bubble Sort
•  Traverse a collection of elements
–  Move from the front to the end
–  “Bubble” the largest value to the end
using pair-wise comparisons and swapping
577123542 101
1 2 3 4 5
Swap5 101
24-01-2020 28
Bubble Sort
•  Traverse a collection of elements
–  Move from the front to the end
–  “Bubble” the largest value to the end
using pair-wise comparisons and swapping
77123542 5
1 2 3 4 5
101
Largest value correctly placed
24-01-2020 29
Items of Interest
•  Notice that only the largest value is
correctly placed
•  All other values are still out of order
•  So we need to repeat this process
77123542 5
1 2 3 4 5
101
Largest value correctly placed
24-01-2020 30
Repeat “Bubble Up” How Many
Times?
•  If we have N elements…
•  And if each time we bubble an element, we
place it in its correct location…
•  Then we repeat the “bubble up” process N – 1
times.
•  This guarantees we’ll correctly place all N
elements.
24-01-2020 31
“Bubbling” All the Elements
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101
4235125 77
1 2 3 4 5 6
101
N-1
24-01-2020 32
Reducing the Number of
Comparisons
12354277 101
1 2 3 4 5 6
5
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101
24-01-2020 33
Already Sorted Collections?
•  What if the collection was already sorted?
•  What if only a few elements were out of place and
after a couple of “bubble ups,” the collection was
sorted?
•  We want to be able to detect this and “stop early”!
4235125 77
1 2 3 4 5
101
24-01-2020 34
Using a Boolean “Flag”
•  We can use a boolean variable to determine
if any swapping occurred during the
“bubble up.”
•  If no swapping occurred, then we know that
the collection is already sorted!
•  This boolean “flag” needs to be reset after
each “bubble up.”
24-01-2020 35
Bubble Sort Algorithm
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
24-01-2020 36
Bubble Sort Pseudocode
procedure bubbleSort( list : array of items )
loop = list.count;
for i = 0 to loop-1 do:
swapped = false
for j = 0 to loop-1 do:
/* compare the adjacent elements */
if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if
end for
24-01-2020 37
/*if no number was swapped that means 	
array is sorted now, break the loop.*/	
	
if(not swapped) then	
break	
end if	
	
end for	
	
end procedure return list
Next Lecture
•  Sorting
•  Data Structures
24-01-2020 38
Useful Links	
•  http://www.csanimated.com/animation.php?t=Quicksort
•  http://www.hakansozer.com/category/c/
•  http://www.nczonline.net/blog/2012/11/27/computer-science-in-
javascript-quicksort/
•  http://www.sorting-algorithms.com/shell-sort
•  https://www.tutorialspoint.com/
•  https://www.hackerearth.com/
•  www.khanacademy.org/computing/computer-science/algorithms
24-01-2020 39
Feedback & Assessment
24-01-2020 40

L 14-ct1120

  • 1.
    First Year BA(IT) CT1120 Algorithms Lecture 14 Dr. Zia Ush Shamszaman z.shamszaman1@nuigalway.ie 1 School of Computer Science, College of Science and Engineering 24-01-2020
  • 2.
    Overview •  Review Linearsearch and binary search •  Sorting •  Feedback and Assessment 224-01-2020
  • 3.
  • 4.
    Sorting •  Sorting isa process in which records are arranged in ascending or descending order 512354277 101 1 2 3 4 5 5 12 35 42 77 101 1 2 3 4 5 6 24-01-2020 4
  • 5.
    Why study sorting? Whenan input is sorted, many problems become easy e.g., searching, min, max, k-th smallest 24-01-2020 5
  • 6.
    Some Applications ofSorting •  Uniqueness testing •  Deleting duplicates •  Prioritizing events •  Frequency counting •  Reconstructing the original order •  Set intersection/union •  Efficient searching 24-01-2020 6
  • 7.
    Types of sorting • Selection sort •  Insertion sort •  Bubble sort •  Merge sort •  Quick sort •  Heap sort •  Shell sort 24-01-2020 7
  • 8.
    Types of sorting • Selection sort •  Insertion sort •  Bubble sort •  Merge sort •  Quick sort •  Heap sort •  Shell sort 24-01-2020 8
  • 9.
    Selection Sort •  Selectionsort is a sorting algorithm which works as follows: –  Find the minimum value in the list –  Swap it with the value in the first position –  Repeat the steps above for remainder of the list (starting at the second position) 24-01-2020 9
  • 10.
    Example: Selection Sort • 26 33 43 100 46 88 52 17 53 77 •  17 | 33 43 100 46 88 52 26 53 77 •  17 26 | 43 100 46 88 52 33 53 77 •  17 26 33 | 100 46 88 52 43 53 77 •  17 26 33 43 | 46 88 52 100 53 77 •  17 26 33 43 46 | 88 52 100 53 77 •  17 26 33 43 46 52 | 88 100 53 77 •  17 26 33 43 46 52 53 | 100 88 77 •  17 26 33 43 46 52 53 77 | 88 100 •  17 26 33 43 46 52 53 77 88 | 100 24-01-2020 10
  • 11.
    Selection Sort Algorithm • Step 1 − Set MIN to location 0 •  Step 2 − Search the minimum element in the list •  Step 3 − Swap with value at location MIN •  Step 4 − Increment MIN to point to next element •  Step 5 − Repeat until list is sorted 24-01-2020 11
  • 12.
    Selection Sort Pseudocode procedureselection sort list : array of items n : size of list for i = 1 to n - 1 /* set current element as minimum*/ min = i /* check the element to be minimum */ for j = i+1 to n if list[j] < list[min] then min = j; end if end for /* swap the minimum element with the current element*/ if indexMin != i then swap list[min] and list[i] end if end for end procedure 24-01-2020 12
  • 13.
  • 14.
    Insertion Sort •  Ininsertion sort, each successive element in the array to be sorted is inserted into its proper place with respect to the other, already sorted elements. •  We divide our array in a sorted and an unsorted array. •  Initially the sorted portion contains only one element: the first element in the array. •  We take the second element in the array, and put it into its correct place. 24-01-2020 14
  • 15.
    Insertion Sort •  Thatis, array[0] and array[1] are in order with respect to each other. •  Then the value in array[2] is put into its proper place, so array [0]…. array[2] is sorted and so on. 36 24 10 6 12 36 24 10 6 12 10 24 36 6 12 6 10 24 36 12 6 10 12 24 36 24 36 10 6 12 24-01-2020 15
  • 16.
    Insertion Sort •  Ourstrategy is to search for insertion point from the beginning of the array and shift the element down to make room for new element •  We compare the item at array[current] to one before it, and swap if it is less. •  We then compare array[current-1] to one before it and swap if necessary. 24-01-2020 16
  • 17.
    Example: Insertion Sort • 99 | 55 4 66 28 31 36 52 38 72 •  55 99 | 4 66 28 31 36 52 38 72 •  4 55 99 | 66 28 31 36 52 38 72 •  4 55 66 99 | 28 31 36 52 38 72 •  4 28 55 66 99 | 31 36 52 38 72 •  4 28 31 55 66 99 | 36 52 38 72 •  4 28 31 36 55 66 99 | 52 38 72 •  4 28 31 36 52 55 66 99 | 38 72 •  4 28 31 36 38 52 55 66 99 | 72 •  4 28 31 36 38 52 55 66 72 99 | 24-01-2020 17
  • 18.
    Insertion Sort Algorithm • Step 1 − If it is the first element, it is already sorted. return 1; •  Step 2 − Pick next element •  Step 3 − Compare with all elements in the sorted sub-list •  Step 4 − Shift all the elements in the sorted sub- list that is greater than the •  value to be sorted •  Step 5 − Insert the value •  Step 6 − Repeat until list is sorted 24-01-2020 18
  • 19.
    Insertion Sort Pseudocode procedureinsertionSort( A : array of items ) int holePosition int valueToInsert for i = 1 to length(A) inclusive do: /* select value to be inserted */ valueToInsert = A[i] holePosition = i /*locate hole position for the element to be inserted */ while holePosition > 0 and A[holePosition-1] > valueToInsert do: A[holePosition] = A[holePosition-1] holePosition = holePosition -1 end while /* insert the number at hole position */ A[holePosition] = valueToInsert end for end procedure 24-01-2020 19
  • 20.
  • 21.
    Bubble Sort •  Bubblesort is similar to selection sort in the sense that it repeatedly finds the largest/ smallest value in the unprocessed portion of the array and puts it back. •  However, finding the largest value is not done by selection this time. •  We "bubble" up the largest value instead. 24-01-2020 21
  • 22.
    Bubble Sort •  Comparesadjacent items and exchanges them if they are out of order. •  Comprises of several passes. •  In one pass, the largest value has been “bubbled” to its proper position. •  In second pass, the last value does not need to be compared. 24-01-2020 22
  • 23.
    Bubble Sort •  Traversea collection of elements –  Move from the front to the end –  “Bubble” the largest value to the end using pair-wise comparisons and swapping 512354277 101 1 2 3 4 5 24-01-2020 23
  • 24.
    Bubble Sort •  Traversea collection of elements –  Move from the front to the end –  “Bubble” the largest value to the end using pair-wise comparisons and swapping 512354277 101 1 2 3 4 5 Swap42 77 24-01-2020 24
  • 25.
    Bubble Sort •  Traversea collection of elements –  Move from the front to the end –  “Bubble” the largest value to the end using pair-wise comparisons and swapping 512357742 101 1 2 3 4 5 Swap35 77 24-01-2020 25
  • 26.
    Bubble Sort •  Traversea collection of elements –  Move from the front to the end –  “Bubble” the largest value to the end using pair-wise comparisons and swapping 512773542 101 1 2 3 4 5 Swap12 77 24-01-2020 26
  • 27.
    Bubble Sort •  Traversea collection of elements –  Move from the front to the end –  “Bubble” the largest value to the end using pair-wise comparisons and swapping 577123542 101 1 2 3 4 5 No need to swap 24-01-2020 27
  • 28.
    Bubble Sort •  Traversea collection of elements –  Move from the front to the end –  “Bubble” the largest value to the end using pair-wise comparisons and swapping 577123542 101 1 2 3 4 5 Swap5 101 24-01-2020 28
  • 29.
    Bubble Sort •  Traversea collection of elements –  Move from the front to the end –  “Bubble” the largest value to the end using pair-wise comparisons and swapping 77123542 5 1 2 3 4 5 101 Largest value correctly placed 24-01-2020 29
  • 30.
    Items of Interest • Notice that only the largest value is correctly placed •  All other values are still out of order •  So we need to repeat this process 77123542 5 1 2 3 4 5 101 Largest value correctly placed 24-01-2020 30
  • 31.
    Repeat “Bubble Up”How Many Times? •  If we have N elements… •  And if each time we bubble an element, we place it in its correct location… •  Then we repeat the “bubble up” process N – 1 times. •  This guarantees we’ll correctly place all N elements. 24-01-2020 31
  • 32.
    “Bubbling” All theElements 77123542 5 1 2 3 4 5 6 101 5421235 77 1 2 3 4 5 6 101 4253512 77 1 2 3 4 5 6 101 4235512 77 1 2 3 4 5 6 101 4235125 77 1 2 3 4 5 6 101 N-1 24-01-2020 32
  • 33.
    Reducing the Numberof Comparisons 12354277 101 1 2 3 4 5 6 5 77123542 5 1 2 3 4 5 6 101 5421235 77 1 2 3 4 5 6 101 4253512 77 1 2 3 4 5 6 101 4235512 77 1 2 3 4 5 6 101 24-01-2020 33
  • 34.
    Already Sorted Collections? • What if the collection was already sorted? •  What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? •  We want to be able to detect this and “stop early”! 4235125 77 1 2 3 4 5 101 24-01-2020 34
  • 35.
    Using a Boolean“Flag” •  We can use a boolean variable to determine if any swapping occurred during the “bubble up.” •  If no swapping occurred, then we know that the collection is already sorted! •  This boolean “flag” needs to be reset after each “bubble up.” 24-01-2020 35
  • 36.
    Bubble Sort Algorithm beginBubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort 24-01-2020 36
  • 37.
    Bubble Sort Pseudocode procedurebubbleSort( list : array of items ) loop = list.count; for i = 0 to loop-1 do: swapped = false for j = 0 to loop-1 do: /* compare the adjacent elements */ if list[j] > list[j+1] then /* swap them */ swap( list[j], list[j+1] ) swapped = true end if end for 24-01-2020 37 /*if no number was swapped that means array is sorted now, break the loop.*/ if(not swapped) then break end if end for end procedure return list
  • 38.
    Next Lecture •  Sorting • Data Structures 24-01-2020 38
  • 39.
    Useful Links •  http://www.csanimated.com/animation.php?t=Quicksort • http://www.hakansozer.com/category/c/ •  http://www.nczonline.net/blog/2012/11/27/computer-science-in- javascript-quicksort/ •  http://www.sorting-algorithms.com/shell-sort •  https://www.tutorialspoint.com/ •  https://www.hackerearth.com/ •  www.khanacademy.org/computing/computer-science/algorithms 24-01-2020 39
  • 40.