标签:
这个demo代码是去年写的。结构不好,测试代码和8个排序算法的function全部写在一起了。那几句测试用的代码其实写一遍就可以了,但去年的我写了8次。我的天。完全没有java该有的味道。懒于再重写了,这里重点是总结排序算法。实现代码什么的能看就行。
package day07; import java.util.Arrays; import java.util.Random; public class SortDemo { public static void main(String[] args) { Random ran = new Random(); int[] arr = new int[10]; for(int i = 0; i < arr.length; i++) { arr[i] = ran.nextInt(1000); } System.out.println(Arrays.toString(arr)); System.out.println("直接插入排序:"); InsertSort(arr,10); System.out.println(Arrays.toString(arr)); System.out.println("**********************************"); for(int i = 0; i < arr.length; i++) { arr[i] = ran.nextInt(1000); } System.out.println(Arrays.toString(arr)); System.out.println("二分排序:"); InsertSort2(arr,10); System.out.println(Arrays.toString(arr)); System.out.println("**********************************"); for(int i = 0; i < arr.length; i++) { arr[i] = ran.nextInt(1000); } System.out.println(Arrays.toString(arr)); System.out.println("希尔排序:"); ShellSort(arr,10); System.out.println(Arrays.toString(arr)); System.out.println("**********************************"); for(int i = 0; i < arr.length; i++) { arr[i] = ran.nextInt(1000); } System.out.println(Arrays.toString(arr)); System.out.println("简单选择排序:"); SelectSort(arr, 10); System.out.println(Arrays.toString(arr)); System.out.println("**********************************"); int[] arr1 = new int[11]; for(int i = 1; i < arr1.length; i++) { arr1[i] = ran.nextInt(1000); } for(int i = 1; i <= 10; i++) { System.out.print(arr1[i] + "\t"); } System.out.print("\n"); System.out.println("堆排序:"); HeapSort(arr1,10); for(int i = 1; i <= 10; i++) { System.out.print(arr1[i] + "\t"); } System.out.print("\n"); System.out.println("**********************************"); for(int i = 0; i < arr.length; i++) { arr[i] = ran.nextInt(1000); } System.out.println(Arrays.toString(arr)); System.out.println("冒泡排序:"); BubbleSort(arr,10); System.out.println(Arrays.toString(arr)); System.out.println("**********************************"); for(int i = 0; i < arr.length; i++) { arr[i] = ran.nextInt(1000); } System.out.println(Arrays.toString(arr)); System.out.println("快速排序:"); QuickSort(arr,0,9); System.out.println(Arrays.toString(arr)); System.out.println("**********************************"); for(int i = 0; i < arr.length; i++) { arr[i] = ran.nextInt(1000); } System.out.println(Arrays.toString(arr)); System.out.println("归并排序:"); MergeSort(arr,10); System.out.println(Arrays.toString(arr)); System.out.println("**********************************"); } /*******************************insert sort**************************/ public static void InsertSort(int[] R, int n) { int i,j,tmp; for(i = 1; i < n; i++) { tmp = R[i]; j = i - 1; while(j >=0 && tmp < R[j]) { R[j + 1] = R[j]; j--; } R[j + 1] = tmp; } } public static void InsertSort2(int[] R, int n) { int i,j; int tmp; int low,mid,high; for(i = 1; i < n; i++) { low = 0; high = i - 1; tmp = R[i]; while(low <= high) { mid = (low + high)/2; if(R[mid] < tmp) low = mid + 1; else high = mid - 1; } for(j = i - 1; j >= high + 1; j--) R[j + 1] = R[j]; R[high + 1] = tmp; } } /*****************************shell sort*****************************/ public static void ShellSort(int R[], int n) { int i,j,tmp,d; d = n / 2; while(d > 0){ for(i = d; i < n; i++) { tmp = R[i]; j = i - d; while(j >= 0 && tmp < R[j]) { R[j + d] = R[j]; j-=d; } R[j + d] = tmp; } d = d/2; } } /******************************select sort****************************/ public static void SelectSort(int R[], int n) { int i, k, j, tmp; for(i = 0; i < n - 1; i++) { k = i; for(j = i + 1; j < n; j++) { if(R[k] > R[j] ) k = j; } if(k != i) { tmp = R[i]; R[i] = R[k]; R[k] = tmp; } } } /****************************Heap sort********************************/ public static void Sift(int R[], int low, int high) { int i = low; int j = 2 * low; int tmp = R[i]; while(j <= high) { if(j < high && R[j + 1] > R[j]) j++; if(tmp < R[j]) { R[i] = R[j]; i = j; j = 2 * i; } else break; } R[i] = tmp; } public static void HeapSort(int R[], int n) { int i,tmp; for(i = n / 2; i >= 1; i--) Sift(R, i, n); for(i = n; i >= 2; i--) { tmp = R[1]; R[1] = R[i]; R[i] = tmp; Sift(R, 1, i - 1); } } /****************************bubble sort******************************/ public static void BubbleSort(int R[], int n) { int i,j,tmp; for(i = 0; i < n - 1; i++) { for(j = 0; j < n - i - 1; j++) { if(R[j] > R[j + 1]) { tmp = R[j]; R[j] = R[j + 1]; R[j + 1] = tmp; } } } } /*****************************quick sort*****************************/ public static void QuickSort(int R[],int s, int t) { int i = s, j = t; int tmp; if(s < t) { tmp = R[s]; while(i != j) { while(j > i && R[j] > tmp) j--; R[i] = R[j]; while(j > i && R[i] < tmp) i++; R[j] = R[i]; } R[i] = tmp; QuickSort(R, s, i - 1); QuickSort(R, i + 1, t); } } /******************************merge sort******************************/ public static void MergeSort(int[]R,int n) { int length; for(length = 1; length < n; length = 2 * length) MergePass(R,length,n); } public static void MergePass(int[] R,int length, int n) { int i; for(i = 0; i + 2 * length - 1 < n; i = i + 2 * length) Merge(R, i, i + length - 1, i + 2 * length - 1); if(i + length - 1 < n) Merge(R, i, i + length - 1,n - 1); } public static void Merge(int[] R, int low, int mid, int high) { int[] R1 = new int[high - low + 1]; int i = low, j = mid + 1,k = 0; while(i <= mid && j <= high) if(R[i] < R[j]) { R1[k] = R[i]; i++; k++; } else { R1[k] = R[j]; j++; k++; } while(i <= mid) { R1[k] = R[i]; i++; k++; } while(j <= high) { R1[k] = R[j]; j++; k++; } for(k = 0,i =low; i <= high; k++,i++) R[i] = R1[k]; } /************************************************************************/ }
标签:
原文地址:http://www.cnblogs.com/rixiang/p/4668754.html