1.冒泡排序
public static int[] bubbleSort1(int[] array){
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length-1; j++) {
if (array[j]>array[j+1]) {
array[j]=array[j]^array[j+1];
array[j+1]=array[j+1]^array[j];
array[j]=array[j]^array[j+1];
}
}
System.out.println(Arrays.toString(array));
}
return array;
}
2.选择排序
public static int[] selectionSort(int[] array){
int mindex = 0;
boolean isHave = false;
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[mindex]) {
mindex = j;
isHave = true;
}
}
if (isHave) {
array[i] = array[i]^array[mindex];
array[mindex] = array[mindex]^array[i];
array[i] = array[i]^array[mindex];
}if (i <= array.length-2) {
mindex = i + 1;
}
isHave = false;
System.out.println(Arrays.toString(array));
}
return array;
}
3.归并排序
public static void mergeSort(int[] array){
//前一个有序组合
ArrayList
//后一个有序组合
ArrayList
//两个有序组合合并的结果
ArrayList
for(int gap=1;gap
for(int i=0;i
if(list01.size()
list01.add(array[i]);
}else
if(list02.size()
list02.add(array[i]);
}
if( (list01.size()==gap && list02.size()==gap) || (list01.size()==gap && list02.size()
switchArray(list01,list02,list03);
list01.clear();
list02.clear();
}
}
for(int j=0;j
}
list01.clear();
list02.clear();
list03.clear();
System.out.println(Arrays.toString(array));
}
}
4.基数排序
public static void radixSort(int[] array) {
int index=0;
for(int i=0;i
if(length>index){
index=length;
}
}
System.out.println(index);
//也可通过先找到最大值,最后转成string获取长度的方法
/*List
List
List
for(int i=0;i<10;i++){
list.add(new ArrayList
}
//按照各个位数进行排序
for(int i = 0;i < index;i++){
for(int m=0;m
int a=array[m]/pow(i);
int b = a % 10;
list.get(b).add(array[m]);
}
System.out.println(list);
// 排序
int n=0;
for(ArrayList
if(arrayList.size() != 0){
for(Integer inner : arrayList){
array[n]=inner.intValue();
n++;
}
arrayList.clear();
}
}
System.out.println("--------"+Arrays.toString(array));
}
System.out.println(Arrays.toString(array));
}
4.堆排序
public static void switchArray(ArrayList
//System.out.println("list1");
int m=0;
int n=0;
while(m
while(m
list3.add(list1.get(m));
m++;
}
while(n
//System.out.println("n==="+n);
list3.add(list2.get(n));
n++;
}
}
while(m
m++;
}
while(n
n++;
}
}
6.快速排序
public static void quickfastSort(int[] array){
if(array.length > 0){
fastSort(array,0,array.length-1);
}
}
public static void fastSort(int[] array,int low ,int high ){
if(low < high){
int middle=findMiddle2(array,low,high);
fastSort(array, low, middle-1);
fastSort(array, middle+1, high);
}
}
public static int findMiddle(int[] array,int low ,int high){
int temp=array[low];
while(low
while(low
}
array[low]=array[high];
System.out.println("=--------"+Arrays.toString(array)+",low="+low+",high="+high);
while( low
low++;
}
array[high]=array[low];
System.out.println("=+++++++++"+Arrays.toString(array)+",low="+low+",high="+high);
}
array[low]=temp;
System.out.println(Arrays.toString(array)+",low="+low+",high="+high);
System.out.println("loc="+low);
return low;
}
public static int findMiddle2(int[] array,int low ,int high){
int temp=array[low];
while(low
while(temp
}
if(low
array[high]^=array[low];
array[low]^=array[high];
}
System.out.println("=--------"+Arrays.toString(array)+",low="+low+",high="+high);
while(temp>array[low]){
low++;
}
if(low
array[low]^=array[high];
array[high]^=array[low];
}
System.out.println("=+++++++++"+Arrays.toString(array)+",low="+low+",high="+high);
}
System.out.println(Arrays.toString(array)+",low="+low+",high="+high);
System.out.println("loc="+low);
return low;
}
7.插入排序
public static void insertSort(int[] array){
int j=0;
for(int i=1;i
int temp=array[i];
j=i-1;
while(j>=0 && temp
array[j+1]=array[j];
j--;
}
array[j+1] = temp;
System.out.println("---------"+Arrays.toString(array));
}
System.out.println(Arrays.toString(array));
}
8.希尔排序
public static void shellSort(int[] array){
int length=array.length;
int d=length/2;
while(d>=1){
int j=0;
for(int i=d; i
int temp=array[i];
j=i-d;
while(j>=0 && temp
array[j+d]=array[j];
j=j-d;
}
array[j+d] = temp;
System.out.println("---------"+Arrays.toString(array));
}
d=d/2;
}
}
}