排序中包含两种基本操作:
1 比较两个关键字的大小;
2 将记录从一个位置移动至另一个位置。(或其他类似的操作)
简单排序的时间复杂度为O(n^2),包括起泡排序,简单选择排序,直接插入排序等,当序列中的记录“基本有序”或n值较小时,简单排序是最佳的排序方法。以java语言描述的代码如下:
package sort;
import java.util.Arrays;
import java.util.Random;
public class SimpleSort {
public static void main(String[] args) {
int[] iarr = new int[10];
Random rand = new Random();
for (int i = 0; i < iarr.length; i++) {
iarr[i] = rand.nextInt(1000);
}
iarr = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
//iarr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
System.out.println(Arrays.toString(iarr));
// clone方法比Arrays.copyOf方法效率高
// iarr2 = Arrays.copyOf(iarr, iarr.length);
int cycletimes;
int swaptimes;
// 把较小的数置于数组前端
System.out.println("simple sort method 1,little first");
cycletimes = 0;
swaptimes = 0;
int[] iarr1 = iarr.clone();
for (int i = 0; i < iarr1.length; i++) {
cycletimes++;
System.out.println("----------------");
if(i+1<iarr1.length) {
cycletimes--;
for (int j = i + 1; j < iarr1.length; j++) {
cycletimes++;
if (iarr1[i] > iarr1[j]) {
int temp = iarr1[i];
iarr1[i] = iarr1[j];
swaptimes++;
System.out.println("inner swap;cycletimes="+cycletimes+";swaptimes=" + swaptimes + ";array:"
+ Arrays.toString(iarr1) + ";temp=" + temp);
iarr1[j] = temp;
swaptimes++;
System.out.println("inner swap;cycletimes="+cycletimes+";swaptimes=" + swaptimes + ";array:"
+ Arrays.toString(iarr1) + ";temp=" + temp);
}
}
}
}
System.out.println("method1,cycletimes=" + cycletimes + ",swaptimes="
+ swaptimes);
// 把较大的数置于数组末端
System.out.println("simple sort method 2,big first");
int[] iarr2 = iarr.clone();
cycletimes = 0;
swaptimes = 0;
for (int i = 0, k = iarr2.length; i < k; k--) {
cycletimes++;
System.out.println("----------------");
if(i+1<k) {
cycletimes--;
for (int j = i + 1; j < k; j++) {
cycletimes++;
if (iarr2[j - 1] > iarr2[j]) {
int temp = iarr2[j - 1];
iarr2[j - 1] = iarr2[j];
swaptimes++;
System.out.println("inner swap;cycletimes="+cycletimes+";swaptimes=" + swaptimes + ";array:"
+ Arrays.toString(iarr2) + ";temp=" + temp);
iarr2[j] = temp;
swaptimes++;
System.out.println("inner swap;cycletimes="+cycletimes+";swaptimes=" + swaptimes + ";array:"
+ Arrays.toString(iarr2) + ";temp=" + temp);
}
}
}
}
System.out.println("method2,cycletimes=" + cycletimes + ",swaptimes="
+ swaptimes);
// 前端始终保持有序数组,后续元素插入到有序数组中
System.out.println("simple sort method 3,straight insertion sort");
int[] iarr3 = iarr.clone();
cycletimes = 0;
swaptimes = 0;
for (int i = 1; i < iarr3.length; i++) {
cycletimes++;
System.out.println("----------------");
if (iarr3[i] < iarr3[i - 1]) {
int temp = iarr3[i];
iarr3[i] = iarr3[i - 1];
swaptimes++;
System.out.println("outter swap;cycletimes="+cycletimes+";swaptimes=" + swaptimes + ";array:"
+ Arrays.toString(iarr3) + ";temp=" + temp);
int j = i - 2;
if(j>=0) {
cycletimes--;
for (; j >= 0; j--) {
cycletimes++;
if (iarr3[j] > temp) {
iarr3[j + 1] = iarr3[j];
swaptimes++;
System.out.println("inner swap;cycletimes="+cycletimes+";swaptimes=" + swaptimes + ";array:"
+ Arrays.toString(iarr3) + ";temp=" + temp);
} else {
break;
}
}
}
iarr3[j + 1] = temp;
swaptimes++;
System.out.println("outter swap;cycletimes="+cycletimes+";swaptimes=" + swaptimes + ";array:"
+ Arrays.toString(iarr3) + ";temp=" + temp);
}
}
System.out.println("method3,cycletimes=" + cycletimes + ",swaptimes="
+ swaptimes);
}
}
输出如下:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
simple sort method 1,little first
----------------
inner swap;cycletimes=1;swaptimes=1;array:[8, 8, 7, 6, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=1;swaptimes=2;array:[8, 9, 7, 6, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=2;swaptimes=3;array:[7, 9, 7, 6, 5, 4, 3, 2, 1, 0];temp=8
inner swap;cycletimes=2;swaptimes=4;array:[7, 9, 8, 6, 5, 4, 3, 2, 1, 0];temp=8
inner swap;cycletimes=3;swaptimes=5;array:[6, 9, 8, 6, 5, 4, 3, 2, 1, 0];temp=7
inner swap;cycletimes=3;swaptimes=6;array:[6, 9, 8, 7, 5, 4, 3, 2, 1, 0];temp=7
inner swap;cycletimes=4;swaptimes=7;array:[5, 9, 8, 7, 5, 4, 3, 2, 1, 0];temp=6
inner swap;cycletimes=4;swaptimes=8;array:[5, 9, 8, 7, 6, 4, 3, 2, 1, 0];temp=6
inner swap;cycletimes=5;swaptimes=9;array:[4, 9, 8, 7, 6, 4, 3, 2, 1, 0];temp=5
inner swap;cycletimes=5;swaptimes=10;array:[4, 9, 8, 7, 6, 5, 3, 2, 1, 0];temp=5
inner swap;cycletimes=6;swaptimes=11;array:[3, 9, 8, 7, 6, 5, 3, 2, 1, 0];temp=4
inner swap;cycletimes=6;swaptimes=12;array:[3, 9, 8, 7, 6, 5, 4, 2, 1, 0];temp=4
inner swap;cycletimes=7;swaptimes=13;array:[2, 9, 8, 7, 6, 5, 4, 2, 1, 0];temp=3
inner swap;cycletimes=7;swaptimes=14;array:[2, 9, 8, 7, 6, 5, 4, 3, 1, 0];temp=3
inner swap;cycletimes=8;swaptimes=15;array:[1, 9, 8, 7, 6, 5, 4, 3, 1, 0];temp=2
inner swap;cycletimes=8;swaptimes=16;array:[1, 9, 8, 7, 6, 5, 4, 3, 2, 0];temp=2
inner swap;cycletimes=9;swaptimes=17;array:[0, 9, 8, 7, 6, 5, 4, 3, 2, 0];temp=1
inner swap;cycletimes=9;swaptimes=18;array:[0, 9, 8, 7, 6, 5, 4, 3, 2, 1];temp=1
----------------
inner swap;cycletimes=10;swaptimes=19;array:[0, 8, 8, 7, 6, 5, 4, 3, 2, 1];temp=9
inner swap;cycletimes=10;swaptimes=20;array:[0, 8, 9, 7, 6, 5, 4, 3, 2, 1];temp=9
inner swap;cycletimes=11;swaptimes=21;array:[0, 7, 9, 7, 6, 5, 4, 3, 2, 1];temp=8
inner swap;cycletimes=11;swaptimes=22;array:[0, 7, 9, 8, 6, 5, 4, 3, 2, 1];temp=8
inner swap;cycletimes=12;swaptimes=23;array:[0, 6, 9, 8, 6, 5, 4, 3, 2, 1];temp=7
inner swap;cycletimes=12;swaptimes=24;array:[0, 6, 9, 8, 7, 5, 4, 3, 2, 1];temp=7
inner swap;cycletimes=13;swaptimes=25;array:[0, 5, 9, 8, 7, 5, 4, 3, 2, 1];temp=6
inner swap;cycletimes=13;swaptimes=26;array:[0, 5, 9, 8, 7, 6, 4, 3, 2, 1];temp=6
inner swap;cycletimes=14;swaptimes=27;array:[0, 4, 9, 8, 7, 6, 4, 3, 2, 1];temp=5
inner swap;cycletimes=14;swaptimes=28;array:[0, 4, 9, 8, 7, 6, 5, 3, 2, 1];temp=5
inner swap;cycletimes=15;swaptimes=29;array:[0, 3, 9, 8, 7, 6, 5, 3, 2, 1];temp=4
inner swap;cycletimes=15;swaptimes=30;array:[0, 3, 9, 8, 7, 6, 5, 4, 2, 1];temp=4
inner swap;cycletimes=16;swaptimes=31;array:[0, 2, 9, 8, 7, 6, 5, 4, 2, 1];temp=3
inner swap;cycletimes=16;swaptimes=32;array:[0, 2, 9, 8, 7, 6, 5, 4, 3, 1];temp=3
inner swap;cycletimes=17;swaptimes=33;array:[0, 1, 9, 8, 7, 6, 5, 4, 3, 1];temp=2
inner swap;cycletimes=17;swaptimes=34;array:[0, 1, 9, 8, 7, 6, 5, 4, 3, 2];temp=2
----------------
inner swap;cycletimes=18;swaptimes=35;array:[0, 1, 8, 8, 7, 6, 5, 4, 3, 2];temp=9
inner swap;cycletimes=18;swaptimes=36;array:[0, 1, 8, 9, 7, 6, 5, 4, 3, 2];temp=9
inner swap;cycletimes=19;swaptimes=37;array:[0, 1, 7, 9, 7, 6, 5, 4, 3, 2];temp=8
inner swap;cycletimes=19;swaptimes=38;array:[0, 1, 7, 9, 8, 6, 5, 4, 3, 2];temp=8
inner swap;cycletimes=20;swaptimes=39;array:[0, 1, 6, 9, 8, 6, 5, 4, 3, 2];temp=7
inner swap;cycletimes=20;swaptimes=40;array:[0, 1, 6, 9, 8, 7, 5, 4, 3, 2];temp=7
inner swap;cycletimes=21;swaptimes=41;array:[0, 1, 5, 9, 8, 7, 5, 4, 3, 2];temp=6
inner swap;cycletimes=21;swaptimes=42;array:[0, 1, 5, 9, 8, 7, 6, 4, 3, 2];temp=6
inner swap;cycletimes=22;swaptimes=43;array:[0, 1, 4, 9, 8, 7, 6, 4, 3, 2];temp=5
inner swap;cycletimes=22;swaptimes=44;array:[0, 1, 4, 9, 8, 7, 6, 5, 3, 2];temp=5
inner swap;cycletimes=23;swaptimes=45;array:[0, 1, 3, 9, 8, 7, 6, 5, 3, 2];temp=4
inner swap;cycletimes=23;swaptimes=46;array:[0, 1, 3, 9, 8, 7, 6, 5, 4, 2];temp=4
inner swap;cycletimes=24;swaptimes=47;array:[0, 1, 2, 9, 8, 7, 6, 5, 4, 2];temp=3
inner swap;cycletimes=24;swaptimes=48;array:[0, 1, 2, 9, 8, 7, 6, 5, 4, 3];temp=3
----------------
inner swap;cycletimes=25;swaptimes=49;array:[0, 1, 2, 8, 8, 7, 6, 5, 4, 3];temp=9
inner swap;cycletimes=25;swaptimes=50;array:[0, 1, 2, 8, 9, 7, 6, 5, 4, 3];temp=9
inner swap;cycletimes=26;swaptimes=51;array:[0, 1, 2, 7, 9, 7, 6, 5, 4, 3];temp=8
inner swap;cycletimes=26;swaptimes=52;array:[0, 1, 2, 7, 9, 8, 6, 5, 4, 3];temp=8
inner swap;cycletimes=27;swaptimes=53;array:[0, 1, 2, 6, 9, 8, 6, 5, 4, 3];temp=7
inner swap;cycletimes=27;swaptimes=54;array:[0, 1, 2, 6, 9, 8, 7, 5, 4, 3];temp=7
inner swap;cycletimes=28;swaptimes=55;array:[0, 1, 2, 5, 9, 8, 7, 5, 4, 3];temp=6
inner swap;cycletimes=28;swaptimes=56;array:[0, 1, 2, 5, 9, 8, 7, 6, 4, 3];temp=6
inner swap;cycletimes=29;swaptimes=57;array:[0, 1, 2, 4, 9, 8, 7, 6, 4, 3];temp=5
inner swap;cycletimes=29;swaptimes=58;array:[0, 1, 2, 4, 9, 8, 7, 6, 5, 3];temp=5
inner swap;cycletimes=30;swaptimes=59;array:[0, 1, 2, 3, 9, 8, 7, 6, 5, 3];temp=4
inner swap;cycletimes=30;swaptimes=60;array:[0, 1, 2, 3, 9, 8, 7, 6, 5, 4];temp=4
----------------
inner swap;cycletimes=31;swaptimes=61;array:[0, 1, 2, 3, 8, 8, 7, 6, 5, 4];temp=9
inner swap;cycletimes=31;swaptimes=62;array:[0, 1, 2, 3, 8, 9, 7, 6, 5, 4];temp=9
inner swap;cycletimes=32;swaptimes=63;array:[0, 1, 2, 3, 7, 9, 7, 6, 5, 4];temp=8
inner swap;cycletimes=32;swaptimes=64;array:[0, 1, 2, 3, 7, 9, 8, 6, 5, 4];temp=8
inner swap;cycletimes=33;swaptimes=65;array:[0, 1, 2, 3, 6, 9, 8, 6, 5, 4];temp=7
inner swap;cycletimes=33;swaptimes=66;array:[0, 1, 2, 3, 6, 9, 8, 7, 5, 4];temp=7
inner swap;cycletimes=34;swaptimes=67;array:[0, 1, 2, 3, 5, 9, 8, 7, 5, 4];temp=6
inner swap;cycletimes=34;swaptimes=68;array:[0, 1, 2, 3, 5, 9, 8, 7, 6, 4];temp=6
inner swap;cycletimes=35;swaptimes=69;array:[0, 1, 2, 3, 4, 9, 8, 7, 6, 4];temp=5
inner swap;cycletimes=35;swaptimes=70;array:[0, 1, 2, 3, 4, 9, 8, 7, 6, 5];temp=5
----------------
inner swap;cycletimes=36;swaptimes=71;array:[0, 1, 2, 3, 4, 8, 8, 7, 6, 5];temp=9
inner swap;cycletimes=36;swaptimes=72;array:[0, 1, 2, 3, 4, 8, 9, 7, 6, 5];temp=9
inner swap;cycletimes=37;swaptimes=73;array:[0, 1, 2, 3, 4, 7, 9, 7, 6, 5];temp=8
inner swap;cycletimes=37;swaptimes=74;array:[0, 1, 2, 3, 4, 7, 9, 8, 6, 5];temp=8
inner swap;cycletimes=38;swaptimes=75;array:[0, 1, 2, 3, 4, 6, 9, 8, 6, 5];temp=7
inner swap;cycletimes=38;swaptimes=76;array:[0, 1, 2, 3, 4, 6, 9, 8, 7, 5];temp=7
inner swap;cycletimes=39;swaptimes=77;array:[0, 1, 2, 3, 4, 5, 9, 8, 7, 5];temp=6
inner swap;cycletimes=39;swaptimes=78;array:[0, 1, 2, 3, 4, 5, 9, 8, 7, 6];temp=6
----------------
inner swap;cycletimes=40;swaptimes=79;array:[0, 1, 2, 3, 4, 5, 8, 8, 7, 6];temp=9
inner swap;cycletimes=40;swaptimes=80;array:[0, 1, 2, 3, 4, 5, 8, 9, 7, 6];temp=9
inner swap;cycletimes=41;swaptimes=81;array:[0, 1, 2, 3, 4, 5, 7, 9, 7, 6];temp=8
inner swap;cycletimes=41;swaptimes=82;array:[0, 1, 2, 3, 4, 5, 7, 9, 8, 6];temp=8
inner swap;cycletimes=42;swaptimes=83;array:[0, 1, 2, 3, 4, 5, 6, 9, 8, 6];temp=7
inner swap;cycletimes=42;swaptimes=84;array:[0, 1, 2, 3, 4, 5, 6, 9, 8, 7];temp=7
----------------
inner swap;cycletimes=43;swaptimes=85;array:[0, 1, 2, 3, 4, 5, 6, 8, 8, 7];temp=9
inner swap;cycletimes=43;swaptimes=86;array:[0, 1, 2, 3, 4, 5, 6, 8, 9, 7];temp=9
inner swap;cycletimes=44;swaptimes=87;array:[0, 1, 2, 3, 4, 5, 6, 7, 9, 7];temp=8
inner swap;cycletimes=44;swaptimes=88;array:[0, 1, 2, 3, 4, 5, 6, 7, 9, 8];temp=8
----------------
inner swap;cycletimes=45;swaptimes=89;array:[0, 1, 2, 3, 4, 5, 6, 7, 8, 8];temp=9
inner swap;cycletimes=45;swaptimes=90;array:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];temp=9
----------------
method1,cycletimes=46,swaptimes=90
simple sort method 2,big first
----------------
inner swap;cycletimes=1;swaptimes=1;array:[8, 8, 7, 6, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=1;swaptimes=2;array:[8, 9, 7, 6, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=2;swaptimes=3;array:[8, 7, 7, 6, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=2;swaptimes=4;array:[8, 7, 9, 6, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=3;swaptimes=5;array:[8, 7, 6, 6, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=3;swaptimes=6;array:[8, 7, 6, 9, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=4;swaptimes=7;array:[8, 7, 6, 5, 5, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=4;swaptimes=8;array:[8, 7, 6, 5, 9, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=5;swaptimes=9;array:[8, 7, 6, 5, 4, 4, 3, 2, 1, 0];temp=9
inner swap;cycletimes=5;swaptimes=10;array:[8, 7, 6, 5, 4, 9, 3, 2, 1, 0];temp=9
inner swap;cycletimes=6;swaptimes=11;array:[8, 7, 6, 5, 4, 3, 3, 2, 1, 0];temp=9
inner swap;cycletimes=6;swaptimes=12;array:[8, 7, 6, 5, 4, 3, 9, 2, 1, 0];temp=9
inner swap;cycletimes=7;swaptimes=13;array:[8, 7, 6, 5, 4, 3, 2, 2, 1, 0];temp=9
inner swap;cycletimes=7;swaptimes=14;array:[8, 7, 6, 5, 4, 3, 2, 9, 1, 0];temp=9
inner swap;cycletimes=8;swaptimes=15;array:[8, 7, 6, 5, 4, 3, 2, 1, 1, 0];temp=9
inner swap;cycletimes=8;swaptimes=16;array:[8, 7, 6, 5, 4, 3, 2, 1, 9, 0];temp=9
inner swap;cycletimes=9;swaptimes=17;array:[8, 7, 6, 5, 4, 3, 2, 1, 0, 0];temp=9
inner swap;cycletimes=9;swaptimes=18;array:[8, 7, 6, 5, 4, 3, 2, 1, 0, 9];temp=9
----------------
inner swap;cycletimes=10;swaptimes=19;array:[7, 7, 6, 5, 4, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=10;swaptimes=20;array:[7, 8, 6, 5, 4, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=11;swaptimes=21;array:[7, 6, 6, 5, 4, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=11;swaptimes=22;array:[7, 6, 8, 5, 4, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=12;swaptimes=23;array:[7, 6, 5, 5, 4, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=12;swaptimes=24;array:[7, 6, 5, 8, 4, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=13;swaptimes=25;array:[7, 6, 5, 4, 4, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=13;swaptimes=26;array:[7, 6, 5, 4, 8, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=14;swaptimes=27;array:[7, 6, 5, 4, 3, 3, 2, 1, 0, 9];temp=8
inner swap;cycletimes=14;swaptimes=28;array:[7, 6, 5, 4, 3, 8, 2, 1, 0, 9];temp=8
inner swap;cycletimes=15;swaptimes=29;array:[7, 6, 5, 4, 3, 2, 2, 1, 0, 9];temp=8
inner swap;cycletimes=15;swaptimes=30;array:[7, 6, 5, 4, 3, 2, 8, 1, 0, 9];temp=8
inner swap;cycletimes=16;swaptimes=31;array:[7, 6, 5, 4, 3, 2, 1, 1, 0, 9];temp=8
inner swap;cycletimes=16;swaptimes=32;array:[7, 6, 5, 4, 3, 2, 1, 8, 0, 9];temp=8
inner swap;cycletimes=17;swaptimes=33;array:[7, 6, 5, 4, 3, 2, 1, 0, 0, 9];temp=8
inner swap;cycletimes=17;swaptimes=34;array:[7, 6, 5, 4, 3, 2, 1, 0, 8, 9];temp=8
----------------
inner swap;cycletimes=18;swaptimes=35;array:[6, 6, 5, 4, 3, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=18;swaptimes=36;array:[6, 7, 5, 4, 3, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=19;swaptimes=37;array:[6, 5, 5, 4, 3, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=19;swaptimes=38;array:[6, 5, 7, 4, 3, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=20;swaptimes=39;array:[6, 5, 4, 4, 3, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=20;swaptimes=40;array:[6, 5, 4, 7, 3, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=21;swaptimes=41;array:[6, 5, 4, 3, 3, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=21;swaptimes=42;array:[6, 5, 4, 3, 7, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=22;swaptimes=43;array:[6, 5, 4, 3, 2, 2, 1, 0, 8, 9];temp=7
inner swap;cycletimes=22;swaptimes=44;array:[6, 5, 4, 3, 2, 7, 1, 0, 8, 9];temp=7
inner swap;cycletimes=23;swaptimes=45;array:[6, 5, 4, 3, 2, 1, 1, 0, 8, 9];temp=7
inner swap;cycletimes=23;swaptimes=46;array:[6, 5, 4, 3, 2, 1, 7, 0, 8, 9];temp=7
inner swap;cycletimes=24;swaptimes=47;array:[6, 5, 4, 3, 2, 1, 0, 0, 8, 9];temp=7
inner swap;cycletimes=24;swaptimes=48;array:[6, 5, 4, 3, 2, 1, 0, 7, 8, 9];temp=7
----------------
inner swap;cycletimes=25;swaptimes=49;array:[5, 5, 4, 3, 2, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=25;swaptimes=50;array:[5, 6, 4, 3, 2, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=26;swaptimes=51;array:[5, 4, 4, 3, 2, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=26;swaptimes=52;array:[5, 4, 6, 3, 2, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=27;swaptimes=53;array:[5, 4, 3, 3, 2, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=27;swaptimes=54;array:[5, 4, 3, 6, 2, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=28;swaptimes=55;array:[5, 4, 3, 2, 2, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=28;swaptimes=56;array:[5, 4, 3, 2, 6, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=29;swaptimes=57;array:[5, 4, 3, 2, 1, 1, 0, 7, 8, 9];temp=6
inner swap;cycletimes=29;swaptimes=58;array:[5, 4, 3, 2, 1, 6, 0, 7, 8, 9];temp=6
inner swap;cycletimes=30;swaptimes=59;array:[5, 4, 3, 2, 1, 0, 0, 7, 8, 9];temp=6
inner swap;cycletimes=30;swaptimes=60;array:[5, 4, 3, 2, 1, 0, 6, 7, 8, 9];temp=6
----------------
inner swap;cycletimes=31;swaptimes=61;array:[4, 4, 3, 2, 1, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=31;swaptimes=62;array:[4, 5, 3, 2, 1, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=32;swaptimes=63;array:[4, 3, 3, 2, 1, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=32;swaptimes=64;array:[4, 3, 5, 2, 1, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=33;swaptimes=65;array:[4, 3, 2, 2, 1, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=33;swaptimes=66;array:[4, 3, 2, 5, 1, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=34;swaptimes=67;array:[4, 3, 2, 1, 1, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=34;swaptimes=68;array:[4, 3, 2, 1, 5, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=35;swaptimes=69;array:[4, 3, 2, 1, 0, 0, 6, 7, 8, 9];temp=5
inner swap;cycletimes=35;swaptimes=70;array:[4, 3, 2, 1, 0, 5, 6, 7, 8, 9];temp=5
----------------
inner swap;cycletimes=36;swaptimes=71;array:[3, 3, 2, 1, 0, 5, 6, 7, 8, 9];temp=4
inner swap;cycletimes=36;swaptimes=72;array:[3, 4, 2, 1, 0, 5, 6, 7, 8, 9];temp=4
inner swap;cycletimes=37;swaptimes=73;array:[3, 2, 2, 1, 0, 5, 6, 7, 8, 9];temp=4
inner swap;cycletimes=37;swaptimes=74;array:[3, 2, 4, 1, 0, 5, 6, 7, 8, 9];temp=4
inner swap;cycletimes=38;swaptimes=75;array:[3, 2, 1, 1, 0, 5, 6, 7, 8, 9];temp=4
inner swap;cycletimes=38;swaptimes=76;array:[3, 2, 1, 4, 0, 5, 6, 7, 8, 9];temp=4
inner swap;cycletimes=39;swaptimes=77;array:[3, 2, 1, 0, 0, 5, 6, 7, 8, 9];temp=4
inner swap;cycletimes=39;swaptimes=78;array:[3, 2, 1, 0, 4, 5, 6, 7, 8, 9];temp=4
----------------
inner swap;cycletimes=40;swaptimes=79;array:[2, 2, 1, 0, 4, 5, 6, 7, 8, 9];temp=3
inner swap;cycletimes=40;swaptimes=80;array:[2, 3, 1, 0, 4, 5, 6, 7, 8, 9];temp=3
inner swap;cycletimes=41;swaptimes=81;array:[2, 1, 1, 0, 4, 5, 6, 7, 8, 9];temp=3
inner swap;cycletimes=41;swaptimes=82;array:[2, 1, 3, 0, 4, 5, 6, 7, 8, 9];temp=3
inner swap;cycletimes=42;swaptimes=83;array:[2, 1, 0, 0, 4, 5, 6, 7, 8, 9];temp=3
inner swap;cycletimes=42;swaptimes=84;array:[2, 1, 0, 3, 4, 5, 6, 7, 8, 9];temp=3
----------------
inner swap;cycletimes=43;swaptimes=85;array:[1, 1, 0, 3, 4, 5, 6, 7, 8, 9];temp=2
inner swap;cycletimes=43;swaptimes=86;array:[1, 2, 0, 3, 4, 5, 6, 7, 8, 9];temp=2
inner swap;cycletimes=44;swaptimes=87;array:[1, 0, 0, 3, 4, 5, 6, 7, 8, 9];temp=2
inner swap;cycletimes=44;swaptimes=88;array:[1, 0, 2, 3, 4, 5, 6, 7, 8, 9];temp=2
----------------
inner swap;cycletimes=45;swaptimes=89;array:[0, 0, 2, 3, 4, 5, 6, 7, 8, 9];temp=1
inner swap;cycletimes=45;swaptimes=90;array:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];temp=1
----------------
method2,cycletimes=46,swaptimes=90
simple sort method 3,straight insertion sort
----------------
outter swap;cycletimes=1;swaptimes=1;array:[9, 9, 7, 6, 5, 4, 3, 2, 1, 0];temp=8
outter swap;cycletimes=1;swaptimes=2;array:[8, 9, 7, 6, 5, 4, 3, 2, 1, 0];temp=8
----------------
outter swap;cycletimes=2;swaptimes=3;array:[8, 9, 9, 6, 5, 4, 3, 2, 1, 0];temp=7
inner swap;cycletimes=2;swaptimes=4;array:[8, 8, 9, 6, 5, 4, 3, 2, 1, 0];temp=7
outter swap;cycletimes=2;swaptimes=5;array:[7, 8, 9, 6, 5, 4, 3, 2, 1, 0];temp=7
----------------
outter swap;cycletimes=3;swaptimes=6;array:[7, 8, 9, 9, 5, 4, 3, 2, 1, 0];temp=6
inner swap;cycletimes=3;swaptimes=7;array:[7, 8, 8, 9, 5, 4, 3, 2, 1, 0];temp=6
inner swap;cycletimes=4;swaptimes=8;array:[7, 7, 8, 9, 5, 4, 3, 2, 1, 0];temp=6
outter swap;cycletimes=4;swaptimes=9;array:[6, 7, 8, 9, 5, 4, 3, 2, 1, 0];temp=6
----------------
outter swap;cycletimes=5;swaptimes=10;array:[6, 7, 8, 9, 9, 4, 3, 2, 1, 0];temp=5
inner swap;cycletimes=5;swaptimes=11;array:[6, 7, 8, 8, 9, 4, 3, 2, 1, 0];temp=5
inner swap;cycletimes=6;swaptimes=12;array:[6, 7, 7, 8, 9, 4, 3, 2, 1, 0];temp=5
inner swap;cycletimes=7;swaptimes=13;array:[6, 6, 7, 8, 9, 4, 3, 2, 1, 0];temp=5
outter swap;cycletimes=7;swaptimes=14;array:[5, 6, 7, 8, 9, 4, 3, 2, 1, 0];temp=5
----------------
outter swap;cycletimes=8;swaptimes=15;array:[5, 6, 7, 8, 9, 9, 3, 2, 1, 0];temp=4
inner swap;cycletimes=8;swaptimes=16;array:[5, 6, 7, 8, 8, 9, 3, 2, 1, 0];temp=4
inner swap;cycletimes=9;swaptimes=17;array:[5, 6, 7, 7, 8, 9, 3, 2, 1, 0];temp=4
inner swap;cycletimes=10;swaptimes=18;array:[5, 6, 6, 7, 8, 9, 3, 2, 1, 0];temp=4
inner swap;cycletimes=11;swaptimes=19;array:[5, 5, 6, 7, 8, 9, 3, 2, 1, 0];temp=4
outter swap;cycletimes=11;swaptimes=20;array:[4, 5, 6, 7, 8, 9, 3, 2, 1, 0];temp=4
----------------
outter swap;cycletimes=12;swaptimes=21;array:[4, 5, 6, 7, 8, 9, 9, 2, 1, 0];temp=3
inner swap;cycletimes=12;swaptimes=22;array:[4, 5, 6, 7, 8, 8, 9, 2, 1, 0];temp=3
inner swap;cycletimes=13;swaptimes=23;array:[4, 5, 6, 7, 7, 8, 9, 2, 1, 0];temp=3
inner swap;cycletimes=14;swaptimes=24;array:[4, 5, 6, 6, 7, 8, 9, 2, 1, 0];temp=3
inner swap;cycletimes=15;swaptimes=25;array:[4, 5, 5, 6, 7, 8, 9, 2, 1, 0];temp=3
inner swap;cycletimes=16;swaptimes=26;array:[4, 4, 5, 6, 7, 8, 9, 2, 1, 0];temp=3
outter swap;cycletimes=16;swaptimes=27;array:[3, 4, 5, 6, 7, 8, 9, 2, 1, 0];temp=3
----------------
outter swap;cycletimes=17;swaptimes=28;array:[3, 4, 5, 6, 7, 8, 9, 9, 1, 0];temp=2
inner swap;cycletimes=17;swaptimes=29;array:[3, 4, 5, 6, 7, 8, 8, 9, 1, 0];temp=2
inner swap;cycletimes=18;swaptimes=30;array:[3, 4, 5, 6, 7, 7, 8, 9, 1, 0];temp=2
inner swap;cycletimes=19;swaptimes=31;array:[3, 4, 5, 6, 6, 7, 8, 9, 1, 0];temp=2
inner swap;cycletimes=20;swaptimes=32;array:[3, 4, 5, 5, 6, 7, 8, 9, 1, 0];temp=2
inner swap;cycletimes=21;swaptimes=33;array:[3, 4, 4, 5, 6, 7, 8, 9, 1, 0];temp=2
inner swap;cycletimes=22;swaptimes=34;array:[3, 3, 4, 5, 6, 7, 8, 9, 1, 0];temp=2
outter swap;cycletimes=22;swaptimes=35;array:[2, 3, 4, 5, 6, 7, 8, 9, 1, 0];temp=2
----------------
outter swap;cycletimes=23;swaptimes=36;array:[2, 3, 4, 5, 6, 7, 8, 9, 9, 0];temp=1
inner swap;cycletimes=23;swaptimes=37;array:[2, 3, 4, 5, 6, 7, 8, 8, 9, 0];temp=1
inner swap;cycletimes=24;swaptimes=38;array:[2, 3, 4, 5, 6, 7, 7, 8, 9, 0];temp=1
inner swap;cycletimes=25;swaptimes=39;array:[2, 3, 4, 5, 6, 6, 7, 8, 9, 0];temp=1
inner swap;cycletimes=26;swaptimes=40;array:[2, 3, 4, 5, 5, 6, 7, 8, 9, 0];temp=1
inner swap;cycletimes=27;swaptimes=41;array:[2, 3, 4, 4, 5, 6, 7, 8, 9, 0];temp=1
inner swap;cycletimes=28;swaptimes=42;array:[2, 3, 3, 4, 5, 6, 7, 8, 9, 0];temp=1
inner swap;cycletimes=29;swaptimes=43;array:[2, 2, 3, 4, 5, 6, 7, 8, 9, 0];temp=1
outter swap;cycletimes=29;swaptimes=44;array:[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];temp=1
----------------
outter swap;cycletimes=30;swaptimes=45;array:[1, 2, 3, 4, 5, 6, 7, 8, 9, 9];temp=0
inner swap;cycletimes=30;swaptimes=46;array:[1, 2, 3, 4, 5, 6, 7, 8, 8, 9];temp=0
inner swap;cycletimes=31;swaptimes=47;array:[1, 2, 3, 4, 5, 6, 7, 7, 8, 9];temp=0
inner swap;cycletimes=32;swaptimes=48;array:[1, 2, 3, 4, 5, 6, 6, 7, 8, 9];temp=0
inner swap;cycletimes=33;swaptimes=49;array:[1, 2, 3, 4, 5, 5, 6, 7, 8, 9];temp=0
inner swap;cycletimes=34;swaptimes=50;array:[1, 2, 3, 4, 4, 5, 6, 7, 8, 9];temp=0
inner swap;cycletimes=35;swaptimes=51;array:[1, 2, 3, 3, 4, 5, 6, 7, 8, 9];temp=0
inner swap;cycletimes=36;swaptimes=52;array:[1, 2, 2, 3, 4, 5, 6, 7, 8, 9];temp=0
inner swap;cycletimes=37;swaptimes=53;array:[1, 1, 2, 3, 4, 5, 6, 7, 8, 9];temp=0
outter swap;cycletimes=37;swaptimes=54;array:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];temp=0
method3,cycletimes=37,swaptimes=54
可以看出,起泡排序法和简单选择排序法思想一致,都需要n(n-1)/2次循环;直接插入排序循环次数最大为(n-1)(n-2)/2+1。