哎,好久没有来,冒泡一个

就呆在云上 2008-12-16 09:03:02

谁给讲讲冒泡排序算法!?
讲好了,给100分!

给一个论坛里面的300分!
...全文
456 104 打赏 收藏 转发到动态 举报
写回复
用AI写文章
104 条回复
切换为时间正序
请发表友善的回复…
发表回复
就呆在云上 2008-12-18
  • 打赏
  • 举报
回复
真的有牛人啊

大家好
zzhouxiaoyu 2008-12-18
  • 打赏
  • 举报
回复
挺佩服94楼的
BeHardOnSelf 2008-12-18
  • 打赏
  • 举报
回复
晕。。。。
三文鱼也会飞 2008-12-18
  • 打赏
  • 举报
回复
呵呵,楼主我来接分的
i_am_different 2008-12-18
  • 打赏
  • 举报
回复
不是吧????jf吧还是!!!
sincor 2008-12-18
  • 打赏
  • 举报
回复
有人还真较真了 或许是领悟到你的幽默了
condor888 2008-12-18
  • 打赏
  • 举报
回复
你怎么还没结贴?
ken_scott 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 38 楼 zhanshen2891 的回复:]
如果不给我分以后再也不给你回帖!!!
[/Quote]
碼上道 2008-12-18
  • 打赏
  • 举报
回复
林子大了,什么鸟都有的,楼主是好鸟
zedzhao 2008-12-17
  • 打赏
  • 举报
回复
up~~~~~~~~
晴风smile 2008-12-17
  • 打赏
  • 举报
回复
来看下
随便学习学习~~!
tianfu1 2008-12-17
  • 打赏
  • 举报
回复
哪个泡比较轻,哪个泡就会被排到上边
elegant87 2008-12-17
  • 打赏
  • 举报
回复
顶个泡!
lc19890326 2008-12-17
  • 打赏
  • 举报
回复
顶啊
backway 2008-12-17
  • 打赏
  • 举报
回复
这楼299
JIGSONG 2008-12-17
  • 打赏
  • 举报
回复
哈哈
bopizi 2008-12-17
  • 打赏
  • 举报
回复
⊙⊙
⊙ ⊙
⊙ ⊙
⊙ ⊙
⊙⊙ ⊙⊙
⊙ ⊙ ⊙ ⊙
⊙ ⊙ ⊙ ⊙
⊙ ⊙ ⊙⊙



fallening 2008-12-17
  • 打赏
  • 举报
回复
Bubble sort
From Wikipedia, the free encyclopedia
Jump to: navigation, search
Bubble sort
General Data
Class: Sorting algorithm
Data Structure: Array
Time Complexity: О(n²)
Space Complexity: О(n) total, O(1) auxiliary
Optimal: No

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.
Contents
[hide]

* 1 Analysis
o 1.1 Performance
o 1.2 Rabbits and turtles
o 1.3 Step-by-step example
o 1.4 Pseudocode implementation
o 1.5 Alternative implementations
* 2 In practice
* 3 Variations
* 4 References
* 5 External links

[edit] Analysis

[edit] Performance

Bubble sort has worst-case and average complexity both О(n²), where n is the number of items being sorted. There exist many sorting algorithms with the substantially better worst-case or average complexity of O(n log n). Therefore bubble sort is not a practical sorting algorithm when n is large, except in rare specific applications where the array is known to be very close to being already sorted initially.

[edit] Rabbits and turtles

The positions of the elements in bubble sort will play a large part in determining its performance. Large elements at the beginning of the list do not pose a problem, as they are quickly swapped. Small elements towards the end, however, move to the beginning extremely slowly. This has led to these types of elements being named rabbits and turtles, respectively.

Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. Cocktail sort does pretty well, but it still retains O(n2) worst-case complexity. Comb sort compares elements large gaps apart and can move turtles extremely quickly, before proceeding to smaller and smaller gaps to smooth out the list. Its average speed is comparable to faster algorithms like Quicksort.

[edit] Step-by-step example

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared.

