java四种数组排序

时间:2022-10-20 18:22:14

数组的四种排序

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

用法1.sort(byte[] a)

     对指定的 byte 型数组按数字升序进行排序。
  sort(byte[] a, int fromIndex, int toIndex)
  对指定 byte 型数组的指定范围按数字升序进行排序。
  sort(char[] a)
  对指定的 char 型数组按数字升序进行排序。
  sort(char[] a, int fromIndex, int toIndex)
  对指定 char 型数组的指定范围按数字升序进行排序。
  sort(double[] a)
  对指定的 double 型数组按数字升序进行排序。
  sort(double[] a, int fromIndex, int toIndex)
  对指定 double 型数组的指定范围按数字升序进行排序。
  sort(float[] a)
  对指定的 float 型数组按数字升序进行排序。
  sort(float[] a, int fromIndex, int toIndex)
  对指定 float 型数组的指定范围按数字升序进行排序。
  sort(int[] a)
  对指定的 int 型数组按数字升序进行排序。
  sort(int[] a, int fromIndex, int toIndex)
  2.sort(long[] a)
  对指定的 long 型数组按数字升序进行排序。
  sort(long[] a, int fromIndex, int toIndex)
  对指定 long 型数组的指定范围按数字升序进行排序。
  sort(Object[] a)
  根据元素的自然顺序,对指定对象数组按升序进行排序。
  sort(Object[] a, int fromIndex, int toIndex)
  根据元素的自然顺序,对指定对象数组的指定范围按升序进行排序。
  sort(short[] a)
  对指定的 short 型数组按数字升序进行排序。
  sort(short[] a, int fromIndex, int toIndex)
  对指定 short 型数组的指定范围按数字升序进行排序。
  sort(T[] a, Comparator<? super T> c)
  根据指定比较器产生的顺序对指定对象数组进行排序。
  sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
  根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。
 

2.冒泡排序法

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};
int temp=0;
for (int i = 0; i < a.length - 1; i++)
{
for (int j = 0; j < a.length - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}

3.选择排序

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=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]>a[j]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

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;
}
}