Sorting Algorithms Overview
- 30Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
Describe the basic sorting algorithm using nested loops.
It uses a for loop for i from 0 to n-1 and an inner loop for j from i+1 to n-1. If arr[j] < arr[i], swap the values using a temporary variable. This process is repeated until the array is sorted in ascending order.
Or swipe the card
30 cards
Describe the basic sorting algorithm using nested loops.
It uses a for loop for i from 0 to n-1 and an inner loop for j from i+1 to n-1. If arr[j] < arr[i], swap the values using a temporary variable. This process is repeated until the array is sorted in ascending order.
How can you modify the basic sorting algorithm to sort in descending order?
Change the condition from arr[j] < arr[i] to arr[j] > arr[i].
What is the time complexity of the basic sorting algorithm?
O(n2)
How does the basic sorting algorithm swap two elements?
It uses a temporary variable: temp = arr[i]; arr[i] = arr[j]; arr[j] = temp.
What is the purpose of the nested loops in the basic sorting algorithm?
The outer loop selects an element, and the inner loop compares all subsequent elements to it, swapping if they are smaller, so that after the outer loop finishes, the smallest element is placed at the current position.
What is selection sort?
Selection sort is a sorting algorithm that repeatedly finds the minimum element from the unsorted portion and swaps it with the first unsorted element.
Describe the steps of selection sort algorithm.
- Start with the first element as the minimum. 2. Scan the rest of the array to find the actual minimum. 3. Swap the found minimum with the first element. 4. Move the boundary between sorted and unsorted by one element. 5. Repeat until the array is sorted.
What three functions are typically used in the selection sort implementation?
A swap function, a location_of_smallest function, and the selection_sort function.
How does the location_of_smallest function work in selection sort?
It takes the array, start index, and end index, then iterates from start to end, keeping track of the index of the smallest element, and returns that index.
What is the time complexity of selection sort?
O(n2)
Why is selection sort inefficient for large arrays?
Because it has a time complexity of O(n2), meaning the number of comparisons grows quadratically with input size.
What is bubble sort?
Bubble sort is a sorting algorithm that repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. The process is repeated until no swaps are needed.
Describe the bubble process function in bubble sort.
It starts from the last element (index n-1) and goes down to index 1, comparing each pair arr[i] and arr[i-1]. If arr[i] < arr[i-1], they are swapped. This 'bubbles' the smaller element upward.
How is bubble sort implemented in the video?
It uses a bubble_sort function that loops i from 0 to n-2, calling the bubble_process function each time. bubble_process takes the array and the size, and performs one pass of adjacent comparisons and swaps from bottom to top.
Why is it called bubble sort?
Because smaller elements 'bubble up' to the top of the array like bubbles in a liquid, while larger elements sink to the bottom.
What is the time complexity of bubble sort?
O(n2) in the worst and average case.
How does bubble sort compare adjacent elements?
It compares arr[i] and arr[i-1] for i from n-1 down to 1. If arr[i] < arr[i-1], they are swapped.
What is insertion sort?
Insertion sort is a sorting algorithm that builds a sorted array one element at a time by repeatedly taking the next element and inserting it into its correct position among the previously sorted elements.
Describe the steps of insertion sort.
- Start with the first element as sorted. 2. For each subsequent element (key), compare it with the elements before it. 3. Shift all elements greater than the key one position to the right. 4. Insert the key into the vacated position. 5. Repeat until the entire array is sorted.
How is the key variable used in insertion sort?
The key stores the current element being inserted. It is compared with elements in the sorted portion, and those greater than the key are shifted right. Finally, the key is placed in its correct position.
What is the time complexity of insertion sort?
O(n2) in the worst and average case, but O(n) in the best case (already sorted).
Why does insertion sort assume a portion is already sorted?
It starts with the first element as a sorted subarray of size 1, then inserts each new element into the correct position within that sorted subarray.
What is merge sort?
Merge sort is a divide-and-conquer sorting algorithm that recursively divides the array into halves, sorts each half, and then merges the sorted halves back together.
Describe the structure of merge sort implementation.
It typically has a wrapper function merge_sort(arr, size) that calls an auxiliary function merge_sort(arr, start, end). The auxiliary function handles recursion: if start < end, find mid, recursively sort left and right halves, then call combine(arr, start, mid, end) to merge them.
What is the base case in the recursive merge sort?
When start >= end, meaning the subarray has one or zero elements, return.
How is the midpoint calculated in merge sort?
mid = (start + end) / 2 using integer division.
Describe the combine function in merge sort.
The combine function merges two sorted subarrays (arr[start..mid] and arr[mid+1..end]) into a single sorted array. It creates a temporary buffer, copies the entire segment into it, then uses two indices i (left start) and j (mid+1) to compare buffer elements and place the smaller into arr[k] where k starts from start. After one subarray is exhausted, the remaining elements are copied.
Why does merge sort use a temporary buffer in the combine step?
To avoid overwriting elements during the merge. The entire segment is copied to a buffer, then merged back into the original array in sorted order.
What is the time complexity of merge sort?
O(nlogn) in all cases (worst, average, best).
How does merge sort compare to other sorting algorithms like selection, bubble, and insertion sort?
Merge sort is much faster for large arrays with time complexity O(nlogn) compared to O(n2) for the others.

Ask anything
We've read this whole study set, so questions about it get the best answers.