Sorting Algorithms Overview
- 25Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
In the basic sorting algorithm described, what is the condition used to determine when to swap two elements?
In the basic sorting algorithm described, what is the condition used to determine when to swap two elements?
25 questions
- 1
In the basic sorting algorithm described, what is the condition used to determine when to swap two elements?
- A
If arr[j] is not equal to arr[i]
- B
If arr[j] is equal to arr[i]
- C
If arr[j] is greater than arr[i]
- D
If arr[j] is less than arr[i]
Correct answer
DIf arr[j] is less than arr[i]
Explanation
The algorithm checks if arr[j] < arr[i], and if true, swaps them to move smaller elements to the left.
- A
- 2
In the basic sorting algorithm, what is the initial relationship between the indices i and j?
- A
j = i
- B
j = i - 1
- C
j = 0
- D
j = i + 1
Correct answer
Dj = i + 1
Explanation
j starts one position ahead of i (i+1) and iterates through the rest of the array.
- A
- 3
To sort in descending order using the basic algorithm, what change is made?
- A
Change the swap to use a different temporary variable
- B
Change arr[j] < arr[i] to arr[j] > arr[i]
- C
Change the array declaration to descending type
- D
Change the outer loop to iterate backwards
Correct answer
BChange arr[j] < arr[i] to arr[j] > arr[i]
Explanation
The condition is reversed to arr[j] > arr[i], so larger elements are swapped to the left.
- A
- 4
In selection sort, what is the purpose of the
location_of_smallestfunction?- A
To swap two elements in the array
- B
To display the array contents
- C
To calculate the size of the array
- D
To find the index of the smallest element in a given range
Correct answer
DTo find the index of the smallest element in a given range
Explanation
The function scans from a start index to an end index and returns the index of the smallest element found.
- A
- 5
How many times is the swap function called in a complete selection sort on an array of n elements?
- A
n times
- B
n-1 times
- C
n/2 times
- D
Depends on the initial order
Correct answer
Bn-1 times
Explanation
In each iteration, the smallest element is placed in its correct position, requiring n-1 swaps.
- A
- 6
What are the parameters passed to the selection sort function?
- A
The array and its size
- B
The array and a comparison function
- C
The array and its start and end indices
- D
The array and the number of elements to sort
Correct answer
AThe array and its size
Explanation
The function takes the array and the size (n) as parameters.
- A
- 7
In bubble sort, how does the 'bubble process' traverse the array?
- A
From the last element to the first element
- B
From the middle to both ends
- C
From the first element to the last element
- D
Randomly
Correct answer
AFrom the last element to the first element
Explanation
The bubble process starts from the last element (n-1) and moves upward to the first element (0).
- A
- 8
What condition triggers a swap in the bubble sort implementation?
- A
If arr[i] is equal to arr[i-1]
- B
If arr[i] is less than arr[i-1]
- C
If arr[i] is not equal to arr[i-1]
- D
If arr[i] is greater than arr[i-1]
Correct answer
BIf arr[i] is less than arr[i-1]
Explanation
The code checks if arr[i] < arr[i-1] and swaps to move the smaller element upward.
- A
- 9
How many times is the bubble process called for an array of n elements?
- A
n/2 times
- B
log n times
- C
n times
- D
n-1 times
Correct answer
Dn-1 times
Explanation
The outer loop runs n-1 times to ensure the array is fully sorted.
- A
- 10
What assumption does insertion sort make about the array initially?
- A
The first element is already sorted
- B
The array is already sorted in reverse order
- C
The array contains only unique elements
- D
The entire array is unsorted
Correct answer
AThe first element is already sorted
Explanation
Insertion sort assumes that the first element (index 0) is a sorted list of size 1, and then inserts subsequent elements.
- A
- 11
What is the purpose of the
keyvariable in insertion sort?- A
To store the current element being inserted
- B
To store the smallest element in the array
- C
To indicate the end of the sorted portion
- D
To count the number of iterations
Correct answer
ATo store the current element being inserted
Explanation
The key holds the value of the element to be placed into the correct position in the sorted part.
- A
- 12
How does insertion sort shift elements to make room for the key?
- A
By creating a new array and copying elements
- B
By swapping the key with each larger element
- C
By swapping adjacent elements
- D
By moving larger elements one position to the right
Correct answer
DBy moving larger elements one position to the right
Explanation
Elements greater than the key are shifted right by one position to create space for the key.
- A
- 13
What is the time complexity of merge sort?
- A
O(n)
- B
O(log n)
- C
O(n^2)
- D
O(n log n)
Correct answer
DO(n log n)
Explanation
Merge sort has a time complexity of O(n log n) in the average and worst cases.
- A
- 14
In the merge sort implementation, what is the base case for recursion?
- A
When the midpoint equals start
- B
When the array is already sorted
- C
When start is greater than or equal to end
- D
When the array size is 0
Correct answer
CWhen start is greater than or equal to end
Explanation
The recursion stops when start >= end, meaning the subarray has zero or one element.
- A
- 15
Why is a temporary buffer used in the merge step of merge sort?
- A
To hold the array of smaller sizes
- B
To increase the speed of copying
- C
To avoid overwriting elements during the merge
- D
To store the final sorted array
Correct answer
CTo avoid overwriting elements during the merge
Explanation
The buffer holds a copy of the subarray so that elements can be merged without losing data.
- A
- 16
In merge sort, how is the midpoint of the array calculated?
- A
(start + end) / 2
- B
end - (end - start) / 2
- C
start + (end - start) / 2
- D
(start + end) * 2
Correct answer
A(start + end) / 2
Explanation
The midpoint is calculated as (start + end) / 2 (integer division) as shown in the code.
- A
- 17
After merging in merge sort, what is done with the temporary buffer?
- A
It is deleted to free memory
- B
It is reused for the next merge
- C
It is returned from the function
- D
It is kept for the entire program
Correct answer
AIt is deleted to free memory
Explanation
The buffer is deallocated using delete[] after the merge to prevent memory leaks.
- A
- 18
Which header file must be included to use the
rand()function in C++?- A
- B
- C
- D
Correct answer
CExplanation
The rand() function is declared in the header.
- A
- 19
How is the size of an array computed using
sizeof?- A
sizeof(array) / sizeof(array[0])
- B
sizeof(array[0]) / sizeof(array)
- C
sizeof(array) - sizeof(int)
- D
sizeof(array) * sizeof(int)
Correct answer
Asizeof(array) / sizeof(array[0])
Explanation
The total size of the array divided by the size of one element gives the number of elements.
- A
- 20
What is a wrapper function in the context of merge sort?
- A
A function that wraps the array in a struct
- B
A function that sorts the array in place
- C
A function that handles error checking
- D
A function that calls another function with additional parameters
Correct answer
DA function that calls another function with additional parameters
Explanation
The merge_sort function with only array and size calls an auxiliary function with start and end indices.
- A
- 21
In the swap function, how are the values exchanged?
- A
Using bitwise XOR
- B
Using a temporary variable to hold one value
- C
Using arithmetic operations without a temporary
- D
Using a function pointer
Correct answer
BUsing a temporary variable to hold one value
Explanation
A temporary variable stores the value of one element, then the other element is assigned, and finally the temporary is assigned to the second element.
- A
- 22
In the bubble sort implementation, what caused an infinite loop when first run?
- A
Missing base case in recursion
- B
Incorrect swap condition
- C
Missing increment of the outer loop variable
- D
Array index out of bounds
Correct answer
CMissing increment of the outer loop variable
Explanation
The outer loop variable i was not incremented, causing the loop to run forever.
- A
- 23
In the selection sort implementation, what caused an infinite loop when first run?
- A
Array index out of bounds
- B
Missing increment of the inner loop variable
- C
Incorrect swap condition
- D
Missing base case in recursion
Correct answer
BMissing increment of the inner loop variable
Explanation
The inner loop variable i was not incremented? Actually in selection sort, they forgot to increment i in the selection sort function, causing an infinite loop.
- A
- 24
What is the purpose of the display function in the code?
- A
To swap two elements in the array
- B
To print the array elements to the console
- C
To calculate the size of the array
- D
To read array input from the user
Correct answer
BTo print the array elements to the console
Explanation
The display function iterates through the array and prints each element.
- A
- 25
Which of the following sorting algorithms has a time complexity of O(n log n)?
- A
Bubble sort
- B
Insertion sort
- C
Merge sort
- D
Selection sort
Correct answer
CMerge sort
Explanation
Merge sort has O(n log n), while the others are O(n^2).
- A

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