44 Lecture
CS301
Midterm & Final Term Short Notes
Selection Sort
Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element and swapping it with the current element. The algorithm divides the input array into two parts: the sorted subarray and the unsorted subar
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the time complexity of selection sort? a) O(n) b) O(n log n) c) O(n^2) d) O(2^n) Answer: c) O(n^2)
Which of the following is true about selection sort? a) It is an in-place sorting algorithm b) It is a stable sorting algorithm c) It is a divide-and-conquer sorting algorithm d) It is a comparison-based sorting algorithm Answer: d) It is a comparison-based sorting algorithm
Which of the following is the best case time complexity of selection sort? a) O(n) b) O(n log n) c) O(n^2) d) O(2^n) Answer: c) O(n^2)
Which of the following data structures is commonly used to implement selection sort? a) Array b) Linked List c) Stack d) Queue Answer: a) Array
Which of the following is the space complexity of selection sort? a) O(n) b) O(log n) c) O(1) d) O(n log n) Answer: c) O(1)
Which of the following is the first step in selection sort? a) Compare the first two elements b) Find the smallest element in the array c) Compare the last two elements d) Swap the first two elements Answer: b) Find the smallest element in the array
Which of the following is the worst case time complexity of selection sort? a) O(n) b) O(n log n) c) O(n^2) d) O(2^n) Answer: c) O(n^2)
Which of the following is the average case time complexity of selection sort? a) O(n) b) O(n log n) c) O(n^2) d) O(2^n) Answer: c) O(n^2)
Which of the following is the last step in selection sort? a) Swap the last two elements b) Swap the first two elements c) Find the smallest element in the array d) Compare the last two elements Answer: a) Swap the last two elements
Which of the following is a disadvantage of selection sort? a) It is a very slow algorithm b) It is not stable c) It requires additional memory space d) It cannot handle large datasets Answer: a) It is a very slow algorithm
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is Selection Sort? Explain with an example. Answer: Selection Sort is an algorithm that sorts an array by repeatedly finding the minimum element from the unsorted part of the array and putting it at the beginning. This process is continued until the whole array is sorted. For example, consider the following array: [64, 25, 12, 22, 11]. The steps to sort this array using selection sort are:
- Find the minimum element in the unsorted array, which is 11.
- Swap the minimum element with the first element of the unsorted array, which results in [11, 25, 12, 22, 64].
- Repeat the above two steps for the remaining unsorted array, resulting in [11, 12, 22, 25, 64].
What is the time complexity of Selection Sort? Explain how you arrived at this answer. Answer: The time complexity of Selection Sort is O(n^2), where n is the number of elements in the array. This is because for each element in the array, we need to find the minimum element in the remaining unsorted part of the array, which takes O(n) time. Since we repeat this process n times, the overall time complexity becomes O(n^2).
Can Selection Sort be used to sort a linked list? If yes, explain how. If no, explain why not. Answer: Yes, Selection Sort can be used to sort a linked list. In a linked list, we can find the minimum element in the remaining unsorted part of the list by traversing the list and keeping track of the minimum element. Once we find the minimum element, we can remove it from its current position and insert it at the beginning of the sorted part of the list. We repeat this process until the whole list is sorted.
What is the best-case time complexity of Selection Sort? Explain when this scenario occurs. Answer: The best-case time complexity of Selection Sort is O(n^2). This scenario occurs when the array is already sorted or nearly sorted, as the algorithm still needs to check each element in the unsorted part of the array to ensure that it is in the correct position.
Compare and contrast Selection Sort and Bubble Sort. Answer: Selection Sort and Bubble Sort are both simple sorting algorithms with a time complexity of O(n^2). However, Selection Sort is more efficient than Bubble Sort as it makes fewer comparisons. In Selection Sort, we find the minimum element in the remaining unsorted part of the array and swap it with the first element of the unsorted part. In Bubble Sort, we repeatedly compare adjacent elements and swap them if they are in the wrong order. This means that Bubble Sort makes more comparisons than Selection Sort, making it less efficient.
How does the number of elements in the array affect the performance of Selection Sort? Answer: The time complexity of Selection Sort is O(n^2), where n is the number of elements in the array. This means that as the number of elements in the array increases, the time taken to sort the array increases quadratically. Therefore, Selection Sort is not efficient for large arrays.
Can Selection Sort be used to sort an array in descending order? If yes, explain how. If no, explain why not. Answer: Yes, Selection Sort can be used to sort an array in descending order. Instead of finding the minimum element in the unsorted part of the array, we find the maximum element and swap it with the first element of the unsorted part. We repeat this process until the whole array is sorted in descending order.
Explain the concept of in-place sorting in Selection Sort. Answer: In-place sorting refers to the property of sorting algorithms that
- Find the smallest element in the unsorted part of the array.
- Swap the smallest element with the first element of the unsorted part of the array.
- Repeat steps 1 and 2 for the remaining unsorted elements until the entire array is sorted.