首页 > 学院 > 开发设计 > 正文

java四种数组排序

2019-11-14 20:57:21
字体:
来源:转载
供稿:网友
java四种数组排序

数组的四种排序

1.快速排序法Arrays.sort();

2.冒泡排序法

 1 int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51};   2     int temp=0;   3         for (int i = 0; i < a.length - 1; i++) 4         { 5             for (int j = 0; j < a.length - 1 - i; j++) 6             { 7                 if (a[j] > a[j + 1]) 8                 { 9                     temp = a[j];10                     a[j] = a[j + 1];11                     a[j + 1] = temp;12                 }13             }14         }

3.选择排序

 1  int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51};   2    for(int i=0;i<a.length;i++){ 3        for(int j=i+1;j<a.length;j++){ 4            if(a[i]>a[j]){ 5                int temp=a[i]; 6                a[i]=a[j]; 7                a[j]=temp; 8            } 9        }10    }

4.插入排序

int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51};       for (int i = 1; i < a.length; i++)        {            for (int j = i; j > 0; j--)            {                if (a[j] < a[j - 1])                {                    int temp = a[j - 1];                    a[j - 1] = a[j];                    a[j] = temp;                }                else                    break;            }        }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表