冒泡排序算法

RUBY:

def bubbleSort(array)
    return array if array.size < 2
    (array.size - 2).downto(0do |i|
        (0 .. i).each do |j|
            array[j], array[j + 1] = array[j + 1], array[j] if array[j] >= array[j + 1]
        end
    end
    return array
end
 
PHP:
function bubbleSort($numbers) {
    $cnt count($numbers);
    for ($i = 0; $i $cnt$i++) {
        for ($j = 0; $j $cnt $i - 1; $j++) {
            if ($numbers[$j] > $numbers[$j + 1]) {
                $temp $numbers[$j];
                $numbers[$j] = $numbers[$j + 1];
                $numbers[$j + 1] = $temp;
            }
        }
    }
 
    return $numbers;
}
 
$num array(20, 40, 60, 80, 30, 70, 90, 10, 50, 0);
var_dump(bubbleSort($num));
 
//输出结果如下:
//array(10) {
//  [0]=>
//  int(0)
//  [1]=>
//  int(10)
//  [2]=>
//  int(20)
//  [3]=>
//  int(30)
//  [4]=>
//  int(40)
//  [5]=>
//  int(50)
//  [6]=>
//  int(60)
//  [7]=>
//  int(70)
//  [8]=>
//  int(80)
//  [9]=>
//  int(90)
//}
 
C#语言:
 
冒泡算法C#
namespace 数组排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int temp = 0;
            int[] arr = {23, 44, 66, 76, 98, 11, 3, 9, 7};
            #region该段与排序无关
            Console.WriteLine("排序前的数组:");
            foreach (intiteminarr)
            {
                Console.Write(item + "");
            }
            Console.WriteLine();
            #endregion
            for (int i = 0; i < arr.Length - 1; i++)
            {
                #region将大的数字移到数组的arr.Length-1-i
                for (int j = 0; j < arr.Length - 1 - i; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                    }
                }
            #endregion
            }
            Console.WriteLine("排序后的数组:");
            foreach (int item in arr)
            {
                Console.Write(item+"");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}
 
C语言:
 
#include <stdio.h>
#define SIZE 8
 
void bubble_sort(int a[], int n);
 
void bubble_sort(int a[], int n)
{
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
        for (i = 0; i < n - 1 - j; i++)
        {
            if(a[i] > a[i + 1])
            {
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
}
 
int main()
{
    int number[SIZE] = {95, 45, 15, 78, 84, 51, 24, 12};
    int i;
    bubble_sort(number, SIZE);
    for (i = 0; i < SIZE; i++)
    {
        printf("%d", number[i]);
    }
    printf("\n");
}
 
JAVA:
 
public class BubbleSort
{
    public void sort()
    {
        int [] array = new int;
        
        int temp = 0 ;
        for (int i = 0 ; i < array.Length - 1 ; i++)
        {
        for (int j = i + 1 ; j < array.Length ; j++)
          {
           if (array[j] < array[i])
           {
            temp = array[i] ;
            array[i] = array[j] ;
            array[j] = temp ;
                   }
          }
        }
    }
}

posted on 2016-11-18 14:54  大芳芳  阅读(114)  评论(0编辑  收藏  举报