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
 
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
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 

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
 
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
 
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)
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
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

Thiophen Mechanism khhjjjjjjjhhhhhhhhhhh
Thiophen Mechanism khhjjjjjjjhhhhhhhhhhhThiophen Mechanism khhjjjjjjjhhhhhhhhhhh
Thiophen Mechanism khhjjjjjjjhhhhhhhhhhhYasamin16
 
Vision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptxVision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptxellehsormae
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGILLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGIThomas Poetter
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...Amil Baba Dawood bangali
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Boston Institute of Analytics
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...ssuserf63bd7
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxaleedritatuxx
 
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024Timothy Spann
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...GQ Research
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 

Recently uploaded (20)

Thiophen Mechanism khhjjjjjjjhhhhhhhhhhh
Thiophen Mechanism khhjjjjjjjhhhhhhhhhhhThiophen Mechanism khhjjjjjjjhhhhhhhhhhh
Thiophen Mechanism khhjjjjjjjhhhhhhhhhhh
 
Vision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptxVision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptx
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGILLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
 
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 

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