冒泡排序  冒泡排序:BubbleSort

来源:互联网 发布:以太坊 json rpc教程 编辑:程序博客网 时间:2024/04/28 10:26

 编辑词条 冒泡排序  冒泡排序:BubbleSort
  基本概念
  冒泡排序的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。重复以上过程,仍从第一对数开始比较(因为可能由于第2个数和第3个数的交换,使得第1个数不再大于第2个数),将小数放前,大数放后,一直比较到最小数前的一对相邻数,将小数放前,大数放后,第二趟结束,在倒数第二个数中得到一个新的最小数。如此下去,直至最终完成排序。
  由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序。
  用二重循环实现,外循环变量设为i,内循环变量设为j。外循环重复9次,内循环依次重复9,8,...,1次。每次进行比较的两个元素都是与内循环j有关的,它们可以分别用a[j]和a[j+1]标识,i的值依次为1,2,...,9,对于每一个i, j的值依次为1,2,...10-i。
  产生
  在许多程序设计中,我们需要将一个数列进行排序,以方便统计,常见的排序方法有冒泡排序,二叉树排序,选择排序等等。而冒泡排序一直由于其简洁的思想方法和比较高的效率而倍受青睐。
  排序过程
  设想被排序的数组R[1..N]垂直竖立,将每个数据元素看作有重量的气泡,根据轻气泡不能在重气泡之下的原则,从下往上扫描数组R,凡扫描到违反本原则的轻气泡,就使其向上"漂浮",如此反复进行,直至最后任何两个气泡都是轻者在上,重者在下为止。
  算法示例
  49 13 13 13 13 13 13 13
  38 49 27 27 27 27 27 27
  65 38 49 38 38 38 38 38
  97 65 38 49 49 49 49 49
  76 97 65 49 49 49 49 49
  13 76 97 65 65 65 65 65
  27 27 76 97 76 76 76 76
  49 49 49 76 97 97 97 97
  Procedure BubbleSort(Var R : FileType) //从下往上扫描的起泡排序//
  Begin
  For I := 1 To N-1 Do //做N-1趟排序//
  begin
  NoSwap := True; //置未排序的标志//
  For J := N - 1 DownTo 1 Do //从底部往上扫描//
  begin
  If R[J+1]< R[J] Then //交换元素//
  begin
  Temp := R[J+1]; R[J+1 := R[J]; R[J] := Temp;
  NoSwap := False
  end;
  end;
  If NoSwap Then Return//本趟排序中未发生交换,则终止算法//
  end
  End; //BubbleSort//
  该算法的时间复杂性为O(n2),算法为稳定的排序方
  冒泡排序c++代码
  #include <iostream.h>
  void BubbleSort(int* pData,int Count)
  {
  int iTemp;
  for(int i=1;i<Count;i++)
  {
  for(int j=Count-1;j>=i;j--)
  {
  if(pData[j]<pData[j-1])
  {
  iTemp = pData[j-1];
  pData[j-1] = pData[j];
  pData[j] = iTemp;
  }
  }
  }
  }
  void main()
  {
  int data[] = {10,9,8,7,6,5,4};
  BubbleSort(data,7);
  for (int i=0;i<7;i++)
  cout<<data[i]<<" ";
  cout<<"/n";
  }
  冒泡排序PHP代码
  <?php
  //冒泡排序(一维数组)
  function bubble_sort($array)
  {
  $count = count($array);
  if ($count <= 0) return false;
  for($i=0; $i<$count; $i++)
  {
  for($j=$count-1; $j>$i; $j--)
  {
  if ($array[$j] < $array[$j-1])
  {
  $tmp = $array[$j];
  $array[$j] = $array[$j-1];
  $array[$j-1] = $tmp;
  }
  }
  }
  return $array;
  }
  //使用实例
  $_array = array('5', '8' ,'5' ,'6' ,'9' ,'3' ,'2' ,'4');
  $_array = bubble_sort($_array);
  print ($_array);
  ?>
  冒泡排序Ruby代码
  def bubble(arr)
  (arr.length-1).downto(1) do |j|
  a1 = arr.dup
  j.times do |i|
  if arr > arr[i+1]
  arr,arr[i+1] = arr[i+1],arr
  end
  end
  break if a1 == arr
  end
  arr
  end
  冒泡排序Java代码
  static void BubbleSort(int a []){
  int temp=0;
  for (int i = 0; i < a.length ; i++) {
  for (int j = 0; j < a.length - i - 1; j++){
  if (a[j]>a[j + 1]){ //把这里改成大于,就是升序了
  temp=a[j];
  a[j]=a[j + 1];
  a[j + 1]=temp;
  }
  }
  }
  }
  冒泡排序Visual Basic代码
  Option Explicit
  Private Sub Form_click()
  Dim a, c As Variant
  Dim i As Integer, temp As Integer, w As Integer
  a = Array(12, 45, 17, 80, 50)
  For i = 0 To UBound(a) - 1
  If (a(i) > a(i + 1)) Then '若是递减,改为a(i)<a(i+1)
  temp = a(i)
  a(i) = a(i + 1)
  a(i + 1) = temp
  End If
  Next
  For Each c In a
  Print c;
  Next
  End Sub
  冒泡排序Pascal代码
  <i id="bks_9tjbxut2">program bubblesort;
  const
  N=20;
  MAX=10;
  var
  a:array[1..N] of 1..MAX;
  temp,i,j:integer;
  begin
  randomize;
  for i:=1 to N do a:=1+random(MAX);
  writeln('Array before sorted:');
  for i:=1 to N do write(a,' ');
  writeln;
  for i:=N-1 downto 1 do
  for j:=1 to i do
  if a[j]<a[j+1] then
  begin
  temp:=a[j];
  a[j]:=a[j+1];
  a[j+1]:=temp
  end;
  writeln('Array sorted:');
  for i:=1 to N do write(a,' ');
  writeln;
  writeln('End sorted.');
  readln;
  end.
  冒泡排序C#代码
  public void BubbleSort(int[] array) {
  int length = array.Length;
  for (int i = 0; i <= length - 2; i++) {
  for (int j = length - 1; j >= 1; j--) {
  if (array[j] < array[j - 1] ) {
  int temp = array[j];
  array[j] = array[j - 1];
  array[j - 1] = temp;
  }
  }
  }
  }
  冒泡排序Python代码
  #algo
  def bubble(list):
  count = len(list) -1
  while count > 0 :
  i = 0
  onceflag = True
  while i < count :
  if int(list) > int(list[i+1]) :
  tmp = list
  list = list [i+1]
  list[i+1] =tmp
  onceflag = False
  i = i + 1
  if onceflag : return list
  count = count - 1
  return list
  #test
  li = [1,9,8,5,4,3,6,7,0,2]
  print bubble(li)
  冒泡排序法的改进
  比如用冒泡排序将4、5、7、1、2、3这6个数排序。在该列中,第二趟排序结束后,数组已排好序,但计算机此时并不知道已经反排好序,计算机还需要进行一趟比较,如果这一趟比较,未发生任何数据交换,则知道已排序好,可以不再进行比较了。因而第三趟比较还需要进行,但第四、五趟比较则是不必要的。为此,我们可以考虑程序的优化。
  为了标志在比较中是否进行了,设一个布尔量flag。在进行每趟比较前将flag置成true。如果在比较中发生了数据交换,则将flag置为false,在一趟比较结束后,再判断flag,如果它仍为true(表明在该趟比较中未发生一次数据交换)则结束排序,否则进行下一趟比较。
  性能分析
  若记录序列的初始状态为"正序",则冒泡排序过程只需进行一趟排序,在排序过程中只需进行n-1次比较,且不移动记录;反之,若记录序列的初始状态为"逆序",则需进行n(n-1)/2次比较和记录移动。因此冒泡排序总的时间复杂度为O(n*n)。


百度百科中的词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。 本词条对我有帮助
354
 
扩展阅读:
1.http://www.zjyz.org/jiaoshi/jszy/Exg/competition/algorithm/sort2.htm
2.http://www.window07.com/dev/code/2006-3-4/k74957.htm
3.http://zn3.ccjy.cn/wz/olympiad/sjjg/16.htm
4.http://ruby-lang.org.cn/forums/redirect.php?tid=4251&goto=lastpost#lastpost
[我来完善]相关词条:
更多
快速排序算法基数排序直接插入排序堆排序指针函数函数指针希尔排序结构体杨辉三角
插入排序选择排序归并排序递归Shell排序 开放分类:
编程,算法,数据结构,排序组合