首页 > 编程语言 >插入排序 和 希尔排序 java

插入排序 和 希尔排序 java

时间:2022-10-08 15:37:02浏览次数:87  
标签:sort java show int 插入排序 gap static 排序 void


​http://mp.weixin.qq.com/s/deUy_VPJ2m6BFbrEZp9DKg​​​
选择排序

/**
* Created by fupeng on 2017/1/19.
*/
public class insert

static void show(int [] a){
for(int i : a){
System.out.println(i);
}
}

static void exch(int [] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}

static void sort(int [] a){
for(int i=1; i< a.length; i++){


int the = a[i];
int j = i-1;
for(; j>=0 && the < a[j] ; j--){
a[j+1] = a[j];
}
a[j+1] = the;


}
}

public static void main(String[] args) {
int [] a = {2,3,4,1,5,9,8,6,7,0};
sort(a);
show(a);

}
}

希尔排序 shell sort, 希尔排序是 改良版的 插入排序

/**
* Created by fupeng on 2017/1/20.
*/
public class shell
static void show(int [] a){
for(int i : a){
System.out.println(i);
}
}

static void exch(int [] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}

static void sort(int [] a){

for(int gap = a.length/2;gap>0;gap /= 2 ) {

for (int i = gap; i < a.length; i=i+gap) {

int the = a[i];
int j = i - gap;
for (; j >= 0 && the < a[j]; j= j-gap) {
a[j + gap] = a[j];
}
a[j + gap] = the;

}
}
}

public static void main(String[] args) {
int [] a = {2,3,4,1,5,9,8,6,7,0};
sort(a);
show(a);

}
}


标签:sort,java,show,int,插入排序,gap,static,排序,void
From: https://blog.51cto.com/u_15815563/5738068

相关文章