publicclass insertSort { |
//基本思想:在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经是排 |
好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数 |
也是排好顺序的。如此反复循环,直到全部排好顺序。 |
public insertSort(){ |
int a[]={ 49 , 38 , 65 , 97 , 76 , 13 , 27 , 49 , 78 , 34 , 12 , 64 , 5 , 4 , 62 , 99 , 98 , 54 , 56 , 17 , 18 , 23 , 34 , 15 , 35 , 25 , 53 , 51 }; |
int temp= 0 ; |
for ( int i= 1 ;i<a.length;i++){ |
int j=i- 1 ; |
temp=a[i]; |
for (;j>= 0 &&temp<a[j];j--){ |
a[j+ 1 ]=a[j]; //将大于temp的值整体后移一个单位 |
} |
a[j+ 1 ]=temp; |
} |
for ( int i= 0 ;i<a.length;i++) |
System.out.println(a[i]); |
} |
} |
中级程序员
by: 低调的小飞 发表于:2017-09-10 10:14:39 顶(0) | 踩(0) 回复
所需辅助空间最多:归并排序
所需辅助空间最少:堆排序
平均速度最快:快速排序
不稳定:快速排序,希尔排序,堆排序。
回复评论