SlideShare a Scribd company logo
1 of 13
Insertion sort and Searching
Course Code: CSC 2106
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 5.2 Week No: 5 Semester: Spring 20-21
Lecturer: Nazia Alfaz
nazia.alfaz@aiub.edu
Course Title: Data Structure (Theory)
Lecture Outline
1. Insertion Sort
2. Searching
• Linear Search (See Lecture 1.2)
• Binary Search
Sorting
Insertion Sort
Insertion sort: In each step, a new incoming value is inserted in the correct
position. The insertion may need to shift one or more elements to place the
element at correct position.
Algorithm:
Input: A (array), N (#elements)
𝑖 = 1
Step 1: 𝑣 = 𝐴[𝑖]
Compare 𝑣 backwards with all previous (down to 0 index) elements. If a previous
element is larger shift it forward otherwise stop comparing.
Step 2: 𝑖 = 𝑖 + 1. If 𝑖 < 𝑁 go to step 1.
Insertion Sort
Simulation
i i i i i i i i i
j j
j j j j j j j
33 66 22 99 77 11 91 55 88 44
22 33 66 77 99
11 22 33 66 77 99
91 99
55 66 77 91 99
88 91 99
44 55 66 77 88 91 99
v
66
22
99
77
11
91
55
88
44
0 1 2 8
3 4 5 6 7 9
j
Sorting
Exercise
Apply different sorting algorithms (Bubble, Selection and Insertion) to sort the
below array. Show step by step simulation.
Also find the number of comparisons, shifting or swapping for each case.
32 12 67 33 22 88 44 25
Sorting
Exercises
1: What will happen if we want to apply Bubble, Selection or Insertion sorting
algorithms to an array which is already sorted? Think in terms of comparisons,
swapping and shifting required in each case.
2: Suppose you have admission test marks of thousands of students with you.
The marks are stored in an array. If you require to print the list of names of top
100 students only which sorting algorithm will you choose?
3: In which of the algorithms, a partially sorted list found in a step does not
change in the next steps?
Searching
Introduction
Search: locate an item in a list of data/information.
Two approaches will be discussed…
1. Linear or Sequential Search:
• Searches sequentially for an element.
• Starts from the first element.
2. Binary Search:
• Searches an element by dividing the sorted elements in a list into two
sub-list
• Starts with the middle element.
Linear Search
Algorithm and Simulation
Algorithm:
Input: Array, #elements, item (to search)
Start with the element at index = 0
Step 1: Compare the element at index with item. If its equal to item then return
index with status “Found” otherwise go to step 2.
Step 2: Increase index by 1. If index is less than #elements go to step 1
otherwise return -1 with status “Not found”.
0 1 2 3 4 5 6 7 8 9
33 66 22 111 77 11 99 55 88 44
index
index
index
index index
index value
position
found
11
false
-1
true
5
Binary Search
Algorithm
Algorithm:
Input: Array, #elements (N), value(to search)
first= 0 and last= N-1
Step 0:
middle = (𝑓𝑖𝑟𝑠𝑡 + 𝑙𝑎𝑠𝑡)/2
Step 1:
If low>high exit with status Not Found.
If 𝐴𝑟𝑟𝑎𝑦[𝑚𝑖𝑑𝑑𝑙𝑒] = 𝑣𝑎𝑙𝑢𝑒 then return middle with status “Found”
If 𝐴𝑟𝑟𝑎𝑦 𝑚𝑖𝑑𝑑𝑙𝑒 < 𝑣𝑎𝑙𝑢𝑒 then update 𝑓𝑖𝑟𝑠𝑡 by middle+1 and repeat step 0.
If 𝐴𝑟𝑟𝑎𝑦 𝑚𝑖𝑑𝑑𝑙𝑒 > 𝑣𝑎𝑙𝑢𝑒 then update last by 𝑚𝑖𝑑𝑑𝑙𝑒 − 1 and repeat step 0.
Binary Search
Simulation
33 44 66 77 88 91
11 22 55 99
first
last
0 1 2 8
3 4 5 6 7 9
middle
first
last
4
0
9
7
5
9
5
5
6
6
6
6
last
first
value 77
first
middle middle
middle
middle
found
not found
60
5
4
last
Search
Analysis
Binary search is more efficient than linear search.
For array of N elements, performs at most 1 + log2 𝑁 comparisons.
Disadvantages: Requires that array elements to be sorted.
References
1. https://en.wikipedia.org/wiki/Insertion_sort
2. https://en.wikipedia.org/wiki/Sorting_algorithm
3. Nice animations are available here
https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
4. https://en.wikipedia.org/wiki/Linear_search
5. https://en.wikipedia.org/wiki/Binary_search_algorithm
Books
 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard
 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Lecture_05.2.pptx

  • 1. Insertion sort and Searching Course Code: CSC 2106 Dept. of Computer Science Faculty of Science and Technology Lecturer No: 5.2 Week No: 5 Semester: Spring 20-21 Lecturer: Nazia Alfaz nazia.alfaz@aiub.edu Course Title: Data Structure (Theory)
  • 2. Lecture Outline 1. Insertion Sort 2. Searching • Linear Search (See Lecture 1.2) • Binary Search
  • 3. Sorting Insertion Sort Insertion sort: In each step, a new incoming value is inserted in the correct position. The insertion may need to shift one or more elements to place the element at correct position. Algorithm: Input: A (array), N (#elements) 𝑖 = 1 Step 1: 𝑣 = 𝐴[𝑖] Compare 𝑣 backwards with all previous (down to 0 index) elements. If a previous element is larger shift it forward otherwise stop comparing. Step 2: 𝑖 = 𝑖 + 1. If 𝑖 < 𝑁 go to step 1.
  • 4. Insertion Sort Simulation i i i i i i i i i j j j j j j j j j 33 66 22 99 77 11 91 55 88 44 22 33 66 77 99 11 22 33 66 77 99 91 99 55 66 77 91 99 88 91 99 44 55 66 77 88 91 99 v 66 22 99 77 11 91 55 88 44 0 1 2 8 3 4 5 6 7 9 j
  • 5. Sorting Exercise Apply different sorting algorithms (Bubble, Selection and Insertion) to sort the below array. Show step by step simulation. Also find the number of comparisons, shifting or swapping for each case. 32 12 67 33 22 88 44 25
  • 6. Sorting Exercises 1: What will happen if we want to apply Bubble, Selection or Insertion sorting algorithms to an array which is already sorted? Think in terms of comparisons, swapping and shifting required in each case. 2: Suppose you have admission test marks of thousands of students with you. The marks are stored in an array. If you require to print the list of names of top 100 students only which sorting algorithm will you choose? 3: In which of the algorithms, a partially sorted list found in a step does not change in the next steps?
  • 7. Searching Introduction Search: locate an item in a list of data/information. Two approaches will be discussed… 1. Linear or Sequential Search: • Searches sequentially for an element. • Starts from the first element. 2. Binary Search: • Searches an element by dividing the sorted elements in a list into two sub-list • Starts with the middle element.
  • 8. Linear Search Algorithm and Simulation Algorithm: Input: Array, #elements, item (to search) Start with the element at index = 0 Step 1: Compare the element at index with item. If its equal to item then return index with status “Found” otherwise go to step 2. Step 2: Increase index by 1. If index is less than #elements go to step 1 otherwise return -1 with status “Not found”. 0 1 2 3 4 5 6 7 8 9 33 66 22 111 77 11 99 55 88 44 index index index index index index value position found 11 false -1 true 5
  • 9. Binary Search Algorithm Algorithm: Input: Array, #elements (N), value(to search) first= 0 and last= N-1 Step 0: middle = (𝑓𝑖𝑟𝑠𝑡 + 𝑙𝑎𝑠𝑡)/2 Step 1: If low>high exit with status Not Found. If 𝐴𝑟𝑟𝑎𝑦[𝑚𝑖𝑑𝑑𝑙𝑒] = 𝑣𝑎𝑙𝑢𝑒 then return middle with status “Found” If 𝐴𝑟𝑟𝑎𝑦 𝑚𝑖𝑑𝑑𝑙𝑒 < 𝑣𝑎𝑙𝑢𝑒 then update 𝑓𝑖𝑟𝑠𝑡 by middle+1 and repeat step 0. If 𝐴𝑟𝑟𝑎𝑦 𝑚𝑖𝑑𝑑𝑙𝑒 > 𝑣𝑎𝑙𝑢𝑒 then update last by 𝑚𝑖𝑑𝑑𝑙𝑒 − 1 and repeat step 0.
  • 10. Binary Search Simulation 33 44 66 77 88 91 11 22 55 99 first last 0 1 2 8 3 4 5 6 7 9 middle first last 4 0 9 7 5 9 5 5 6 6 6 6 last first value 77 first middle middle middle middle found not found 60 5 4 last
  • 11. Search Analysis Binary search is more efficient than linear search. For array of N elements, performs at most 1 + log2 𝑁 comparisons. Disadvantages: Requires that array elements to be sorted.
  • 12. References 1. https://en.wikipedia.org/wiki/Insertion_sort 2. https://en.wikipedia.org/wiki/Sorting_algorithm 3. Nice animations are available here https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html 4. https://en.wikipedia.org/wiki/Linear_search 5. https://en.wikipedia.org/wiki/Binary_search_algorithm
  • 13. Books  “Schaum's Outline of Data Structures with C++”. By John R. Hubbard  “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.  “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993  “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008  “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A. Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012  “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers, 2001.  “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”, Bruno R. Preiss,