快速排序参考:
http://blog.csdn.net/morewindows/article/details/6684558
希尔排序参考:
http://blog.csdn.net/yy_done/article/details/7102661
九种排序算法效率比较:
http://blog.csdn.net/cjf_iceking/article/details/7953637
public class TestSort {
public static void main(String[] args) {
int[] a={1,2,10,4,8,3};
bubbleSort(a); //冒泡排序
for(int i=0; i
System.out.print(” “+a[i]);
}
System.out.println();
int[] b={1,2,12,4,5,3};
selectSort(b); //选择排序
for(int i=0; i
System.out.print(” “+b[i]);
}
System.out.println();
int[] c={1,2,12,4,5,3};
insertSort(c); //选择排序
for(int i=0; i
System.out.print(” “+c[i]);
}
System.out.println();
int[] d={1,8,17,4,5,3};
shellSort(d); //希尔排序
for(int i=0; i
System.out.print(” “+d[i]);
}
System.out.println();
int[] e={1,8,17,4,6,3};
quickSort(e,0,e.length-1); //快速排序
for(int i=0; i
System.out.print(” “+e[i]);
}
System.out.println();
}
public static void bubbleSort(int[] source) { //冒泡排序,source[j]和source[j+1] 比较,大的往下沉
for(int i=0; i
for(int j=0; j
{
if(source[j] > source[j+1]) {
int temp = source[j];
source[j] = source[j+1];
source[j+1] = temp;
}
}
}
public static void selectSort(int[] source) { //选择排序,第一个和后面每一个比较,最小的放第一个,第二个和后面比较,依此类推
for(int i=0; i
for(int j=i+1; j
if(source[i]>source[j]) {
int temp = source[i];
source[i] = source[j];
source[j] = temp;
}
}
}
public static void insertSort(int[] source) {//插入排序,将数组分成有序+无序,我这个版本比较易于自己理解
for (int i=1; i
int j = i;
while(source[j]
int tmp = source[j];
source[j] = source[j-1];
source[j-1] =tmp;
j–;
if(j==0) break;//防止数组越界
}
}
}
public static void shellSort(int[] source) {//希尔排序,时间复杂度和增量序列有关系,了解即可,一种插入排序的改进,不太熟悉
int gap = source.length/2;
int temp;
while(gap>0) {
for(int i=gap; i
temp = source[i];
int j = i – gap;
while(j>=0 && temp < source[j]) {
source[j+gap] = source[j];
j = j – gap;
}
source[j+gap] = temp;
}
gap = gap/2;
}
}
public static void quickSort(int[] source, int start, int end) {//快速排序,测试下面效率最高
if(start
{
int i=start, j=end, x=source[start];
while(i
{
while(i=x)
j–;
if(i
source[i++]=source[j];
while(i
i++;
if(i
source[j–]=source[i];
}
source[i]=x;
quickSort(source, start,i-1);
quickSort(source,i+1,end);
}
}
}