1. Home
  2. Explore
  3. Computer Science
  4. Sorting Algorithms Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Sorting Algorithms Overview

  • 25Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Basic Sorting

In the basic sorting algorithm described, what is the condition used to determine when to swap two elements?

25 questions

  1. 1

    In the basic sorting algorithm described, what is the condition used to determine when to swap two elements?

    1. A

      If arr[j] is not equal to arr[i]

    2. B

      If arr[j] is equal to arr[i]

    3. C

      If arr[j] is greater than arr[i]

    4. D

      If arr[j] is less than arr[i]

    Correct answer
    D

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

  2. 2

    In the basic sorting algorithm, what is the initial relationship between the indices i and j?

    1. A

      j = i

    2. B

      j = i - 1

    3. C

      j = 0

    4. D

      j = i + 1

    Correct answer
    D

    j = i + 1

    Explanation

    j starts one position ahead of i (i+1) and iterates through the rest of the array.

  3. 3

    To sort in descending order using the basic algorithm, what change is made?

    1. A

      Change the swap to use a different temporary variable

    2. B

      Change arr[j] < arr[i] to arr[j] > arr[i]

    3. C

      Change the array declaration to descending type

    4. D

      Change the outer loop to iterate backwards

    Correct answer
    B

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

  4. 4

    In selection sort, what is the purpose of the location_of_smallest function?

    1. A

      To swap two elements in the array

    2. B

      To display the array contents

    3. C

      To calculate the size of the array

    4. D

      To find the index of the smallest element in a given range

    Correct answer
    D

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

  5. 5

    How many times is the swap function called in a complete selection sort on an array of n elements?

    1. A

      n times

    2. B

      n-1 times

    3. C

      n/2 times

    4. D

      Depends on the initial order

    Correct answer
    B

    n-1 times

    Explanation

    In each iteration, the smallest element is placed in its correct position, requiring n-1 swaps.

  6. 6

    What are the parameters passed to the selection sort function?

    1. A

      The array and its size

    2. B

      The array and a comparison function

    3. C

      The array and its start and end indices

    4. D

      The array and the number of elements to sort

    Correct answer
    A

    The array and its size

    Explanation

    The function takes the array and the size (n) as parameters.

  7. 7

    In bubble sort, how does the 'bubble process' traverse the array?

    1. A

      From the last element to the first element

    2. B

      From the middle to both ends

    3. C

      From the first element to the last element

    4. D

      Randomly

    Correct answer
    A

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

  8. 8

    What condition triggers a swap in the bubble sort implementation?

    1. A

      If arr[i] is equal to arr[i-1]

    2. B

      If arr[i] is less than arr[i-1]

    3. C

      If arr[i] is not equal to arr[i-1]

    4. D

      If arr[i] is greater than arr[i-1]

    Correct answer
    B

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

  9. 9

    How many times is the bubble process called for an array of n elements?

    1. A

      n/2 times

    2. B

      log n times

    3. C

      n times

    4. D

      n-1 times

    Correct answer
    D

    n-1 times

    Explanation

    The outer loop runs n-1 times to ensure the array is fully sorted.

  10. 10

    What assumption does insertion sort make about the array initially?

    1. A

      The first element is already sorted

    2. B

      The array is already sorted in reverse order

    3. C

      The array contains only unique elements

    4. D

      The entire array is unsorted

    Correct answer
    A

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

  11. 11

    What is the purpose of the key variable in insertion sort?

    1. A

      To store the current element being inserted

    2. B

      To store the smallest element in the array

    3. C

      To indicate the end of the sorted portion

    4. D

      To count the number of iterations

    Correct answer
    A

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

  12. 12

    How does insertion sort shift elements to make room for the key?

    1. A

      By creating a new array and copying elements

    2. B

      By swapping the key with each larger element

    3. C

      By swapping adjacent elements

    4. D

      By moving larger elements one position to the right

    Correct answer
    D

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

  13. 13

    What is the time complexity of merge sort?

    1. A

      O(n)

    2. B

      O(log n)

    3. C

      O(n^2)

    4. D

      O(n log n)

    Correct answer
    D

    O(n log n)

    Explanation

    Merge sort has a time complexity of O(n log n) in the average and worst cases.

  14. 14

    In the merge sort implementation, what is the base case for recursion?

    1. A

      When the midpoint equals start

    2. B

      When the array is already sorted

    3. C

      When start is greater than or equal to end

    4. D

      When the array size is 0

    Correct answer
    C

    When start is greater than or equal to end

    Explanation

    The recursion stops when start >= end, meaning the subarray has zero or one element.

  15. 15

    Why is a temporary buffer used in the merge step of merge sort?

    1. A

      To hold the array of smaller sizes

    2. B

      To increase the speed of copying

    3. C

      To avoid overwriting elements during the merge

    4. D

      To store the final sorted array

    Correct answer
    C

    To avoid overwriting elements during the merge

    Explanation

    The buffer holds a copy of the subarray so that elements can be merged without losing data.

  16. 16

    In merge sort, how is the midpoint of the array calculated?

    1. A

      (start + end) / 2

    2. B

      end - (end - start) / 2

    3. C

      start + (end - start) / 2

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

  17. 17

    After merging in merge sort, what is done with the temporary buffer?

    1. A

      It is deleted to free memory

    2. B

      It is reused for the next merge

    3. C

      It is returned from the function

    4. D

      It is kept for the entire program

    Correct answer
    A

    It is deleted to free memory

    Explanation

    The buffer is deallocated using delete[] after the merge to prevent memory leaks.

  18. 18

    Which header file must be included to use the rand() function in C++?

    1. A
    2. B
    3. C
    4. D
    Correct answer
    C

    Explanation

    The rand() function is declared in the header.

  19. 19

    How is the size of an array computed using sizeof?

    1. A

      sizeof(array) / sizeof(array[0])

    2. B

      sizeof(array[0]) / sizeof(array)

    3. C

      sizeof(array) - sizeof(int)

    4. D

      sizeof(array) * sizeof(int)

    Correct answer
    A

    sizeof(array) / sizeof(array[0])

    Explanation

    The total size of the array divided by the size of one element gives the number of elements.

  20. 20

    What is a wrapper function in the context of merge sort?

    1. A

      A function that wraps the array in a struct

    2. B

      A function that sorts the array in place

    3. C

      A function that handles error checking

    4. D

      A function that calls another function with additional parameters

    Correct answer
    D

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

  21. 21

    In the swap function, how are the values exchanged?

    1. A

      Using bitwise XOR

    2. B

      Using a temporary variable to hold one value

    3. C

      Using arithmetic operations without a temporary

    4. D

      Using a function pointer

    Correct answer
    B

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

  22. 22

    In the bubble sort implementation, what caused an infinite loop when first run?

    1. A

      Missing base case in recursion

    2. B

      Incorrect swap condition

    3. C

      Missing increment of the outer loop variable

    4. D

      Array index out of bounds

    Correct answer
    C

    Missing increment of the outer loop variable

    Explanation

    The outer loop variable i was not incremented, causing the loop to run forever.

  23. 23

    In the selection sort implementation, what caused an infinite loop when first run?

    1. A

      Array index out of bounds

    2. B

      Missing increment of the inner loop variable

    3. C

      Incorrect swap condition

    4. D

      Missing base case in recursion

    Correct answer
    B

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

  24. 24

    What is the purpose of the display function in the code?

    1. A

      To swap two elements in the array

    2. B

      To print the array elements to the console

    3. C

      To calculate the size of the array

    4. D

      To read array input from the user

    Correct answer
    B

    To print the array elements to the console

    Explanation

    The display function iterates through the array and prints each element.

  25. 25

    Which of the following sorting algorithms has a time complexity of O(n log n)?

    1. A

      Bubble sort

    2. B

      Insertion sort

    3. C

      Merge sort

    4. D

      Selection sort

    Correct answer
    C

    Merge sort

    Explanation

    Merge sort has O(n log n), while the others are O(n^2).

Question 1 of 25
Question 1 of 25

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent