0% found this document useful (0 votes)
63 views

Insertion Sort and Searching: Dept. of Computer Science Faculty of Science and Technology

This document provides an overview of insertion sort and searching algorithms. It describes insertion sort, linear search, and binary search algorithms. It includes pseudocode for insertion sort and binary search, and provides simulations and analyses of the algorithms. The document also lists references and book recommendations for further reading on data structures and algorithms.

Uploaded by

ArafaT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Insertion Sort and Searching: Dept. of Computer Science Faculty of Science and Technology

This document provides an overview of insertion sort and searching algorithms. It describes insertion sort, linear search, and binary search algorithms. It includes pseudocode for insertion sort and binary search, and provides simulations and analyses of the algorithms. The document also lists references and book recommendations for further reading on data structures and algorithms.

Uploaded by

ArafaT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Insertion sort and Searching

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology
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)

Step 1:
Comparebackwards with all previous (down to 0 index) elements. If a previous
element is larger shift it forward otherwise stop comparing.
Step 2: . 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 j
44
88
55
91
11
77
99
22
66

v 22
11
33 66
33
22 66
33
22 77
66
55
44
99 77
99
66
55 91
99
77
66
11 99
91
88
77 55
99
91
88 88
99
91 44
99
0 1 2 3 4 5 6 7 8 9
Sorting
Exercise

Apply different sorting algorithms (Bubble, Selection and Insertion) to sort the
below array. Show step by step simulation.
32 12 67 33 22 88 44 25

Also find the number of comparisons, shifting or swapping for each case.
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”.

indexindex
indexindex
indexindex value 11

0 1 2 3 4 5 6 7 8 9 found false
true

33 66 22 111 77 11 99 55 88 44 position -1
5
Binary Search
Algorithm

Algorithm:
 
Input: Array, #elements (N), value(to search)
first= 0 and last= N-1
Step 0:

Step 1:
If low>high exit with status Not Found.
If then return middle with status “Found”
If then update by middle and repeat step 0.
If then update by and repeat step 0.
Binary Search
Simulation

last last last


value 77
60
first first
first
first 0
6
5
last 9
6
4 11 22 33 44 55 66 77 88 91 99
middle 7
5
6
4 0 1 2 3 4 5 6 7 8 9

found
not found middle
middle middle
middle
Search
Analysis

 Binary search is more efficient than linear search.

For array of N elements, performs at most 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,

You might also like