Sorting and Searching Ppt
Sorting and Searching Ppt
Algorithms
Dr. Akash Kumar
Gautam Buddha University, Greater Noida
Objective
This lab aims to implement various searching and sorting algorithms
and analyze their time complexity.
• Linear Search
• Binary Search
• Bubble sort
• Insertion Sort
• Selection Sort
• Quick Sort
• Merge Sort
Searching
• Searching is a methods to find the element in any data structure like array, linked-list, tree,
graph.
Sequential Binary
Search Search
Sequential Search Example
• Comparing the “target element” with middle most item of the array.
• Middle element=(low+high)/2
• If the middle element > “target element”, then search the target element from
the left side of the array. Otherwise, Search from the right side of the array.
In linear search input data need not to be in sorted. In binary search input data need to be in sorted order.
The time complexity of linear search O(n). The time complexity of binary search O(log n).
Non-Increasing Order:- Successive element is less than or equal to the previous one. For example,
60, 45, 35, 35, 25, 10
Sorting
● Sorting refers to arranging data in a particular format.
● Sorting algorithm specifies the way to arrange data in a particular
order.
● The choice of the algorithm depends on the criteria of the task.
Analysis Criteria for Sorting
Algorithm
Time Complexity
Space Complexity (In-place and Not-In-place sorting)
Comparison sorting
Stable and Unstable sorting
Internal and External sorting
Recursive and Non-Recursive sorting
Adaptive and Non-Adaptive sorting
Analysis Criteria for Sorting Algorithm
Time Complexity :- The best and worst case time complexity of the sorting algorithms is O(n) and O().
Space Complexity
In-place sorting:- Not extra space require for comparison and temporary storage of few data
elements. For example:- Bubble, Insertion, Selection, Quick sort algorithm
Not-in-place sorting:- Require some extra space for comparison and temporary storage of few
data elements. For example:- Merge sort
Comparison sorting:- Example:- Bubble, Insertion, Selection, Quick sort algorithm
Stable:- After sorting the contents, does not change the sequence of similar content in
which they appear.
Unstable sorting:- After sorting the contents, change the sequence of similar content in
which they appear.
Stable Sorting
25 10 35 45 60 35
10 25 35 35 45 60
Unstable Sorting
25 10 35 45 60 35
10 25 35 35 45 60
Stable Sorting
2 1 1 4 5 3
1 1 2 3 4 5
Unstable Sorting
2 1 1 4 5 3
1 1 2 3 4 5
Analysis Criteria for Sorting
Algorithm
Internal sorting:- All the data is loaded into the memory
External sorting :- Not Loaded into the memory
Non-Adaptive sorting :- Does not take into account the elements which are already
sorted & force every single element to be re-ordered to confirm their sortedness.
Few important Sorting Algorithms
● Bubble Sort
● Selection Sort
● Insertion Sort
● Merge Sort
● Quick Sort
Bubble Sort
● Each pair of adjacent elements is compared and swapped them if they are in
wrong order.
● The algorithm repeats this process until the list is sorted.
Bubble Sort Example
Bubble Sort Algorithm (Code)
}
Time complexity
7 4 5 9 8 2 1 Unsorted
min swap
1 4 5 9 8 2 7
min swap
1 2 5 9 8 4 7
swap
min
1 2 4 9 8 5 7
min swap
1 2 4 5 8 9 7
min swap
1 2 4 5 7 9 8
1 2 4 5 7 8 9 Sorted
Selection Sort Algorithm (Function)
● Poor performance.
● Not suitable for most real-world scenarios.
● Unstable
Insertion Sort
3rd Pass 11 12 13 5 6 11 12 5 13 6
11 5 12 13 6
4th Pass 5 11 12 13 6 5 11 12 6 13
5 11 6 12 13
}
}
}}
Time complexity
● Not suitable for most real-world scenarios: Due to its poor performance,
insertion sort is not suitable for use in most real-world scenarios where larger
datasets are involved. It is primarily used for educational purposes and small
datasets.
Merge Sort
Divide
Conquer
Merge Sort Algorithm
● It is a stable sort.
● Suitable for sorting large data sets.
● The divide and conquer strategy allows sorting elements in a parallel way,
which can be very beneficial in certain scenarios where the algorithm needs to
be efficient and running on multiple cores.
Merge Sort Disadvantages
● Not-In-Place
● It may not be efficient for small arrays, as the overhead of the divide-and-
conquer strategy can outweigh the benefits for small data sets.
● It's not always the best choice when working with real-time systems where
memory and time constraints are strict.
Quick Sort
● Quick sort uses divide and conquer to gain the same advantages as the merge
sort, while not using additional storage.
● It selects a "pivot" element from the array and partition the other elements into
two parts:
1. less than the pivot in the left side
2. greater than the pivot in right side.
● The pivot element is then in its correct position in the sorted array.
● The partitioning step is then repeated recursively on the two sub-arrays until
the entire array is sorted.
Quick Sort Example
Quick Sort Algorithm
● Always picks the middle element as the pivot. Time complexity on that
scenario is θ(nlogn).
● Sorted list (Worst Case): Select the first or last element is always picked as
a pivot. Time complexity on that case is O().
Best and Average case:- Time
complexity
T(n)=2T(n/2)+n; (1)
T(n/2)=2T(n/2^2)+n/2; (2)
Put T(n/2) value in equation 1.
=2(2T(n/4)+n/2) +n
= 2^2T(n/2^2)+ n+n
= 2^2T(n/2^2)+ 2*n
= 2^iT(n/2^i) + i*n ;