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

  • 39Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Basic Sorting Algorithm

What is the condition used in the basic sorting algorithm to determine when to swap elements for ascending order?

39 questions

  1. 1

    What is the condition used in the basic sorting algorithm to determine when to swap elements for ascending order?

    Model answer

    If array[j] is less than array[i], the values are swapped.

    Explanation

    The algorithm checks if the element at index j is smaller than the element at index i. When true, it swaps them to move the smaller element forward.

  2. 2

    Explain how swapping is performed in the basic sorting algorithm.

    Model answer

    A temporary variable temp stores the value of array[i]. Then array[i] is assigned the value of array[j], and array[j] is assigned the value of temp.

    Explanation

    This three-step swap exchanges the two values without losing data, using a temporary variable.

  3. 3

    How would you modify the basic sorting algorithm to sort in descending order instead of ascending?

    Model answer

    Change the condition from array[j] < array[i] to array[j] > array[i].

    Explanation

    The condition determines when to swap. For descending order, we swap when the element at j is larger than the element at i.

  4. 4

    In the basic sorting algorithm, what are the roles of the outer loop index i and the inner loop index j?

    Model answer

    i represents the current position being filled, and j scans all elements after i looking for a smaller element to swap.

    Explanation

    The outer loop iterates over each element from first to second-last. The inner loop starts at i+1 and goes to the end, comparing and swapping as needed.

  5. 5

    Why does the inner loop of the basic sorting algorithm start at j = i + 1?

    Model answer

    Because the algorithm compares the element at i with all subsequent elements, and there is no need to compare with elements before i.

    Explanation

    The outer loop has already handled earlier positions, so comparing with elements before i would be redundant.

  6. 6

    What is the time complexity of the basic sorting algorithm?

    Model answer

    O(n^2) because it uses nested loops, each iterating up to n times.

    Explanation

    The basic algorithm has two nested loops, so the number of comparisons is roughly n*(n-1)/2, which is O(n^2).

  7. 7

    What is the purpose of the locationOfSmallest function in selection sort?

    Model answer

    It finds the index of the smallest element in the unsorted portion of the array (from start to end).

    Explanation

    The function scans the subarray from index i to n-1 and returns the index of the smallest element found.

  8. 8

    Describe the steps of selection sort for the array [5,4,3,7,9].

    Model answer

    Start at index 0. Find smallest (3 at index 2), swap with 5 → [3,4,5,7,9]. Move to index 1: smallest is 4, no swap. Index 2: smallest is 5, no swap. Array is sorted.

    Explanation

    Selection sort repeatedly selects the smallest element from the unsorted part and moves it to the front.

  9. 9

    How does selection sort differ from the basic sorting algorithm described first?

    Model answer

    Selection sort finds the smallest element first and then swaps it into place in one swap per outer iteration, while the basic algorithm swaps every time a smaller element is encountered.

    Explanation

    The basic algorithm may swap multiple times per outer iteration, whereas selection sort only swaps after finding the smallest through locationOfSmallest.

  10. 10

    In selection sort, why is the outer loop condition i < n-1?

    Model answer

    Because once the first n-1 elements are in place, the last element is automatically in the correct position.

    Explanation

    After placing n-1 smallest elements, the largest element remains at the end, so no further passes are needed.

  11. 11

    What parameters does the swap function take in the selection sort implementation?

    Model answer

    It takes an array a and two integer indices x and y (e.g., swap(a, i, j)).

    Explanation

    The swap function in the code is defined as void swap(int a[], int x, int y) and swaps a[x] and a[y].

  12. 12

    How is the size of the array calculated in the selection sort code example?

    Model answer

    Using sizeof(arr)/sizeof(int).

    Explanation

    The total size of the array in bytes divided by the size of one int equals the number of elements.

  13. 13

    What does the display function do in the selection sort code?

    Model answer

    It prints all elements of the array separated by spaces.

    Explanation

    The display function iterates through the array and outputs each element with a space, then a newline.

  14. 14

    How does the bubble process in bubble sort work?

    Model answer

    It starts from the last element and compares each pair of adjacent elements, swapping if the lower element is smaller than the higher one, moving the smaller element upward like a bubble.

    Explanation

    The bubble process scans from n-1 down to 1, swapping whenever array[i] < array[i-1] to push lighter values to the top.

  15. 15

    In bubble sort, why does the algorithm start comparing from the bottom (last element) rather than the top?

    Model answer

    Because the goal is to let lighter elements rise to the top; starting from the bottom ensures smaller elements bubble up one step per pass.

    Explanation

    The analogy of bubbles rising in liquid dictates that the smaller (lighter) values ascend, so we begin at the deepest element.

  16. 16

    What condition triggers a swap in bubble sort?

    Model answer

    If array[i] (current element) is less than array[i-1] (element above), they are swapped.

    Explanation

    The comparison is between adjacent elements; if the lower element is smaller, they exchange so the smaller one moves up.

  17. 17

    How many passes (outer loop iterations) are needed to guarantee a sorted array in the worst case for bubble sort?

    Model answer

    n-1 passes.

    Explanation

    Each pass ensures the largest remaining element sinks to its correct place; after n-1 passes, all elements are sorted.

  18. 18

    In the bubble sort code, why did the first run result in an infinite loop?

    Model answer

    Because the outer loop variable i was not incremented (missing i++).

    Explanation

    Without incrementing i, the loop condition i < n-1 always remained true, causing an infinite loop.

  19. 19

    Explain how the array [3,5,1,4,3] is sorted using bubble sort, showing the steps after each pass.

    Model answer

    Pass 1: compare (4,3) swap → [3,5,1,3,4]; then (1,3) no swap? Actually careful: Start from bottom: indices 4 and 3: 3<4? no. i=3: compare 4 and 1? Wait, algorithm compares i with i-1 from bottom. Let's list properly: Array [3,5,1,4,3]. i=4: compare 3 and 4? no swap. i=3: compare 4 and 1? 4<1? false. i=2: compare 1 and 5? 1<5 → swap → [3,1,5,4,3]; i=1: compare 1 and 3? 1<3 → swap → [1,3,5,4,3]. End pass1: [1,3,5,4,3]. Pass2: bottom: i=4: 3<4? swap → [1,3,5,3,4]; i=3: compare 3 and 5? swap → [1,3,3,5,4]; i=2: compare 3 and 3? no; i=1: compare 3 and 1? no. Pass3: i=4: 4<5? yes swap → [1,3,3,4,5]; i=3: compare 4 and 3? no; i=2: compare 3 and 3? no; i=1: compare 3 and 1? no. Sorted.

    Explanation

    Bubble sort repeatedly passes from bottom to top, swapping adjacent elements that are out of order. Three passes sift the smallest elements to the front.

  20. 20

    What assumption does insertion sort make about the array?

    Model answer

    It assumes that a portion of the array (the left part) is already sorted.

    Explanation

    Insertion sort builds the sorted array one element at a time by inserting each new element into its correct place within the already sorted prefix.

  21. 21

    Why does insertion sort start from index 1 rather than 0?

    Model answer

    Because the first element (index 0) is considered trivially sorted, so we begin inserting from the second element.

    Explanation

    A single-element subarray is always sorted, so we start with that assumption and insert subsequent elements.

  22. 22

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

    Model answer

    It stores the value of the current element being inserted into the sorted portion.

    Explanation

    The key is taken from the element at index i, and we compare it with elements in the sorted portion to find its correct position.

  23. 23

    How does insertion sort shift elements?

    Model answer

    It repeatedly moves elements from the sorted portion one position to the right if they are greater than the key, creating space for the key.

    Explanation

    The inner while loop shifts elements: a[j+1] = a[j] while j >= 0 and a[j] > key, then a[j+1] = key.

  24. 24

    What are the conditions for the inner while loop in insertion sort?

    Model answer

    j >= 0 (to stay within array bounds) and a[j] > key (element from sorted portion is larger than the key).

    Explanation

    The loop continues as long as there are elements greater than the key to shift them right.

  25. 25

    Trace the insertion sort algorithm on the array [3,7,9,4].

    Model answer

    Start with sorted [3]. i=1: key=7, no shift → [3,7]. i=2: key=9, no shift → [3,7,9]. i=3: key=4, shift 9 and 7 right → [3,4,7,9].

    Explanation

    Each element is inserted into the already sorted prefix by shifting larger elements right until the correct spot is found.

  26. 26

    How does insertion sort's approach compare to bubble sort and selection sort?

    Model answer

    Insertion sort builds a sorted list by inserting elements one by one into the correct position, while bubble sort repeatedly swaps adjacent elements and selection sort selects the smallest to swap with the front.

    Explanation

    Insertion sort works online (like sorting a hand of cards), whereas bubble and selection sort are different strategies. Insertion sort can be more efficient for nearly sorted data.

  27. 27

    Explain the divide-and-conquer strategy used in merge sort.

    Model answer

    Merge sort divides the array into two halves, recursively sorts each half, and then merges the two sorted halves into one sorted array.

    Explanation

    The problem is split into smaller subproblems, solved recursively, and combined to get the final solution.

  28. 28

    What is the base case in the recursive merge sort helper function?

    Model answer

    If start index s is greater than or equal to end index e, the function returns (no further division needed).

    Explanation

    When the subarray has zero or one element, it is already sorted, so recursion stops.

  29. 29

    How is the midpoint calculated in merge sort?

    Model answer

    mid = (s + e) / 2, using integer division.

    Explanation

    The sum of start and end indices divided by two gives the split point. Integer division discards the remainder.

  30. 30

    Why does merge sort require a temporary buffer?

    Model answer

    To store a copy of the original array so that merging can be done without overwriting elements during comparison.

    Explanation

    The buffer holds the original data while the algorithm compares and writes the sorted result directly into the array.

  31. 31

    Describe the merging process in merge sort.

    Model answer

    The array is copied to a buffer. Three indices i (left half start), j (right half start), and k (output position) step through; the smaller of buffer[i] and buffer[j] is written to array[k], and the corresponding index advances. This continues until one half is exhausted, then the remaining elements from the other half are copied.

    Explanation

    Merging combines two sorted subarrays into one sorted array by comparing the front elements of each half.

  32. 32

    After merging, why is the buffer deallocated?

    Model answer

    To free the memory allocated from the heap, preventing memory leaks.

    Explanation

    The buffer is dynamically allocated and no longer needed after the merge is complete, so it is deleted.

  33. 33

    What is the time complexity of merge sort?

    Model answer

    O(n log n).

    Explanation

    Merge sort divides the array log n times and each level merges n elements, resulting in n log n time.

  34. 34

    What is the role of the wrapper function in merge sort?

    Model answer

    It calls the recursive helper function with the initial start index 0 and end index size-1.

    Explanation

    The wrapper provides a clean interface; the user passes only the array and its size, while the helper handles recursion with indices.

  35. 35

    What header files are needed for the sorting programs in C++?

    Model answer

    #include for input/output and #include for rand().

    Explanation

    iostream is required for std::cout and std::cin; cstdlib provides the rand() function used for random array generation.

  36. 36

    How can you generate random numbers in C++ for array initialization?

    Model answer

    Use the rand() function, which returns a random integer between 0 and RAND_MAX.

    Explanation

    The code uses array[i] = rand(); to fill the array with random values.

  37. 37

    How do you calculate the size of an array in C++ using sizeof?

    Model answer

    Total size of array divided by size of one element: sizeof(array) / sizeof(array[0]) or sizeof(array) / sizeof(int).

    Explanation

    sizeof(array) gives the total bytes of the array; dividing by the size of an element yields the number of elements.

  38. 38

    What are the fundamental programming constructs required to understand sorting algorithms?

    Model answer

    Sequence, selection (conditional statements), and iteration (loops).

    Explanation

    The video mentions that basic programming constructs like sequence, selection, and iteration are necessary to follow the sorting implementations.

  39. 39

    Why might merge sort be preferred over O(n^2) sorting algorithms for large datasets?

    Model answer

    Because its O(n log n) time complexity is significantly faster than O(n^2) as input size increases.

    Explanation

    O(n log n) grows much slower than O(n^2), making merge sort more efficient for large arrays.

1 of 39
Question 1 of 39

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent