
In computer science, algorithm efficiency is a key factor in determining how well an algorithm performs, particularly with large datasets. The efficiency of an algorithm can be...
In computer science, algorithm efficiency is a key factor in determining how well an algorithm performs, particularly with large datasets. The efficiency of an algorithm can be measured in terms of its time complexity and space complexity. Time complexity tells us how the runtime of an algorithm increases as the input size grows, while space complexity refers to the amount of memory an algorithm requires.
When it comes to sorting algorithms, Selection Sort and Insertion Sort are among the simplest sorting algorithms. Both have their advantages and disadvantages, particularly when considering efficiency.
The efficiency of an algorithm is typically evaluated using Big O notation, which classifies algorithms based on their worst-case, best-case, and average-case performance in terms of input size (n).
Time Complexity: Refers to how the time taken by an algorithm increases as the size of the input grows. It's usually expressed as a function of n (where n is the size of the input data).
Space Complexity: Refers to the amount of memory used by an algorithm as a function of the input size.
Some common time complexities for sorting algorithms are:
For sorting algorithms like Selection Sort and Insertion Sort, we primarily focus on time complexity.
Selection Sort is an in-place comparison-based sorting algorithm. It works by repeatedly selecting the smallest (or largest, depending on the sorting order) element from the unsorted portion of the array and swapping it with the first unsorted element.
for i = 0 to n-1
min_index = i
for j = i+1 to n
if array[j] < array[min_index]
min_index = j
swap array[i] with array[min_index]
Insertion Sort is another in-place, comparison-based sorting algorithm. It works by building a sorted portion of the array one element at a time. It repeatedly takes the next unsorted element and inserts it into the correct position in the sorted portion of the array.
for i = 1 to n-1
key = array[i]
j = i - 1
while j >= 0 and array[j] > key
array[j + 1] = array[j]
j = j - 1
array[j + 1] = key
Best Case:
Average and Worst Case:
Advantages:
Disadvantages:
| Criterion | Selection Sort | Insertion Sort |
|---|---|---|
| Time Complexity (Best Case) | O(n²) | O(n) |
| Time Complexity (Worst/Average Case) | O(n²) | O(n²) |
| Space Complexity | O(1) | O(1) |
| Stability | Unstable (does not preserve relative order of equal elements) | Stable (preserves relative order of equal elements) |
| Performance on Small/Almost Sorted Arrays | Performs equally for all cases | Performs well on small or partially sorted arrays |
| Usage | Used in situations where memory is highly constrained and performance is not critical | Useful for small arrays or nearly sorted data |
For larger datasets, more efficient algorithms like Merge Sort, Quick Sort, or Heap Sort should be considered, as they offer better performance with time complexities of O(n log n).
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.