Data Structure and Algorithm Chap 2
Data Structure and Algorithm Chap 2
Chapter 2:
Simple Sorting & Searching Algorithms
2
Searching
Searching is an operation or a technique that helps
finds the place of a given element or value in the list.
3
Linear Searching (Sequential )
It is a very basic and simple search algorithm.
In Linear search, we search an element or value in a given
array by traversing the array from the starting, till the
desired element or value is found.
6
Linear Search (Sequential Search)
7
Sequential Search Example 1
Binary Search
• workis for both Ordered array
• log2n
• random access
Sort Algorithms
5 7
Arranging Your Hand
5 7
5 6 7
5 6 7 K
5 6 7 8 K
Insertion Sort
7 K Unsorted - shaded
Look at 2nd item - 5.
Compare 5 to 7.
7 1 5 is smaller, so move 5 to temp, leaving
5
an empty slot in position 2.
v Move 7 into the empty slot, leaving
position 1 open
7 . 5
2 >
5 7 3
<
Insertion Sort (con’t)
5 7 6 K Look at next item - 6.
Compare to 1st - 5.
6 is larger, so leave 5. Compare to next - 7.
5 7
2 >
5 6 7 3
<
Insertion Sort (con’t)
Look at next item - King.
Compare to 1st - 5.
5 6 7 K King is larger, so leave 5 where it
is.
Compare to next - 6. King is larger,
leave 6 where it so
is. Compare to next - 7. King is larger,
so
leave 7 where it is.
Insertion Sort (con’t)
5 6 7 K 8
5 6 8 1
7 K
v
5 6 7 K 8
5 6 7 K
>
2
5 6 7 8 K 3
<
Insertion Sort Exercise
5 2 4 5 1 3
Insertion Sort Illustration
Insertion sort complexity
Best Case Complexity - It occurs when there is no
sorting required, i.e. the array is already sorted.
O(n).
Average Case Complexity - It occurs when the array
elements are in jumbled order that is not properly
ascending and not properly descending.
O(n2).
Worst Case Complexity - It occurs when the array
elements are required to be sorted in reverse order.
That means suppose you have to sort the array
elements in ascending order, but its elements are in
descending order
O(n2).
Selection Sort Algorithm
12 31 25 8 32 17
Selection Sort Exercise:
Time Complexity
Best Case Complexity - It occurs when there is no sorting
required, i.e. the array is already sorted.
O(n2).
Average Case Complexity - It occurs when the array
elements are in jumbled order that is not properly
ascending and not properly descending.
O(n2).
Worst Case Complexity - It occurs when the array
elements are required to be sorted in reverse order. That
means suppose you have to sort the array elements in
ascending order, but its elements are in descending order.
O(n2).
Quick Sort
It is the most widely used algorithm and the most
efficient sorting algorithm.
It works on the divide and conquer approach, i.e., the
array is divided into subarrays, and when these subarrays
are sorted, they are combined to form a complete sorted
array.
Quick Sort: Idea
In this approach, a pivot element is selected, and the
array is partitioned into two halves based on the pivot
element.
The elements that are smaller than the pivot element are
shifted to the left side of it, and
the elements greater than the pivot element are moved to
the right side.
5 15 1 8 22 6
Quick Sort Illustration:
Quicksort complexity
Best Case Complexity - In Quicksort, the best-case
occurs when the pivot element is the middle element or
near to the middle element.
O(n*logn).
Average Case Complexity - It occurs when the array
elements are in jumbled order that is not properly
ascending and not properly descending.
O(n*logn).
Worst Case Complexity - In quick sort, worst case occurs
when the pivot element is either greatest or smallest
element. Suppose, if the pivot element is always the last
element of the array, the worst case would occur when
the given array is sorted already in ascending or
descending order.
O(n2).