Sorting Algorithm
Sorting Algorithm
SEARCHING AND
SORTING
Algorithms
Bubble Sort:
Bubble Sort is a comparison based Sorting Algorithm.
It works by checking its adjacent element whether, it is in sorted order or not.
It is an In-place Sorting algorithm as we don’t need any extra data structure while sorting.
It is stable (the sequence of repeating elements does not change).
Ans.) 8 swaps.
Best Case = T(n/2) + T(n/2) + n, when after partition, there will be equal elements
on both sides.
Worst Case + T(n-1) + n, when after partition, the elements are skewed on one
side only.
Merge Sort:
It is another comparison based sorting algorithm.
It is based on the Divide and conquer technique.
It is Outplace algorithm as we need an additional array to store the temp
output of the sorted array.
It is Stable Sort.
Time Complexity of Merge algorithm is : O(N) for outplace,
O(n/2*n/2) = O(n2) for in-place.
Recurrence Relation
:1.) for Outplace:
Heap Sort:
NOTE: Array is better for complete and almost complete Binary Tree.
Linked List is better idea for any gap in Nodes.
Max-Heap: Root is maximum or equal to children at every level.
for ith max, Total comparison = i(i-1)/2 = O(i2).
In Bubble Sort, every pass is costly O(n), but in heap sort, every comparison cost is O(1).
NOTE: To delete an element , we can simply put the deleted element at last place, and reduce the loop to one place (from 1-8).
As soon as we deleted the element, we will get the elements in ascending order (because we are placing max element
at last place.)
Heap Sort:
For Insertion in a max heap or min heap, O(logn) times requires for an single element.
for n elements = O(nlogn).
for Deletion in a max or min heap, O(logn) times require for an single element.
for deletion of n elements = O(nlogn)
For heap sort, creation and deletion requires for n elements = O(2nlogn).
NOTE: This method takes O(nlogn) to create a max heap, but there is another method, which is heapify
method and it takes O(n) to create a Build Heap.
Imp Questions:
1. Write a function in C/C++ to implement binary search method. Show the step wise
execution of the binary search algo to search an element 48 in following list.
1, 5, 8, 13, 19, 20, 23, 44, 48, 58, 61.
2. Write an algo for Bubble Sort and calculate its time complexity.
3. Write a pseudo-C code for Merge Sort. What is its time complexity.
4. Write a pseudo-C code for Binary search and state advantages of binary search over linear
search.
5. Explain Quick sort algorithm sorts a sequence S using Divide and Conquer approach.
6. Which searching technique is best in data structure? Why?
7. List out the types of sorting available in Data Structures.
8. What is the purpose of quick sort and advantage?
9. Write a program to implement bubble sort concept.
10. Explain Selection Algo with a proper example.
11. Write a pseudo-C code for Quick sort algorithm.
12. Write a C/C++ non-recursive and recursive function for binary search.
13. Show the stepwise execution of the Bubble sort algorithm for the following list. Give the
time complexity of algorithm.
23, 8, 44, 1, 61, 13, 58, 20, 48, 19
14. What is Max Heap and Min Heap. What is Heapify process.
15. Explain Heap Sort with a proper example. And explain the time complexity of Heap Sort.