`
jackey25
  • 浏览: 109020 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java冒泡排序

阅读更多
代码如下
c#的。回家再贴java的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Test2009_8_24
{
    class Program
    {
        
        public static void Main(String[] args)
        {
            int[] a = {1,3,7,5,4,34,22,9};
            BubbleSort(a);
            foreach (int cc in a)
            {
                System.Console.WriteLine(cc);
            }

        }

        public static void BubbleSort(int[] a)
        {
            int k = a.Length;
            int tmp;
            for (int j = a.Length-1; j >= 0; j--)
            {
                for (int b = 0; b < j; b++)
                {
                    if (a[b] > a[b+1])
                    {
                        tmp = a[b];
                        a[b] = a[b + 1];
                        a[b + 1] = tmp;
                    }
                }
            }
        }
    }
}
    


贴上java代码,跟c#没啥差别。


/*
 * 冒泡排序
 * author : Jackey Nie
 */
public class BubbleSort {
	public static void main(String[] args){
		int[] a = {7,8,5,4,33,24,65,2,1,45,9};
		BubbleSort(a);
		for(int cc : a){
			System.out.println(cc);
		}
	}
	private static void BubbleSort(int[] a){
		int i,j,tmp;
		for(int k=a.length-1;k>=0;k--){
			for(int m=0;m<k;m++){
				if(a[m]>a[m+1]){
					tmp = a[m];
					a[m] = a[m+1];
					a[m+1] = tmp;
				}
			}
		}
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics