3
1
Your task is to create a program that performs a bubble sort on a given array until it is sorted.
Algorithm
In order to qualify as a bubble sort, the program must operate as follows:
- Each pass goes through the array, looks at every two adjacent elements in turn, and potentially swaps them.
- Each pass goes through the array in the same direction and without gaps. There are no partial passes - each pass must go through the whole array.
- The program must never compare or swap array elements that aren't adjacent.
- A sufficient (and finite) number of passes must be performed, such that the output array is guaranteed to be sorted. (E.g. you can safely stop if there was a whole pass without any swaps, or after
len(array)passes have been made, etc.)
Rules
- You must provide a sufficient explanation for how your answer does a bubble sort.
- You may not use built-in or standard library sorting algorithms. You must make your own code.
- Your program should take an array of numbers as input and output them in the correct order.
- Input:
10 5 9 2 . . . (and so on) - The program should always output the sorted array.
- Output can be an array returned by the program when run, or it can modify the input array in-place.
- Output can also be the correctly ordered values separated by whitespace.
- Input:
- Speed does not matter as much as if the program functions.
Test Cases
10 5 9 2 1 6 8 3 7 4(you can format it differently depending on your language)1 2 5 4 3
This is code-golf, so the shortest answer in bytes wins.
@Suever There is no set number of comparisons for this challenge. – ckjbgames – 2017-01-28T18:34:16.010
7
Saying "use this algorithm" is usually seen as a non-observable requirement. Perhaps "perform one iteration" of bubble sort is a better challenge?
– FlipTack – 2017-01-28T18:43:31.5077Also, 10 minutes in the sandbox isn't very long. You should leave it there for longer (at least a day, if not more) to get more feedback first. – FlipTack – 2017-01-28T18:45:16.143
@FlipTack I will remember for next time I ask something – ckjbgames – 2017-01-28T18:46:25.670
5
It seems very similar to this challenge, except that one has a parameter for max comparisons.
– FlipTack – 2017-01-28T18:47:16.0771It's well known that many people mix up the various simple quadratic sorts. I agree with FlipTack that asking specifically for bubble sort makes a bad question, but I would add that if you do want to require a specific algorithm then you must clearly specific exactly what qualifies as that algorithm. This question as it stands is inviting arguments over the difference between insertion sort, selection sort, and bubble sort. – Peter Taylor – 2017-01-28T19:51:22.833
1@PeterTaylor: It could be distinguished from those two, by requiring that comparisons are only ever allowed between two adjacent elements. – smls – 2017-01-28T20:03:35.003
@smls, that doesn't distinguish insertion sort from bubble sort, which rather supports my point. – Peter Taylor – 2017-01-28T22:26:24.890
1http://codegolf.stackexchange.com/questions/92753/bubble-sorting-in-progress – Magic Octopus Urn – 2017-01-30T14:23:47.373
@carusocomputing This challenge requires you to perform a bubble sort until the array is sorted, rather than a specific number of times. – ckjbgames – 2017-01-30T14:26:23.383
And we're saying that's not a good requirement, because [code-golf] generally only cares about input and output, not process. – mbomb007 – 2017-01-30T14:41:15.810
@mbomb007 There are differences. My challenge asks that it takes 1 input and outputs an array that is in order no matter what, while the other challenge takes 2 inputs and only outputs the array if it is sorted in n iterations of bubble sort, where n is the second argument. – ckjbgames – 2017-01-30T14:47:26.070
Maybe you should read the meta post that Fliptack linked to. – mbomb007 – 2017-01-30T14:54:55.327
ckjbgames, I edited the task to add a clear(er) specification of what counts as a bubble sort is. Feel free to revert if you don't agree. @PeterTaylor, does this sufficiently distinguish it from insertion sort? – smls – 2017-01-30T16:23:08.607
@smls I think this distinguishes it enough. – ckjbgames – 2017-01-30T16:24:31.217
Bubble sort is well defined and the OP has a link to the wikipedia article for it. In my opinion this should be more than sufficient when it comes to distinguishing bubble sort from other sorts. – Poke – 2017-01-30T16:25:58.063
@FlipTack in his defense, it has been upvoted 3 times. – None – 2017-01-30T16:57:06.653
@JackBates yes, but also two downvotes, and the post was actually closed at first until it was more clearly specified. These flaws could have been eliminated by posting in the sandbox first, as everyone should. – FlipTack – 2017-01-30T17:25:37.160