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);
}
}