How Many Swaps Can Bubble Sort Take for Six Items?

  • Thread starter Thread starter phospho
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary

Discussion Overview

The discussion centers on determining the maximum number of swaps required to sort a list of six items using the bubble sort algorithm. Participants explore the mathematical reasoning behind the number of swaps needed in various scenarios, including worst-case conditions, and seek to generalize the findings for any number of items.

Discussion Character

  • Mathematical reasoning
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant suggests that the maximum number of swaps for six items is 15, based on a summation of decreasing swaps needed for each pass.
  • Another participant provides a practical example using Java syntax to illustrate the sorting of an array from highest to lowest, explaining the number of exchanges required for each element.
  • A similar viewpoint is reiterated by another participant, emphasizing the worst-case scenario and proposing a general formula for the number of swaps as the summation of exchanges needed.
  • A different participant introduces a method for summing an incrementing series of numbers, leading to the conclusion that the total number of swaps can be expressed as (n(n-1))/2.

Areas of Agreement / Disagreement

Participants present multiple approaches and formulas for calculating the number of swaps, but there is no consensus on a single definitive method or conclusion. The discussion remains exploratory with various interpretations of the problem.

Contextual Notes

Some participants note that the discussion may be more appropriate for a computer science context, while others argue it fits within mathematical reasoning. There are also references to educational curricula that may influence perspectives on the topic.

phospho
Messages
250
Reaction score
0
Q: Find the maximum number of interchanges needed to sort a list of six pieces of data using the bubble-sort algorithm

working:

for the first past, maximum needed is n-1 swaps where n is the amount of the pieces of data
2nd - n-2
3rd - n-3
4th - n-4
5th - n-5

so generally it takes

(n-1) + (n-2) +... (n-(n-1))

is this correct? It works for six as the it will be a maximum of 15 swaps, also how can I generalise this?
 
Physics news on Phys.org
This would be more suitable for the computer science section, but hey ill help.

Consider an array of natural numbers ( I'll be using some java syntax here to provide a practical example ) :

int[] nums = {0,1,2,3,4,5,...n};

and presume you want to sort it from highest to lowest ( So we have a worst case scenario here ).

There are n elements in the array, so to move 0 to the nth position would require n-1 exchanges.

The next exchange for 1 would require n-2 exchanges.

This continues until you have n-(n-1) exchanges since n-n would be redundant because the list is sorted at that point.

I suppose a way you could generalize this would be to say :

[itex]\sum_{k=1}^{n-1}[/itex] (n-k)
 
Zondrina said:
This would be more suitable for the computer science section, but hey ill help.

Consider an array of natural numbers ( I'll be using some java syntax here to provide a practical example ) :

int[] nums = {0,1,2,3,4,5,...n};

and presume you want to sort it from highest to lowest ( So we have a worst case scenario here ).

There are n elements in the array, so to move 0 to the nth position would require n-1 exchanges.

The next exchange for 1 would require n-2 exchanges.

This continues until you have n-(n-1) exchanges since n-n would be redundant because the list is sorted at that point.

I suppose a way you could generalize this would be to say :

[itex]\sum_{k=1}^{n-1}[/itex] (n-k)
Cheers,

btw I had no idea it was the wrong section, in the UK, this is under our maths syllabus and we're not programming anything, but I can see how it leads to things :)

thanks again.
 
There's a trick to adding up an incrementing series of numbers, arrange the numbers forwards and backards and add up the sum:

Code:
n-1 + n-2 + n-3 + ... +   1
  1 +   2 +   3 _ ... + n-1
---------------------------
  n +   n +   n + ... +   n

This sum equals n (n-1), but since the array was added twice divide by 2:

(n-1) + (n-2) + (n-3) + ... + 1 = n (n-1) / 2
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K