2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,15 +1,19 @@
|
|||
{{Sorting Algorithm}}
|
||||
In this task, the goal is to sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
|
||||
|
||||
;Task:
|
||||
Sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
|
||||
|
||||
The bubble sort is generally considered to be the simplest sorting algorithm.
|
||||
|
||||
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
|
||||
|
||||
Because of its abysmal O(n<sup>2</sup>) performance, it is not used often for large (or even medium-sized) datasets.
|
||||
|
||||
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
|
||||
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
|
||||
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
|
||||
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
|
||||
A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
|
||||
|
||||
This can be expressed in pseudocode as follows (assuming 1-based indexing):
|
||||
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
|
||||
'''repeat'''
|
||||
hasChanged := false
|
||||
'''decrement''' itemCount
|
||||
|
|
@ -19,6 +23,8 @@ This can be expressed in pseudocode as follows (assuming 1-based indexing):
|
|||
hasChanged := true
|
||||
'''until''' hasChanged = '''false'''
|
||||
|
||||
|
||||
;References:
|
||||
* The article on [[wp:Bubble_sort|Wikipedia]].
|
||||
* Dance [http://www.youtube.com/watch?v=lyZQPjUT5B4&feature=youtu.be interpretation].
|
||||
<br><br>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue