冒泡排序代碼

來源:互聯網
上載者:User
AAuto

  bubble_sort = function(array){

  var temp;

  for( i=1;#array ){

  //i前面的已經是最小的數,並排序好了

  for(j=#array;i+1;-1){

  //挨個比較

  if(array[j]<array[j-1]){

  //小的總是往前排

  bubble = array[j]

  array[j] = array[j-1];

  array[j-1] = bubble;

  }

  }

  }

  }

  io.print("----------------")

  io.print("冒泡排序( 交換類換排序 )")

  io.print("----------------")

  array ={2;46;5;17;1;2;3;99;12;56;66;21};

  bubble_sort(array,1,#array)

  //輸出結果

  for(i=1;#array;1){

  io.print( array[i] )

  }

C

  void bubble_sort(int *x, int n)

  {

  int j, k, h, t;

  for (h=n-1; h>0; h=k) /*迴圈到沒有比較範圍*/

  {

  for (j=0, k=0; j<h; j++) /*每次預置k=0,迴圈掃描後更新k*/

  {

  if (*(x+j) > *(x+j+1)) /*大的放在後面,小的放到前面*/

  {

  t = *(x+j);

  *(x+j) = *(x+j+1);

  *(x+j+1) = t; /*完成交換*/

  k = j; /*儲存最後下沉的位置。這樣k後面的都是排序排好了的。*/

  }

  }

  }

  }

C++

  #include <iostream>

  #define LEN 9

  using namespace std;

  int main()

  {

  int nArray[LEN];

  for(int i=0;i<LEN;i++)nArray[i]=LEN-i;

  cout<<"未經處理資料為:"<<endl;

  for(int i=0;i<LEN;i++)cout<<nArray[i]<<" ";

  cout<<endl;

  //開始冒泡

  {

  int temp;

  for(int i=LEN-1;i>0;i--)

  for(int j=0;j<i;j++)

  {

  if(nArray[j]>nArray[j+1])

  {

  temp=nArray[j];

  nArray[j]=nArray[j+1];

  nArray[j+1]=temp;

  }

  }

  }

  //結束冒泡

  cout<<"排序結果:"<<endl;

  for(int i=0;i<LEN;i++)cout<<nArray[i]<<" ";

  return 0;

  }

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

  // 冒泡排序

  public class BubbleSort {

  public static void sort(Comparable[] data) {

  // 數組長度

  int len = data.length;

  for (int i = 0; i < len - 1; i++) {

  // 臨時變數

  Comparable temp = null;

  // 交換標誌,false表示未交換

  boolean isExchanged = false;

  for (int j = len - 1; j > i; j--) {

  // 如果data[j]小於data[j - 1],交換

  if (data[j].compareTo(data[j - 1]) < 0) {

  temp = data[j];

  data[j] = data[j - 1];

  data[j - 1] = temp;

  // 發生了交換,故將交換標誌置為真

  isExchanged = true;

  }// end if

  }// end for

  // 本趟排序未發生交換,提前終止演算法,提高效率

  if (!isExchanged) {

  return;

  }// end if

  }// end for

  }// end sort

  public static void main(String[] args) {

  // 在JDK1.5版本以上,基礎資料型別 (Elementary Data Type)可以自動裝箱

  // int,double等基本類型的封裝類已實現了Comparable介面

  Comparable[] c = { 4, 9, 23, 1, 45, 27, 5, 2 };

  sort(c);

  for (Comparable data : c) {

  System.out.println(data);

  }

  }

  }

Visual Basic

  Sub ttttttttt()

  Dim a, c As Variant

  Dim i As Integer, j As Integer, temp As Integer, bSwap As Boolean

  a = Array(17, 45, 12, 80, 50)

  For j = 0 To UBound(a) - 1

  bSwap = False

  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

  bSwap = True

  End If

  Next

  If bSwap = False Then

  Exit For

  End If

  Next

  For Each c In a

  Debug.Print c;

  Next

  End Sub

Pascal

  program name;

  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 - 1; i++)

  {

  for (int j = length - 1; j > i; j--)

  {

  if (array[j] < array[j - 1] )

  {

  int temp = array[j];

  array[j] = array[j - 1];

  array[j - 1] = temp;

  }

  }

  }

  }

Python

  #BubbleSort used python3.1 or python 2.x

  def bubble(str):

  tmplist = list(str)

  count = len(tmplist)

  for i in range(0,count-1):

  for j in range(0,count-1):

  if tmplist[j] > tmplist[j+1]:

  tmplist[j],tmplist[j+1] = tmplist[j+1],tmplist[j]

  return tmplist

  #useage:

  str = "zbac"

  print(bubble(str)) # ['a', 'b', 'c', 'z']

  number=[16,134,15,1]

  print(bubble(number)) # [1, 15, 16, 134]

JS

  <script language="javascript">

  var DataOne=new Array(5,6,7,8,3,1,2,-1,100)

  var len=DataOne.length

  for(var i=0;i<len;i++)

  {

  for(var j=0;j<len;j++)

  {

  One=DataOne[j]

  Two=DataOne[j+1]

  if(One<Two)

  {

  DataOne[j]=Two

  DataOne[j+1]=One

  }

  }

  }

  var str=""

  for(var n=0;n<len;n++)

  {

  str+=DataOne[n]+","

  }

  alert(str)

  </script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.