java数组有排序方法吗_JAVA 中数组的几种排序方法

1、数组的冒泡排序

public void bubbleSort(int a[]) {

int n = a.length;

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

for (int j = i+1; j < n ; j++) {

if (a[j] > a[j + 1]) {

int temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

}

}

}

}

2、数组的选择排序

public void selectSort(int a[]) {

for (int n = a.length; n > 1; n--) {

int i = max(a, n);

int temp = a[i];

a[i] = a[n - 1];

a[n - 1] = temp;

}

}

3、数组的插入排序

public void insertSort(int a[]) {

int n = a.length;

for (int i = 1; i < n; i++) { //将a[i]插入a[0:i-1]

int t = a[i];

int j;

for (j = i - 1; j >= 0 && t < a[j]; j--) {

a[j + 1] = a[j];

}

a[j + 1] = t;

}

}

4、设置两层循环

for(int i=0;i

{

for(int j=i+1;j

{

if(arrayOfInts[i]>arrayOfInts[j])

{

a=arrayOfInts[i];

arrayOfInts[i]=arrayOfInts[j];

arrayOfInts[j]=a;

}

}

}

5、还有一种方法就是用Arrays.sort()方法:

//导入包

import java.util.Arrays;

public class Two3{

public static void main(String[]args)

{

int[]arrayOfInts={32,87,3,589,12,7076,2000,8,622,127};

Arrays.sort(arrayOfInts);

for(int i=0;i

{

System.out.print(arrayOfInts[i]+" ");

}

}

}

小示例:

Array stuInfo = Array.CrateInstance(typeOf(Student),5);

Array stuScore = Array.CreateInstance(typeOf(double),5);

Array.Sort(stuScore,stuInfo);

Array.Reverse(stuInfo);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值