Anda di halaman 1dari 4

Jerusalem College of Engineering

Department of Electronic and Communication Engineering


EC6301 OOPS & DS
UNIT V - SORTING AND SEARCHING
PART-A
1. What is meant by sorting?
Arranges the numerical or alphabetical data in a list, in a specific order(Ascending or
Descending)
2. What are the two main classifications of sorting based on the source of data?
i) Internal sorting
Internal sorting is a technique in which data resides in the main memory of the computer.
ii) External sorting
External sorting is a technique in which data resides in external or secondary storage devices such
as hard disk, floppy disk etc.
3. What are the various factors that could be considered for choosing a sorting technique?
i) Size of the data structure
ii) Algorithm efficiency
iii) Programmers knowledge of the technique.
4. What is insertion sort?
Insertion sort method sorts a list of elements by inserting each successive element in the
previously sorted sub list. [Elements are inserted at their appropriate place]
It requires other elements to be shifted as required.
4. List out the Advantages and disadvantages of insertion sort.
Advantages:
i) Simple to implement
ii) It performs well in the case of smaller lists
iii) More efficient than other simple sorting algorithms such as bubble, selection sort.
iv) It is called in-place sorting algorithm (only requires a constant amount O (1) of extra memory space).
Disadvantages:
i) The efficiency of O(n2) is not suited for large sized lists.
ii) It requires large number of elements to be shifted.
5. What is divide and conquer technique?
Divide and Conquer algorithm is based on dividing the problem to be solved
into several, smaller sub instances, solving them independently and then combining
the sub instances solutions so as to yield a solution for the original instance.
6. What is meant by Quick sort?
Quick sort is one of the fastest sorting method that is based on divide and conquer strategy.
Steps:
Divide:
Split the array into two sub arrays based on pivot element. All the elements that are less than
pivot should be left sub array and all the elements that are more than pivot should be right sub array.

Conquer:
Recursively sort the two sub arrays.
Combine:
Combine all the sorted elements in a group to form a list of sorted elements.
7. What is a pivot element?
Pivot element is a key value that is selected and shuffled continuously as per the quick sort
algorithm until it attains its final position in the list.
8. What are the advantages and disadvantages of quick sort?
Advantages:
i) It is one of the fastest sorting algorithms.
ii) Quick sort reduces unnecessary swaps and moves an item.
ii) Its implementation does not require any additional memory.
Disadvantages:
i) The worst case efficiency of O(n2) is not well suited for large sized lists.
ii) Little more complex than other sorting algorithms.
9. What are the techniques used to choose the pivot element for quick sort and how many pointers
are used for quick sort?
The various techniques are First element, Random pick Median of three portioning
Quick sort requires two scans one from left to right and another from right to left, so two pointers
needed for the scans.
10. What is meant by merge sort?
Merge sort is based on divide-and-conquer approach.
It divides a list into several sub lists of equal sizes and sorts them individually.
It merges the various sub lists in pairs to eventually form the original list.
11. How many pointers are used in merge sort?
Three pointers are used in merge sort. i - ptr for the first sorted sub array, j - ptr for the second
sorted sub array and k - ptr for the merged sorted sub array.
12. What are the advantages and disadvantages of merge sort?
Advantages:
i) It is a fast and stable sorting method
ii) It always ensures an efficiency of O(n log(n))
Disadvantages:
i) It requires additional memory space to perform sorting.
ii) The number of comparisons made by merge sort is nearly optimal; its performance is slightly lesser
than quick sort.
13. Discuss the efficiency of different sorting algorithms.

Sorting Algorithms
Insertion sort
Bubble sort
Selection sort
Quick sort

Best case
O(n)
O(n)
O(n2)
O(n log(n))

Average Case
O(n2)
O(n2)
O(n2)
O(n log(n))

Worst Case
O(n2)
O(n2)
O(n2)
O(n2)

Merge sort

O(n log(n))

O(n log(n))

O(n log(n))

14. Define Algorithm.


An algorithm is clearly specified set of simple instructions to be followed to solve a problem. The
algorithm forms a base for program.
15. What is complexity analysis?
Complexity is the analysis of the amount of memory and time an algorithm requires to
completion.
There are two types of Complexity
Time Complexity:
Time complexity is the amount of computer time an algorithm requires to run
to completion.
Space Complexity:
`
Space complexity of an algorithm is the amount of memory space it needs to run to completion.
16. What does asymptotic notation mean?
Asymptotic notations are terminology that is introduced to enable us to make meaningful
statements about the time and space complexity of an algorithm.
The different notations are

Big Oh notation
Omega notation
Theta notation.

17. Define best case of an algorithm.


Best case is the shortest time that an algorithm will use over all instances of size n for a given
problem to produce the result.
18. Define searching.
Searching refers to determining whether an element is present in a given list of elements or not. If
the element is found to be present in the list then the search is considered as successful, otherwise it is
considered as an unsuccessful search.
Types:
1. Linear Search
2. Binary Search
19. What is Linear Search?
Sequentially searches for an element in the list.
It typically starts with the first element in the list and moves forwards the end in a step-by-step
fashion.
In each iteration, it compares the element to be searched with the list element, and if there is a
match, the location of the list element is returned.

1.
2.
3.
4.

20. Explain linear search algorithm.


Algorithm:
Input the total number of elements (n).
Allocate memory for n elements.
Input the elements one by one.
Enter the element to be searched (key element) .

5.
6.
7.

For each element in the array, check whether the element is same as the element to be searched
If the element is equal to e, then display its position.
Else, the element is not found.

21. What is meant by Binary search?


Binary search requires the elements to be already arranged in a sorted manner. It begins by
comparing the key element with middle element in the list. If there is a match then the search ends
immediately and location of the middle element is returned. However if there is a mismatch then it
focuses the search either in the left or the right sub list depending on whether the key element is lesser
than or greater than middle element.

22. What the time complexity of various searching algorithms?


Searching Algorithm
Linear Search
Binary Search

Time complexity
O(n)
O(log(n))

Part B
1.
2.
3.
4.
5.
6.
7.

Explain in detail about insertion sorting technique with appropriate example.


Narrate the concept of quick sort algorithm with suitable example.
Write C++ code to implement merge sort with suitable example.
Write C++ code to implement linear search with suitable example.
Write a C++ program that sorts the given set of integers and perform binary search on them.
Write C++ code to implement binary search with suitable example.
Explain how divide and conquer is applied to merge sort. Trace the algorithm for the
following set of data. 25, 0, 8, 78, 6, 34, 56, 90, 94, 27, 100
8. Sort the following values using Quick sort & Insertion Sort: 65,70,75,80,60,55,85,50,45

Anda mungkin juga menyukai