Algorithms and programs Flashcards
(128 cards)
What is the purpose of sorting data?
To organize data in a specific order to improve readability or to make searching more efficient.
What does the bubble sort algorithm do?
It repeatedly compares and swaps adjacent items if they are in the wrong order until the list is sorted.
Why is it called ““bubble sort””?
Because higher values “bubble up” to the top of the list, similar to bubbles in a fizzy drink.
What is a ““pass”” in bubble sort?
A single complete iteration through the list.
What is a ““comparison”” in bubble sort?
Checking whether two adjacent items are in the correct order.
What types of data can bubble sort work on?
Numbers, characters, and strings.
What is the best-case scenario for bubble sort?
When the list is already sorted — requires only one pass and n-1 comparisons.
What is the worst-case scenario for bubble sort?
When the list is in reverse order — maximum number of comparisons and swaps.
What is the time complexity of bubble sort in the worst case?
O(n²)
What is the best-case time complexity of bubble sort when using the best bubble sort algorithm?
O(n)
What is the average-case time complexity of bubble sort?
O(n²)
What is the space complexity of bubble sort?
O(1), because it only uses a temporary variable for swapping.
Why is bubble sort considered inefficient for large datasets?
Because of its O(n²) time complexity in average and worst cases.
What does the insertion sort algorithm do?
It builds a sorted sublist one item at a time by inserting each item into its correct position.
What is the initial state of the sorted sublist in insertion sort?
It contains just the first item of the list.
What is a ‘pass’ in insertion sort?
A single operation of inserting one unsorted item into the correct position in the sorted sublist.
What is a ‘comparison’ in insertion sort?
Comparing an unsorted item with items in the sorted sublist to find its correct position.
Why is insertion sort usually faster than bubble sort?
Because it often performs fewer comparisons per pass.
What is the best-case scenario for insertion sort?
When the list is already sorted — only one comparison per pass is needed.
What is the worst-case scenario for insertion sort?
When the list is in reverse order — maximum comparisons and shifts per pass are needed.
What is the time complexity of insertion sort in the worst case?
O(n²)
What is the best-case time complexity of insertion sort?
O(n), when the list is already sorted.
What is the average-case time complexity of insertion sort?
O(n²)
What is the space complexity of insertion sort?
O(1), as sorting is done in-place using only one extra variable.