package com.study.sort;
import java.util.Arrays;
/**
* 性能:插入>选择>冒泡 O(n2)
*/
public class Linesort extends BaseSort{
public static void main(String[] args) {
int len=10;
int []array=genArray(len);
bubbleSort(array);
// int []array2= new int[]{12,15,9,20,6,31,24};
int []array2=genArray(len);
insertSort(array2);
// int []array3= new int[]{13,38,65,97,76,49,27};
int []array3=genArray(len);
selectSort(array3);
}
/**
*
* @param array
* 总结:冒泡排序性能并不好
* 每轮从第1和第2位开始比较两个数大小,如果前一位比后一位值大,则交换位置;
* 每一轮可以排出最后一位数;第1轮排出第n-1位数,第2轮排出第n-2位数
* 第i轮可以排出n-i下标数,下一轮则循环小于n-i即可
*
* ---bubbleSort
* 89 13 10 35 29 62 62 72 67 39
* =====================
* 13 10 35 29 62 62 72 67 39 89
* 10 13 29 35 62 62 67 39 72 89
* 10 13 29 35 62 62 39 67 72 89
* 10 13 29 35 62 39 62 67 72 89
* 10 13 29 35 39 62 62 67 72 89
* 10 13 29 35 39 62 62 67 72 89
* 10 13 29 35 39 62 62 67 72 89
* 10 13 29 35 39 62 62 67 72 89
* 10 13 29 35 39 62 62 67 72 89
* bubbleSortEnd==len:10============Cost:5
*/
public static void bubbleSort(int array[]){
long start=System.currentTimeMillis();
System.out.println("---bubbleSort");
p(array);
System.out.println("=====================");
if(array==null|| array.length==0){
return ;
}
int len=array.length;
for(int i=0;i<len-1;i++){
for (int j=1;j<len-i;j++){
if(array[j-1]>array[j]){
/* int tmp=array[j];
array[j]=array[j-1];
array[j-1]=tmp;*/
change(array,j-1,j);
}
}
p(array);
}
System.out.println("bubbleSortEnd==len:"+array.length+"============Cost:"+(System.currentTimeMillis()-start)+"\n\n");
}
/**
*
* @param array
* 插入排序:性能>选择>冒泡
* 总结:从第2位(下标1)开始到n位(下标n-1);
* 每轮先取出j位上的数记作posN,依次从j-1开始到0,取出array[j-1]的数据与posN比较,如果array[j-1]大于posN,那么a[j-1]向后移动一位;
* 此时a[j-1]数据移动到a[j]位置上,而posN就是从原来a[j]的数据,未来为posN会放到第1个小于posN的位置上;
*---insertSort
* 84 15 16 21 17 9 99 87 82 13
* =====================
* 15 84 16 21 17 9 99 87 82 13
* 15 16 84 21 17 9 99 87 82 13
* 15 16 21 84 17 9 99 87 82 13
* 15 16 17 21 84 9 99 87 82 13
* 9 15 16 17 21 84 99 87 82 13
* 9 15 16 17 21 84 99 87 82 13
* 9 15 16 17 21 84 87 99 82 13
* 9 15 16 17 21 82 84 87 99 13
* 9 13 15 16 17 21 82 84 87 99
* insertSort==len:10============Cost:8
*/
public static void insertSort(int array[]){
System.out.println("---insertSort");
long start=System.currentTimeMillis();
p(array);
System.out.println("=====================");
if(array==null|| array.length==0){
return ;
}
int len=array.length;
for(int i=0;i<len-1;i++){
int j=i+1;
int posN=array[j];
while(j>0&&array[j-1]>posN){
array[j]=array[j-1];//符合条件依次后移
j--;
}
if(j!=posN){//插入到位置上
array[j]=posN;
}
p(array);
}
System.out.println("insertSort==len:"+array.length+"============Cost:"+(System.currentTimeMillis()-start)+"\n\n");
}
/**
* 选择排序:
* 每轮i从下标1位开始到len-1;每次找到最小的数的下标pos,然后交换i和pos,每轮最多有1次交换
* ---selectSort
* 5 99 7 78 23 15 10 35 54 3
* =====================
* 3 99 7 78 23 15 10 35 54 5
* 3 5 7 78 23 15 10 35 54 99
* 3 5 7 78 23 15 10 35 54 99
* 3 5 7 10 23 15 78 35 54 99
* 3 5 7 10 15 23 78 35 54 99
* 3 5 7 10 15 23 78 35 54 99
* 3 5 7 10 15 23 35 78 54 99
* 3 5 7 10 15 23 35 54 78 99
* 3 5 7 10 15 23 35 54 78 99
* selectSort==len:10============Cost:6
*/
public static void selectSort(int array[]){
long start=System.currentTimeMillis();
System.out.println("---selectSort");
p(array);
System.out.println("=====================");
if(array==null|| array.length==0){
return ;
}
int len=array.length;
for(int i=0;i<len-1;i++){//外层循环到len-2位置,因为最后一位不用排,在之前的循环中已经排好了
int v=array[i];
int posN=i;
for(int j=i+1;j<len;j++){
if(array[j]<v){
v=array[j];
posN=j;
}
}
if(i!=posN){//选择到后交换
change(array,i,posN);
}
p(array);
}
System.out.println("selectSort==len:"+array.length+"============Cost:"+(System.currentTimeMillis()-start)+"\n\n");
}
public static void change(int array[],int i,int j){
int tmp=array[j];
array[j]=array[i];
array[i]=tmp;
}
}