MHB When Bubble Sort to sort a list of numbers 7, 12, 5, 22, 13, 32

Click For Summary
SUMMARY

The discussion centers on the implementation of the Bubble Sort algorithm to sort the list of numbers: 7, 12, 5, 22, 13, 32. Two examples were presented, with the first correctly demonstrating the sorting process through comparisons and swaps, while the second failed to show the necessary changes after each pass. It was confirmed that the number 32 is in its correct position at the end of the first pass when sorting in increasing order. Additionally, the number of comparisons in an unoptimized Bubble Sort remains constant across passes, while an optimized version reduces comparisons by one with each subsequent pass.

PREREQUISITES
  • Understanding of the Bubble Sort algorithm
  • Familiarity with sorting algorithms in Discrete Mathematics
  • Basic knowledge of algorithmic complexity
  • Ability to interpret algorithm visualizations and graphs
NEXT STEPS
  • Study the mechanics of Bubble Sort and its variations
  • Learn about algorithm optimization techniques for sorting
  • Explore visual representations of sorting algorithms
  • Investigate the time complexity of different sorting algorithms
USEFUL FOR

Students of Discrete Mathematics, software developers implementing sorting algorithms, and educators teaching algorithm visualization techniques.

Joystar77
Messages
122
Reaction score
0
Can somebody tell me which example is right when a question that is given to me says to bubble sort a list of numbers 7, 12, 5, 22, 13, 32? I found two examples and one was with a graph that included Original List, Pass 1, Pass 2, Pass 3, Pass 4, Pass, 5, and Pass 6, the numbers with 7 on one line, 12 on another line, 5 on another line, 22 on another line, 13 on another line, and 32 on another line which includes Comparisons, and Swaps. The second example was to do the following:

First Pass:

(7, 12, 5, 22, 13, 32)- Here, algorithm compares the first two elements, and swaps since 7 <12.
(7, 12, 5, 22, 13, 32)- Swap since 12 > 5.
(7, 12, 5, 22, 13, 32)- Swap since 5 < 22.
(7, 12, 5, 22, 13, 32)- Swap since 22 > 13.
(7, 12, 5, 22, 13, 32)- Now, since these elements are already in order (32 > 13), algorithm does not swap them.

Second Pass and Third Pass is done the same way.

Can someone please tell me which example is correct when bubble sorting the list of numbers?
 
Technology news on Phys.org
Discrete Mathematics (Bubble Sort to sort the list)

Is it true that the number 32 is definitely in its correct position at the end of the first pass when it comes to the following numbers: 7, 12, 5, 22, 13, 32?
 
Discrete Mathematics Numbers 7, 12, 5, 22, 13, 32

How does the number of comparisons required change as the pass number increases?
 
Re: Discrete Mathematics Numbers 7, 12, 5, 22, 13, 32

Joystar1977 said:
Can somebody tell me which example is right when a question that is given to me says to bubble sort a list of numbers 7, 12, 5, 22, 13, 32? I found two examples and one was with a graph that included Original List, Pass 1, Pass 2, Pass 3, Pass 4, Pass, 5, and Pass 6, the numbers with 7 on one line, 12 on another line, 5 on another line, 22 on another line, 13 on another line, and 32 on another line which includes Comparisons, and Swaps. The second example was to do the following:

First Pass:

(7, 12, 5, 22, 13, 32)- Here, algorithm compares the first two elements, and swaps since 7 <12.
(7, 12, 5, 22, 13, 32)- Swap since 12 > 5.
(7, 12, 5, 22, 13, 32)- Swap since 5 < 22.
(7, 12, 5, 22, 13, 32)- Swap since 22 > 13.
(7, 12, 5, 22, 13, 32)- Now, since these elements are already in order (32 > 13), algorithm does not swap them.

Second Pass and Third Pass is done the same way.

Can someone please tell me which example is correct when bubble sorting the list of numbers?
I did not understand the description of the graph very well. The second example is incorrect. First, the swaps are not shown: all lines are the same. Second, if the sorting is done in increasing order, then there is no reason to swap 7 and 12: they are already ordered correctly.

The list during the first pass should change as follows. (Compared elements are underlined.)

\begin{array}{rrrrrr} \underline{7} &\underline{12}& 5& 22& 13& 32\\ 7& \underline{12}& \underline{5}& 22& 13& 32\\ 7& 5& \underline{12}& \underline{22}& 13& 32\\ 7& 5& 12& \underline{22}& \underline{13}& 32\\ 7& 5& 12& 13& \underline{22}& \underline{32}\\ 7& 5& 12& 13& 22& 32\end{array}

Joystar1977 said:
Is it true that the number 32 is definitely in its correct position at the end of the first pass when it comes to the following numbers: 7, 12, 5, 22, 13, 32?
Yes, if the sorting is done in increasing order.

Joystar1977 said:
How does the number of comparisons required change as the pass number increases?
In the unoptimized bubble sort, the number of comparisons is the same for all passes. In the optimized sort, the number decreases by 1 from one pass to the next because numbers at the end of the array are in their correct positions. Thus, for an array of $n$ numbers the first pass requires $n-1$ comparisons, the second $n-2$ comparisons, and so on.
 
Thank you Evgeny.Makarov! I seen two different examples of Bubble Sorting and was trying to see which is done correctly. I wanted to just clarify which one I am supposed to use when Bubble Sorting. Am I correct then that its easier to see things on a graph for Bubble Sorting such as having the Original List of numbers with 7 on one line, 12 on another line, 5 on another line, 22 on another line, 13 on another line, and 32on another line, Pass 1, Pass, 2, Pass 3, Pass 4, Pass 5, Pass 6, Comparisons, and Swaps? Just trying to get a thorough understanding of this.
 
Joystar1977 said:
Am I correct then that its easier to see things on a graph for Bubble Sorting such as having the Original List of numbers with 7 on one line, 12 on another line, 5 on another line, 22 on another line, 13 on another line, and 32on another line, Pass 1, Pass, 2, Pass 3, Pass 4, Pass 5, Pass 6, Comparisons, and Swaps? Just trying to get a thorough understanding of this.
I am having trouble understanding this description of a graph, so I can't say whether it makes sense. Execution of Bubble sort can be illustrated in many ways. The most natural, I think, is to show the array after each comparison and potential swap. I showed the first pass in this way in my previous post. For another illustration, see Wikipedia. Note that while there are many ways to illustrate the execution of the Bubble sort, the algorithm itself works in a unique, fixed way. It is this way of working that is important, not whether it is illustrated as a graph or as a sequence of lines.
 
Thanks for the explanation Evgeny.Makarov!
 

Similar threads

Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 68 ·
3
Replies
68
Views
12K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
2K