排序動畫

import java.util.Arrays;
/**
*
*
<pre>
例如:int[] values = { 5, 2, 4, 1, 3 };
排序过程:
第1次:2,5,4,1,3
第2次:2,4,5,1,3
第3次:1,2,4,5,3
第4次:1,2,3,4,5
</pre>
* @author baoy
*
*/
public class InsertSort {
public static void main(String[] args) {
int[] a = { 5, 2, 4, 1, 3,5, 2, 4, 1, 3 };
sort(a);
System.out.println(Arrays.toString(a));
}
public static void sort(int [] a){
for (int i = 1; i < a.length; i++) {
int index = i;
for (int j = 0; j < i; j++) {
if (a[i] < a[j]) {
index = j;
break;
}
}
int temp = a[i];
for (int k = i; k >index; k--) {
a[k] = a[k-1];
}
a[index] = temp;
}
}
public static int[] swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
return a;
}
}
捐助开发者
在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/



谢谢您的赞助,我会做的更好!
2586

被折叠的 条评论
为什么被折叠?



