Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
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 Linear search and binary search
• Sorting
• Feedback and Assessment
224-01-2020
4. 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
5. Why study sorting?
When an input is sorted, many problems
become easy e.g.,
searching, min, max, k-th smallest
24-01-2020 5
6. 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
9. 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
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
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
14. 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
15. 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
16. 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
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
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
21. 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
22. 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
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
24-01-2020 23
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
512354277 101
1 2 3 4 5
Swap42 77
24-01-2020 24
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
512357742 101
1 2 3 4 5
Swap35 77
24-01-2020 25
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
512773542 101
1 2 3 4 5
Swap12 77
24-01-2020 26
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
No need to swap
24-01-2020 27
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
577123542 101
1 2 3 4 5
Swap5 101
24-01-2020 28
29. 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
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
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
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
37. 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