数据结构排序算法特别多,由于时间有限,随着复习深度的推进和算法的重要性,这里进行适当的归纳总结,在往后的时间里我会逐步完善和细化。算法皆由本人用Java独立编程实现,转载请注明出处,同时欢迎大神拍砖。
1.冒泡排序
1 package test; 2 /** 3 * 冒泡排序 4 * @author xuanxufeng 5 * 6 */ 7 public class BubbleSort { 8 public BubbleSort(){} 9 public void bubbleSort(int[] source){ 10 boolean exchange; 11 for(int i=0;i<source.length-1;i++){ 12 exchange = false; 13 for(int j=0;j<source.length-1-i;j++){ 14 if(source[j]>source[j+1]){ 15 swap(source,j,j+1); 16 exchange = true; 17 } 18 } 19 if(!exchange){ 20 return; 21 } 22 } 23 } 24 private void swap(int[] source,int x,int y){ 25 int temp = source[x]; 26 source[x]=source[y]; 27 source[y]=temp; 28 } 29 public static void main(String[] args){ 30 int[] a = {4,2,1,6,3,6,0,-5,1,1}; 31 BubbleSort b = new BubbleSort(); 32 b.bubbleSort(a); 33 for(int i=0;i<a.length;i++){ 34 System.out.print(a[i]+" "); 35 } 36 } 37 38 }
2.选择排序
1 package test; 2 /** 3 * 选择排序 4 * @author xuanxufeng 5 * 6 */ 7 public class SelectSort { 8 public void selectSort(int[] source){ 9 for(int i=0;i<source.length-1;i++){ 10 for(int j=i+1;j<source.length;j++){ 11 if(source[i]>source[j]){ 12 swap(source,i,j); 13 } 14 } 15 } 16 } 17 18 private void swap(int[] source,int x,int y){ 19 int temp = source[x]; 20 source[x]=source[y]; 21 source[y]=temp; 22 } 23 24 public static void main(String[] args) { 25 // TODO Auto-generated method stub 26 int[] a = {4,2,1,6,3,6,0,-5,1,1}; 27 SelectSort s = new SelectSort(); 28 s.selectSort(a); 29 for(int i=0;i<a.length;i++){ 30 System.out.print(a[i]+" "); 31 } 32 33 } 34 35 }
3.插入排序
package test; /** * 插入排序 * @author xuanxufeng * 本方法采用类似冒泡的方法,而《数据结构》教材中采用设置哨兵,比较和移动元素,最后复制哨兵的方式 */ public class InsertSort { public void insertSort(int[] source){ for(int i=1;i<source.length;i++){ /** * 初始状态排序完的序列只有一个元素, * 即source[0],故从1到source.length-1依次插入 */ for(int j=i;(j>0)&&(source[j]<source[j-1]);j--){// /** * 从下往上冒泡,因为插入的队列有序 * 只要找到合适的位置就可以结束操作了 * 另外,终止条件一定要是j>0在前, * 否则会报数组下标越界 */ swap(source,j,j-1); } } } private void swap(int[] source,int x,int y){ int temp = source[x]; source[x]=source[y]; source[y]=temp; } public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {4,2,1,6,3,6,0,-5,1,1}; InsertSort is = new InsertSort(); is.insertSort(a); for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } } }
4.Shell排序
1 package test; 2 /** 3 * 希尔排序 4 * @author xuanxufeng 5 * 类似于插入排序,不同之处在于有序部分其实是几个组混在一起 6 * 同组内部有序,组间无序,且插入时其实是同组间比较 7 */ 8 public class ShellSort { 9 10 public void shellSort(int[] source){ 11 int n = source.length; 12 for(int step=n/2;step>=1;step/=2){ 13 for(int i=step;i<n;i++){ 14 for(int j=i;(j-step>=0)&&(source[j]<source[j-step]);j-=step){ 15 swap(source,j,j-step); 16 } 17 } 18 } 19 } 20 21 private void swap(int[] source,int x,int y){ 22 int temp = source[x]; 23 source[x]=source[y]; 24 source[y]=temp; 25 } 26 27 public static void main(String[] args) { 28 // TODO Auto-generated method stub 29 int[] a = {4,2,1,6,3,6,0,-5,1,1}; 30 ShellSort shell = new ShellSort(); 31 shell.shellSort(a); 32 for(int i=0;i<a.length;i++){ 33 System.out.print(a[i]+" "); 34 } 35 } 36 37 }
5.二分排序
1 package test; 2 /** 3 * 二分排序或折半插入排序 4 * @author xuanxufeng 5 * 6 */ 7 public class BinaryInsertionSort { 8 public void binaryInsertSort(int[] source){ 9 for(int i=1;i<source.length;i++){ 10 int low = 0,high=i-1,mid; 11 int temp = source[i]; 12 /*算法执行的最终结果必然是high<low, 13 *而要插入的位置必然是high之后,low之前 14 * */ 15 while(low<=high){ 16 mid = (low+high)/2; 17 if(source[i]>source[mid]) low = mid+1; 18 else high= mid - 1; 19 } 20 /* 21 * 要插入第i个元素,必然要从第i-1个元素起向后移动, 22 * 一直到第high+1个元素,将第(high+1)个元素向后移动后, 23 * 将第i个元素插入到第(high+1)的位置 24 */ 25 for(int j=i-1;j>high;j--){ 26 source[j+1]=source[j]; 27 } 28 source[high+1] = temp; 29 } 30 } 31 32 public static void main(String[] args) { 33 // TODO Auto-generated method stub 34 int[] a = {4,2,1,6,3,6,0,-5,1,1}; 35 BinaryInsertionSort bisort = new BinaryInsertionSort(); 36 bisort.binaryInsertSort(a); 37 for(int i=0;i<a.length;i++){ 38 System.out.print(a[i]+" "); 39 } 40 } 41 42 }
6. 快速排序
1 package test; 2 /** 3 * 快速排序 4 * @author xuanxufeng 5 * 6 */ 7 public class QuickSort { 8 9 public void quickSort(int[] source,int low,int high){ 10 if(low<high){ 11 int pos = Partition(source,low,high); 12 quickSort(source,low,pos-1); 13 quickSort(source,pos+1,high); 14 } 15 } 16 17 private int Partition(int[] source, int low, int high) { 18 int temp = source[low]; 19 while (low < high) { 20 while (low < high && source[high] >= temp) high--; //一定要是大于等于,否则死循环!!! 21 source[low] = source[high]; 22 while (low < high && source[low] <= temp) low++; 23 source[high] = source[low]; 24 } 25 source[low] = temp; 26 return low; 27 } 28 public static void main(String[] args) { 29 // TODO Auto-generated method stub 30 int[] a = { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 }; 31 QuickSort qsort = new QuickSort(); 32 qsort.quickSort(a, 0, a.length - 1); 33 for (int i = 0; i < a.length; i++) { 34 System.out.print(a[i] + " "); 35 } 36 37 } 38 39 }
7.归并排序
1 package test; 2 /** 3 * 归并排序 4 * @author xuanxufeng 5 * 6 */ 7 8 public class MergeSort { 9 10 private int[] B; 11 private int[] A; 12 13 public MergeSort(int[] a){ 14 //1.直接传递引用,避免复制,节省空间和时间 15 //2.B作为类的成员变量,整个过程只创建一次,若放在函数中,则会严重浪费时间和空间 16 A = a; 17 B = new int[A.length]; 18 } 19 20 private void Merge(int low,int mid,int high){ 21 int i,j,k; 22 23 for(i=low;i<=high;i++) 24 B[i] = A[i]; 25 26 for(i=low,j=mid+1,k=i;i<=mid&&j<=high;){ 27 if(B[i]<B[j]) A[k++] = B[i++]; 28 else A[k++] = B[j++]; 29 } 30 31 while(i<=mid) A[k++]=B[i++]; 32 while(j<=high) A[k++]=B[j++]; 33 } 34 35 public void mergeSort(int low,int high){ 36 if(low<high){ //终止条件,当low=high时,mergeSort()什么都不做,一般这种情况对应到无法继续分割的情形,如mergeSort(3,3) 37 //这个时候什么都不做,接着执行Merge()操作 38 int mid = (low+high)/2; 39 mergeSort(low,mid); 40 mergeSort(mid+1,high); 41 Merge(low,mid,high); 42 } 43 } 44 45 public static void main(String[] args) { 46 // TODO Auto-generated method stub 47 int[] a = { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 }; 48 MergeSort msort = new MergeSort(a); 49 msort.mergeSort(0, a.length-1); 50 for (int i = 0; i < a.length; i++) { 51 System.out.print(a[i] + " "); 52 } 53 } 54 55 }
8. 堆排序
1 package test; 2 /** 3 * 此处和数据结构课本保持一致,下标从1开始,下标0用于寄存 4 * 如果是k个元素,则开辟K+1个空间 5 * @author xuanxufeng 6 * 7 */ 8 9 public class HeapSort { 10 public void buildMaxHeap(int[] source){ 11 int len = source.length-1; 12 for(int i=len/2;i>0;i--){ 13 adjustDown(source,i+1,source.length-1); 14 } 15 } 16 public void adjustDown(int[] source,int k,int len){ 17 int temp = source[k]; //先寄存一下第k个元素 18 for(int i = k*2;i<=len;i*=2){ 19 if(i<len){ 20 if(source[i]<source[i+1]) i++; 21 } 22 if(temp>=source[i]) break; 23 else{ 24 source[k] = source[i]; 25 k = i; 26 } 27 }//end for 28 source[k] = temp; 29 } 30 private void swap(int[] source,int x,int y){ 31 int temp = source[x]; 32 source[x]=source[y]; 33 source[y]=temp; 34 } 35 public void heapSort(int[] source){ 36 buildMaxHeap(source); 37 for(int i = source.length-1;i>1;i--){ 38 swap(source,1,i); 39 adjustDown(source,1,i-1); 40 } 41 } 42 43 public static void main(String[] args) { 44 // TODO Auto-generated method stub 45 int[] a = { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 }; 46 int[] b = new int[a.length+1]; 47 b[0] =0; 48 for(int i=1;i<=a.length;i++){ 49 b[i] = a[i-1]; 50 } 51 HeapSort h = new HeapSort(); 52 h.heapSort(b); 53 for(int i=1;i<b.length-1;i++){ 54 System.out.print(b[i]+" "); 55 } 56 System.out.println(); 57 } 58 59 }
9.其它
桶排序,基数排序以后有时间再说~