冒泡:
点击(此处)折叠或打开
我们提供的服务有:网站制作、做网站、微信公众号开发、网站优化、网站认证、山阳ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的山阳网站制作公司
-
import java.util.Arrays;
-
class arrayDemo4
-
{
-
public static void main(String[] args)
-
{
-
int[] a={0,9,8,7,6,5,4,3,66,44,32,22};
-
int[] b=arraySort1(a);
-
System.out.println(Arrays.toString(b));
-
}
-
public static int[] arraySort1(int[] a){
-
for (int i=0; i
-
{
-
for (int j=0; j
-
{
-
if (a[j]>a[j+1])
-
{
-
int t=a[j];
-
a[j]=a[j+1];
-
a[j+1]=t;
-
}
-
}
-
}
-
return a;
-
}
-
}
选择:
-
import java.util.Arrays;
-
class arrayDemo3
-
{
-
public static void main(String[] args)
-
{
-
int[] a={6,4,6,2,1,5,7,2,3,6,7,3,1};
-
int[] b=arraySort(a);
-
System.out.println(Arrays.toString(b));
-
}
-
public static int[] arraySort(int[] a){
-
for (int i=0; i
-
{
-
for (int j=i+1; j
-
{
-
if (a[i]>a[j])
-
{
-
int t=a[i];
-
a[i]=a[j];
-
a[j]=t;
-
}
-
}
-
}
-
return a;
-
}
插入排序
-
import java.util.Arrays;
-
class arrayDemo6
-
{
-
public static void main(String[] args)
-
{
-
int[] a={12,45,135,2,464,9,454,21,12423};
-
System.out.println(Arrays.toString(insertSort1(a)));
-
}
-
public static int[] insertSort1(int[] a){
-
int t,j;
-
for (int i=1;i
-
{
-
t=a[i];
-
for (j=i-1;j>=0;j--)
-
{
-
if (t
-
{
-
a[j+1]=a[j];
-
}else
-
break;
-
}
-
a[j+1]=t;
-
}
-
return a;
-
}
-
}
优化后的插入排序:
-
import java.util.Arrays;
-
class arrayDemo7
-
{
-
public static void main(String[] args)
-
{
-
int[] a={12,45,135,2,464,9,454,21,12423};
-
System.out.println(Arrays.toString(insertSort1(a)));
-
}
-
public static int[] insertSort1(int[] a){
-
int t,j;
-
for (int i=1;i
-
{
-
t=a[i];
-
for (j=i-1;j>=0&&t
-
{
-
a[j+1]=a[j];
-
}
-
a[j+1]=t;
-
}
-
return a;
-
}
-
}
名称栏目:JAVA小练习冒泡排序,选择排序和插入排序
本文网址:
http://cdkjz.cn/article/iphiis.html