DSA-Chapter 3 - Sorting and Searching Algorithms
DSA-Chapter 3 - Sorting and Searching Algorithms
SORTING AND
SEARCHING
ALGORITHMS
DSA- CH-3: Sorting and Searching Algorithms
CH-3 Contents
1. Searching and Sorting Basic
2. Searching Algorithm
3. Sorting Algorithms
4. Efficient sorting algorithms
CH-3 Contents
1. Searching and Sorting Basic
2. Searching Algorithm
3. Sorting Algorithms
4. Efficient sorting algorithms
Searching and Sorting Basic
Searching
Searching is a process of looking for a specific
element in a list of items or determining that the item is
not in the list.
There are two simple searching algorithms:
Sequential Search, and
Binary Search
Sorting
Sorting is a process of reordering a list of items in either
increasing or decreasing order.
It is one of the most important operations performed by
computers.
Simple sorting algorithms used to sort small-sized
lists.
Insertion Sort
Selection Sort
Bubble Sort
CH-3 Contents
1. Searching and Sorting Basic
2. Searching Algorithm
3. Sorting Algorithms
4. Efficient sorting algorithms
Linear Search (Sequential
Search)
Linear search is a very simple search algorithm.
In this type of search, a sequential search is made over
all items one by one.
Every item is checked and if a match is found then
that particular item is returned, otherwise the
search continues till the end of the data collection.
Algorithm : Linear Search
Linear Search ( Array list, Value key)
Step 1: Set i to 0
Step 2: if i > n then go to step 7
Step 3: if list[i] = key then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element key Found at index i and go to step
8
Step 7: Print element not found
Step 8: Exit
Implementation: Linear search
Example with illustration: Linear Search
Binary Search
Binary search is a fast search algorithm with run-
time complexity of Ο(log n).
This search algorithm works on the principle of divide
and conquer.
For this algorithm to work properly, the data collection
should be in the sorted form.
Basic Idea: Binary Search
Binary search looks for a particular item by comparing the
middle most item of the collection.
If a match occurs, then the index of item is returned.
If the item is greater than the middle item, then the item
is searched in the sub-array to the right of the middle
item.
Otherwise, the item is searched for in the sub-array to the
left of the middle item.
This process continues on the sub-array as well until the
size of the sub-array reduces to zero.
Implementation: Binary Search
Example with illustration: Binary Search
Analysis: Searching Algorithm
Time Complexity
Type Best Case Expected
Linear Search O(1) O(n)
Binary Search O(1) O(log n)
Space Complexity
Type Space
Complexity
Linear Search O(1)
Binary Search O(1)
Exercise
Search an element “55” from the given data using
binary searching?
CH-3 Contents
1. Searching and Sorting Basic
2. Searching Algorithm
3. Sorting Algorithms
4. Efficient sorting algorithms
Sorting Algorithm
Sorting algorithm specifies the way to arrange data in a particular
order.
Most common orders are in numerical or lexicographical order.
Important Terms
Increasing Order
A sequence of values is said to be in increasing order, if the successive
element is greater than the previous one. For example, 1, 3, 4, 6, 8, 9 are
in increasing order, as every next element is greater than the previous
element.
Decreasing Order
A sequence of values is said to be in decreasing order, if the successive
element is less than the current one. For example, 9, 8, 6, 4, 3, 1 are in
decreasing order, as every next element is less than the previous element.
Non-Increasing Order
A sequence of values is said to be in non-increasing order, if the
successive element is less than or equal to its previous element in the
sequence. This order occurs when the sequence contains duplicate
values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every
next element is less than or equal to (in case of 3) but not greater than
any previous element.
Non-Decreasing Order
A sequence of values is said to be in non-decreasing order, if the
successive element is greater than or equal to its previous element in
the sequence. This order occurs when the sequence contains duplicate
values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every
next element is greater than or equal to (in case of 3) but not less than
the previous one.
Insertion Sort
This is an in-place comparison-based sorting algorithm. Here, a
sub-list is maintained which is always sorted.
For example, the lower part of an array is maintained to be sorted.
An element which is to be 'insert'ed in this sorted sub-list, has
to find its appropriate place and then it has to be inserted there.
Hence the name, insertion sort.
The array is searched sequentially and unsorted items are
moved and inserted into the sorted sub-list (in the same
array). This algorithm is not suitable for large data sets as its
average and worst case complexity are of Ο(n2), where n is the
number of items.
Algorithm : Insertion Sort
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
Implementation: Insertion Sort
Example with illustration: Insertion Sort
Selection Sort
Selection sort is a simple sorting algorithm.
It is an in-place comparison-based algorithm in which
the list is divided into two parts,
The sorted part at the left end and the unsorted part at
the right end.
Initially, the sorted part is empty and the unsorted
part is the entire list.
The smallest element is selected from the unsorted
array and swapped with the leftmost element, and that
element becomes a part of the sorted array.
…
This process continues moving unsorted array
boundary by one element to the right.
This algorithm is not suitable for large data sets as its
average and worst case complexities are of O(n2),
where n is the number of items.
Algorithm: Selection Sort
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
Implementation: Selection Sort
Example with illustration: Selection Sort
Bubble Sort
Bubble sort is a simple sorting algorithm.
This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are
not in order.
This algorithm is not suitable for large data sets as
its average and worst case complexity are of O(n 2)
where n is the number of items.
Algorithm: Bubble Sort
Step 1 − Loop through array from i=0 to n
Step 2 − Pick two adjacent element
Step 3 – Compare the elements
Step 4 – Swap the adjacent elements if they are out
of order
Step 5 − Repeat until list is sorted
Implementation: Bubble Sort
Example with illustration: Bubble Sort
Analysis: Sorting Algorithm
A comparison of the asymptotic complexities
for three simple sorting
Exercise
Sort the following data
Element 1 2 3 4 5 6 7 8
Data 27 63 1 72 64 58 14 9
1. Insertion Sort
2. Selection Sort
3. Bubble Sort
CH-3 Contents
1. Searching and Sorting Basic
2. Searching Algorithm
3. Sorting Algorithms
4. Efficient sorting algorithms
Efficient sorting algorithms
Most sorting algorithms work by comparing the data being
sorted.
In some cases, it may be desirable to sort a large chunk of data (for
instance, a struct containing a name and address) based on only a portion
of that data.
The piece of data actually used to determine the sorted order is called the
key.
Sorting algorithms are usually judged by their efficiency.
In this case, efficiency refers to the algorithmic efficiency as the size of the
input grows large and is generally based on the number of elements to sort.
Example
Shell Sort, Heap Sort, Quick Sort, Merge Sort, Radix Sort
ASSIGNMENT (10%)
Efficient Sorting Algorithm
Title
Type of Sorting Assigned Group
1. Shell Sort 1. G1
2. Heap Sort 2. G2
3. Quick Sort 3. G3
4. Merge Sort 4. G4
5. Radix Sort 5. G5
6. Shell Sort 6. G6
7. Heap Sort 7. G7
8. Quick Sort 8. G8
9. Merge Sort 9. G9
10. Radix Sort 10. G10
Due Date: Submission and Presentation
Tentative
Friday, June 24, 2022
What you submit?