All Exams Test series for 1 year @ ₹349 only
Question

Arrange the following items in ascending order using Bubble Sort. What is the intermediate sequence of 37, 54, 21, 85, 68, 12, 9, and 57 after the second pass?

The correct answer is

21, 37, 54, 12, 9, 57, 68, 85

Understanding Bubble Sort and Intermediate Sequences

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. In each pass, the largest unsorted element 'bubbles up' to its correct position.

We need to sort the given sequence in ascending order using Bubble Sort and find the state of the sequence after the second pass.

The initial sequence is: 37, 54, 21, 85, 68, 12, 9, 57

Performing Bubble Sort - First Pass

In the first pass, we compare adjacent elements starting from the beginning and swap them if the left element is greater than the right element. This process continues until the end of the list. The largest element will move to the last position after the first pass.

  • Compare 37 and 54: 37 is not > 54. Sequence remains: 37, 54, 21, 85, 68, 12, 9, 57
  • Compare 54 and 21: 54 > 21. Swap. Sequence becomes: 37, 21, 54, 85, 68, 12, 9, 57
  • Compare 54 and 85: 54 is not > 85. Sequence remains: 37, 21, 54, 85, 68, 12, 9, 57
  • Compare 85 and 68: 85 > 68. Swap. Sequence becomes: 37, 21, 54, 68, 85, 12, 9, 57
  • Compare 85 and 12: 85 > 12. Swap. Sequence becomes: 37, 21, 54, 68, 12, 85, 9, 57
  • Compare 85 and 9: 85 > 9. Swap. Sequence becomes: 37, 21, 54, 68, 12, 9, 85, 57
  • Compare 85 and 57: 85 > 57. Swap. Sequence becomes: 37, 21, 54, 68, 12, 9, 57, 85

After the first pass, the sequence is: 37, 21, 54, 68, 12, 9, 57, 85. The element 85 is now in its final sorted position.

Performing Bubble Sort - Second Pass

In the second pass, we repeat the process, but this time we only need to compare up to the second-to-last element, as the last element is already sorted. The second largest unsorted element will 'bubble up' to its correct position (the second to last).

Sequence after Pass 1: 37, 21, 54, 68, 12, 9, 57, 85

  • Compare 37 and 21: 37 > 21. Swap. Sequence becomes: 21, 37, 54, 68, 12, 9, 57, 85
  • Compare 37 and 54: 37 is not > 54. Sequence remains: 21, 37, 54, 68, 12, 9, 57, 85
  • Compare 54 and 68: 54 is not > 68. Sequence remains: 21, 37, 54, 68, 12, 9, 57, 85
  • Compare 68 and 12: 68 > 12. Swap. Sequence becomes: 21, 37, 54, 12, 68, 9, 57, 85
  • Compare 68 and 9: 68 > 9. Swap. Sequence becomes: 21, 37, 54, 12, 9, 68, 57, 85
  • Compare 68 and 57: 68 > 57. Swap. Sequence becomes: 21, 37, 54, 12, 9, 57, 68, 85

After the second pass, the sequence is: 21, 37, 54, 12, 9, 57, 68, 85. The element 68 is now in its final sorted position (before 85).

Intermediate Sequence After Second Pass

The intermediate sequence of the numbers 37, 54, 21, 85, 68, 12, 9, and 57 after the second pass of Bubble Sort in ascending order is 21, 37, 54, 12, 9, 57, 68, 85.

Revision Table: Bubble Sort Passes

Pass Comparisons (pairs) Resulting Sequence
Initial - 37, 54, 21, 85, 68, 12, 9, 57
Pass 1 (37,54), (54,21)*, (54,85), (85,68)*, (85,12)*, (85,9)*, (85,57)* 37, 21, 54, 68, 12, 9, 57, 85
Pass 2 (37,21)*, (37,54), (54,68), (68,12)*, (68,9)*, (68,57)* 21, 37, 54, 12, 9, 57, 68, 85

* indicates a swap occurred during the comparison.

Additional Information on Bubble Sort

Bubble Sort is often used for educational purposes to introduce the concept of sorting algorithms because of its simplicity. However, it is not efficient for large datasets due to its time complexity.

  • Time Complexity: The worst-case and average-case time complexity of Bubble Sort is O(n^2), where n is the number of elements. This happens when the list is in reverse order. The best-case time complexity is O(n) if the list is already sorted (requires one pass to detect no swaps).
  • Space Complexity: Bubble Sort has a space complexity of O(1) because it only requires a small amount of extra memory for temporary variables during swaps.
  • Stability: Bubble Sort is a stable sorting algorithm, meaning that elements with the same value maintain their relative order in the sorted output.
  • Adaptive: Bubble Sort can be optimized to detect if the list is already sorted in a pass. If no swaps occur in a pass, the algorithm can terminate early.

Understanding the intermediate steps, like finding the sequence after the second pass, helps in visualizing how the algorithm works and how elements gradually move towards their sorted positions.

Was this answer helpful?

Important Questions from Sorting - Teaching

  1. Which of the following is stable sort

  2. Which of the following algorithm design approach is used in Quick sort algorithm?

  3. What is the worst case running time of quick sort algorithm?

  4. Sort the following list using the Radix sort algorithm.

    329, 457, 839, 436, 720, 355, 657

    What is the output of the algorithm after the second pass?

  5. You are given a sequence of n elements to sort. The input sequence consists of n/k subsequences, each containing k elements. The elements in a given subsequence are all smaller than the elements in the succeeding subsequence and larger than the elements in the preceding subsequence. Thus, all that is needed to sort the whole sequence of length n is to sort the k elements in each of the n/k subsequences.

    The lower bound on the number of comparisons needed to solve this variant of the sorting problem is :

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App