//冒泡排序,两两排,不断将大或小值向后移。public int[] bubbleSort(int[] old){ for(int i=0;i<old.length;i++) { for(int j=0;j<old.length-i-1;j++) { if(old[j]>old[j+1]) { int temp=old[j]; old[j]=old[j+1]; old[j+1]=temp; } } } return old;} //插入排序,不断将i前数列排出顺序,再将第i列插进去public int[] insertSort(int[] old){ for(int i=1;i<old.length;i++) { int current=old[i]; int j=i-1; while(j>=0&&old[j]>current) { old[j+1]=old[j]; j--; } old[j+1]=current; } return old;} //希尔排序,插入排序升级版,1.划块2.跳区3.反减public int[] shellSort(int[] a){ for(int incurrent=a.length/2;incurrent>0;incurrent/=2) for(int i=0;i<a.length;i+=incurrent) for(int j=i;j>0;j-=incurrent) if(a[j]<a[j-incurrent]) { int temp=a[j]; a[j]=a[j-incurrent]; a[j-incurrent]=temp; } return a;} //选择排序 即一次选出最大/最小放在最前面public int[] chooseSort(int[] old){ for(int i=0;i<old.length;i++) for(int j=i+1;j<old.length;j++) if(old[j]<old[i]) { int temp = old[j]; old[j] = old[i]; old[i] = temp; } return old;}
时间: 2024-10-03 16:08:55