插入,希尔,快速,堆排序实现
2014-09-07 22:08
204 查看
import javax.naming.ldap.SortControl; import javax.xml.crypto.Data; class QuikSort { public static int partion(int arr[],int s,int t) { int temp=arr[s]; while(s<t) { while(s<t&&temp<=arr[t]) t--; arr[s]=arr[t]; while(s<t&&temp>arr[s]) s++; arr[t]=arr[s]; } arr[s]=temp; return s; } public static void qsort(int arr[],int s,int t) { if (s<t) { int mid=partion(arr,s,t); qsort(arr, s, mid-1); qsort(arr, mid+1, t); } } public static void quikSort(int arr[]) { qsort(arr,0,arr.length-1); } } class HeapSort { public static void heapSort(int arr[]) { for (int i = arr.length-1; i>0; i--) { buildMinHeap(arr,i); swap(arr,0,i); } } private static void swap(int[] arr, int i, int j) { // TODO Auto-generated method stub int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } private static void buildMinHeap(int[] arr, int lastIndex) { // TODO Auto-generated method stub for (int i = lastIndex/2; i>=0; i--) { int k=i; while(2*k+1<=lastIndex) { int smallerIndex=2*k+1; if (smallerIndex<lastIndex) { if (arr[smallerIndex]>arr[smallerIndex+1]) { smallerIndex++; } } if (arr[k]>arr[smallerIndex]) { swap(arr, k, smallerIndex); k=smallerIndex; } else { break; } } } } } class MergeSort { public static void mergeSort(int arr[]) { sort(arr,0,arr.length-1); } private static void sort(int[] arr, int left, int right) { // TODO Auto-generated method stub if (left>=right) { return; } int center=(left+right)/2; sort(arr, left, center); sort(arr, center+1, right); merge(arr,left,center,right); } private static void merge(int[] arr, int left, int center, int right) { // TODO Auto-generated method stub int temparr[]=new int[arr.length]; int mid=center+1; int third=left; int tmp=left; while(left<=center&&mid<=right) { if (arr[left]<=arr[mid]) { temparr[third++]=arr[left++]; } else { temparr[third++]=arr[mid++]; } } while(mid<=right) temparr[third++]=arr[mid++]; while(left<=center) temparr[third++]=arr[left++]; while(tmp<=right) arr[tmp]=temparr[tmp++]; } } public class Test3 { public void insertSort(int arr[]) { for (int i = 1; i < arr.length; i++) { int temp=arr[i]; int j; for (j = i; j>0&&temp<arr[j-1]; j--) { arr[j]=arr[j-1]; } arr[j]=temp; } } public void shellSort(int arr[]) { int d=arr.length/2; while(d>0) { for (int i = d; i < arr.length; i++) { int temp=arr[i]; int j; for (j = i-d; j>=0&&temp<arr[j]; j-=d) { arr[j+d]=arr[j]; } arr[j+d]=temp; } d=d/2; } } public void run() { int arr[]={5,2,4,1,3}; MergeSort.mergeSort(arr); for (int i : arr) { System.out.print(i+" "); } } public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub new Test3().run(); } }
相关文章推荐
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)
- 七大排序算法(冒泡,选择,插入,二分法排序,希尔,快速,合并,堆排序)的java实现
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)
- 常见内部排序算法 简单数组实现与分析(快速(偶原创partition函数,望众高手指正)、归并、希尔、插入、选择、冒泡)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- java实现各种基础排序(冒泡排序、快速排序、直接选择排序、堆排序、直接插入排序、归并排序)
- 排序算法,插入、快速、希尔、基数、归并排序的代码实现和效率分析
- 排序算法复习(Java实现):插入,冒泡,选择,Shell,快速排序, 归并排序,堆排序,桶式排序,基数排序
- 七种排序(直接插入、折半插入、希尔、起泡、快速、简单选择、堆排序)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- C语言实现基本排序算法----排序(直接插入排序,SHELL排序,冒泡排序,快速排序,简单选择排序,堆排序)
- C语言常用排序——直接插入,冒泡,选择,希尔,快速,堆排序
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 选择,插入,希尔,快速,堆,归并排序六种排序方式的Java 实现和性能对比(付代码)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 七种排序(直接插入、折半插入、希尔、起泡、快速、简单选择、堆排序)