SlideShare a Scribd company logo
1 of 40
Download to read offline
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

More Related Content

What's hot

Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithmmaamir farooq
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and AnalysisAkashBorse2
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data StructureTalha Shaikh
 
Alg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatricesAlg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatricesjtentinger
 
Alg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - MatricesAlg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - Matricesjtentinger
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Binary search python
Binary search pythonBinary search python
Binary search pythonMaryamAnwar10
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHISowmya Jyothi
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHISowmya Jyothi
 

What's hot (20)

Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Sorting
SortingSorting
Sorting
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Searching
SearchingSearching
Searching
 
Binary search
Binary searchBinary search
Binary search
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Excel formula
Excel formulaExcel formula
Excel formula
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Alg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatricesAlg II Unit 3-6-solvingsystemsmatrices
Alg II Unit 3-6-solvingsystemsmatrices
 
Alg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - MatricesAlg II 3-6 Solving Systems - Matrices
Alg II 3-6 Solving Systems - Matrices
 
Binary Search
Binary SearchBinary Search
Binary Search
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Binary search python
Binary search pythonBinary search python
Binary search python
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
 

Similar to L 14-ct1120

Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
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 analysisRadhika Talaviya
 
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 structuresPRIANKA R
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptxTusharTikia
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptxAnSHiKa187943
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...BhumikaBiyani1
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sortKrish_ver2
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptxSujan527908
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 

Similar to L 14-ct1120 (20)

Bubble sort
Bubble sortBubble sort
Bubble sort
 
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
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
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
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptx
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
Sorting
SortingSorting
Sorting
 
1.4 Sorting.pptx
1.4 Sorting.pptx1.4 Sorting.pptx
1.4 Sorting.pptx
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
Sorting
SortingSorting
Sorting
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 

More from Zia Ush Shamszaman

Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2Zia Ush Shamszaman
 
Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1Zia Ush Shamszaman
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...Zia Ush Shamszaman
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...Zia Ush Shamszaman
 

More from Zia Ush Shamszaman (11)

Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015Pdfslide.net book of-abstracts-insight-student-conference-2015
Pdfslide.net book of-abstracts-insight-student-conference-2015
 
Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3Hacking with Backtrack Lecture-3
Hacking with Backtrack Lecture-3
 
Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2Hacking with Backtrack Lecture-2
Hacking with Backtrack Lecture-2
 
Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1Hacking with Backtrack Lecture-1
Hacking with Backtrack Lecture-1
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...
 
On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...On the need for applications aware adaptive middleware in real-time RDF data ...
On the need for applications aware adaptive middleware in real-time RDF data ...
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
 
L 18 ct1120
L 18 ct1120L 18 ct1120
L 18 ct1120
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
L 15 ct1120
L 15 ct1120L 15 ct1120
L 15 ct1120
 
Bangladesh
BangladeshBangladesh
Bangladesh
 

Recently uploaded

Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...nirzagarg
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schscnajjemba
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...nirzagarg
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...gajnagarg
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制vexqp
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxchadhar227
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样wsppdmt
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...gajnagarg
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........EfruzAsilolu
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制vexqp
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubaikojalkojal131
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdftheeltifs
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样wsppdmt
 

Recently uploaded (20)

Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptx
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdf
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
 
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
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
  • 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 •  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
  • 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 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
  • 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 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
  • 32. “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
  • 33. 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
  • 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
  • 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