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
  1. 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)

  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

  3. 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)

  4. 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

  5. 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)

  6. 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

  7. 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)

  8. 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)

  9. 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

  10. 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
  1. 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].
  1. 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).

  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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Explain the concept of in-place sorting in Selection Sort. Answer: In-place sorting refers to the property of sorting algorithms that

Selection sort is a simple sorting algorithm that works by selecting the minimum element from the unsorted part of an array and placing it at the beginning of the array. It repeats this process for each unsorted element until the entire array is sorted. The algorithm's time complexity is O(n^2) and it's an in-place sorting algorithm, meaning it doesn't require any additional memory space other than the array being sorted. The steps of selection sort are as follows:
  1. Find the smallest element in the unsorted part of the array.
  2. Swap the smallest element with the first element of the unsorted part of the array.
  3. Repeat steps 1 and 2 for the remaining unsorted elements until the entire array is sorted.
Although selection sort is not efficient for large lists, it has several advantages over other sorting algorithms. It's simple to understand, and it performs well for small lists or lists with large elements. It's also efficient for partially sorted lists or lists with a small number of elements that need to be sorted. One disadvantage of selection sort is that it performs the same number of comparisons, n(n-1)/2, regardless of whether the array is already sorted or not. This makes it inefficient for large arrays. Additionally, selection sort has a high number of swaps, which can be expensive in some scenarios. Overall, selection sort is a useful algorithm for sorting small arrays or partially sorted arrays, but it's not the best option for large arrays. Other algorithms like merge sort or quicksort are more efficient for large arrays.