First Pass:
( 5 1 4 2 8 ) \to ( 1 5 4 2 8 ) Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) \to ( 1 4 5 2 8 )
( 1 4 5 2 8 ) \to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) \to ( 1 4 2 5 8 ) Now, since these elements are already in order, algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) \to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. Algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
Finally, the array is sorted, and the algorithm can terminate.

[edit] Pseudocode implementation

The algorithm can be expressed as:

procedure bubbleSort( A : list of sortable items ) defined as:
  do
swapped := false
for each i in 0 to length(A) - 2 inclusive do:
if A[ i ] > A[ i + 1 ] then
swap( A[ i ], A[ i + 1 ] )
swapped := true
end if
end for
while swapped
end procedure


[edit] Alternative implementations

A common way to optimize bubblesort is to note that, after each pass, the largest element will always move down to the end. During each comparison, it is clear that the largest element will move downwards. Given a list of size n, the nth element will be guaranteed to be in its proper place. Thus it suffices to sort the remaining n - 1 elements. Again, after this pass, the n - 1th element will be in its final place.

In pseudocode, this will cause the following changes:

procedure bubbleSort( A : list of sortable items ) defined as:
  n := length( A )
do
swapped := false
n := n - 1
for each i in 0 to n - 1 inclusive do:
if A[ i ] > A[ i + 1 ] then
swap( A[ i ], A[ i + 1 ] )
swapped := true
end if
end for
while swapped
end procedure


We can then do bubbling passes over increasingly smaller parts of the list. More precisely, instead of doing n2 comparisons (and swaps), we can use only (n-1) + (n-2) + ... + 1 comparisons. This sums up to n(n - 1) / 2, which is still O(n2), but which can be considerably faster in practice.

[edit] In practice

Although bubble sort is one of the simplest sorting algorithms to understand and implement, its O(n2) complexity means it is far too inefficient for use on lists having more than a few elements. Even among simple O(n2) sorting algorithms, algorithms like insertion sort are usually considerably more efficient.

Due to its simplicity, bubble sort is often used to introduce the concept of an algorithm, or a sorting algorithm, to introductory computer science students. However, some researchers such as Owen Astrachan have gone to great lengths to disparage bubble sort and its continued popularity in computer science education, recommending that it no longer even be taught.[1]

The Jargon file, which famously calls bogosort "the archetypical perversely awful algorithm", also calls bubble sort "the generic bad algorithm".[2] Donald Knuth, in his famous The Art of Computer Programming, concluded that "the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems", some of which he discusses therein.

Bubble sort is asymptotically equivalent in running time to insertion sort in the worst case, but the two algorithms differ greatly in the number of swaps necessary. Experimental results such as those of Astrachan have also shown that insertion sort performs considerably better even on random lists. For these reasons many modern algorithm textbooks avoid using the bubble sort algorithm in favor of insertion sort.

Bubble sort also interacts poorly with modern CPU hardware. It requires at least twice as many writes as insertion sort, twice as many cache misses, and asymptotically more branch mispredictions. Experiments by Astrachan sorting strings in Java show bubble sort to be roughly 5 times slower than insertion sort and 40% slower than selection sort.[3]

[edit] Variations

* Odd-even sort is a parallel version of bubble sort, for message passing systems.
* In some cases, the sort works from right to left (the opposite direction), which is more appropriate for partially sorted lists, or lists with unsorted items added to the end.

[edit] References

1. ^ http://www.cs.duke.edu/~ola/papers/bubble.pdf
2. ^ http://www.jargon.net/jargonfile/b/bogo-sort.html
3. ^ Owen Astrachan. Bubble Sort: An Archaeological Algorithmic Analysis. SIGCSE 2003. (pdf)

* Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 106–110 of section 5.2.2: Sorting by Exchanging.
* Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Problem 2-2, pg.38.
* Sorting in the Presence of Branch Prediction and Caches


分来~~~~
lzh9955 2008-12-17
  • 打赏
  • 举报
回复
up
lynn_9527 2008-12-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sagegz 的回复:]
冒泡排序(Bubble Sort,台湾译为:泡沫排序或气泡排序)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个项目,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

冒泡排序对n个项目需要O(n2)的比较次数,且可以原地排序。尽管这个算法是最简单了解和实作的排序算法…
[/Quote]
加载更多回复(83)

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