浏览 1806 次
锁定老帖子 主题:java 排序算法
精华帖 (0) :: 良好帖 (0) :: 新手帖 (15) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-12-25
public void doBubbleSort(int[] a){ int temp; for(int i = 0 ; i < a.length ; i++){ for(int j = 0; j < a.length - i - 1; j++){ if(a[j] > a[j+1]){ temp = a[j];//back move a[j] = a[j+1]; a[j+1] = temp; } } } } -----------------------direct insert sort----------------------------- /** * 直接插入排序 *@param int a[] *@date Dec 24, 2008 *@description 描述 */ public void inSort(int a[]) { int k = a.length; int c = 0; for(int i=1;i<k;++i) { for(int j=i-1;j>=0;--j) { if(a[j]>a[j+1]){ c = a[j]; a[j] = a[j+1]; a[j+1] = c; } } } for(int l=0;l<a.length;++l) { System.out.println("InsertSort 第"+l+"个 :"+a[l]); } } ------------------------------------disCount sort--------------------- /** * 折半插入排序 *@param int a[] *@date Dec 24, 2008 *@author Chao *@description 描述 */ public void disCountSort(int temp[]) { int length = temp.length; for(int i=1;i<length;i++) { int tempVar = temp[i]; int low = 0; int high = i-1; while(low<=high) {//减少对比 int middle = (low+high)/2; if(tempVar<temp[middle]) { high = middle-1; }else { low = middle+1; } for(int j=i;j>high+1;j--) { temp[j]=temp[j-1]; temp[high+1] = tempVar; } } } for(int l=0;l<temp.length;++l) { System.out.println("discountSort 第"+l+"个 :"+temp[l]); } } ---------------------------------------------------------------------- ------------------------------------------------------------------- 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-12-25
cherryQQ 写道 ------------------//bubble Sort-------------------------------------
public void doBubbleSort(int[] a){ int temp; for(int i = 0 ; i < a.length ; i++){ for(int j = 0; j < a.length - i - 1; j++){ if(a[j] > a[j+1]){ temp = a[j];//back move a[j] = a[j+1]; a[j+1] = temp; } } } }-----------------------direct insert sort----------------------------- /** * 直接插入排序 *@param int a[] *@date Dec 24, 2008 *@description 描述 */ public void inSort(int a[]) { int k = a.length; int c = 0; for(int i=1;i<k;++i) { for(int j=i-1;j>=0;--j) { if(a[j]>a[j+1]){ c = a[j]; a[j] = a[j+1]; a[j+1] = c; } } } for(int l=0;l<a.length;++l) { System.out.println("InsertSort 第"+l+"个 :"+a[l]); } } ------------------------------------disCount sort--------------------- /** * 折半插入排序 *@param int a[] *@date Dec 24, 2008 *@author Chao *@description 描述 */ public void disCountSort(int temp[]) { int length = temp.length; for(int i=1;i<length;i++) { int tempVar = temp[i]; int low = 0; int high = i-1; while(low<=high) {//减少对比 int middle = (low+high)/2; if(tempVar<temp[middle]) { high = middle-1; }else { low = middle+1; } for(int j=i;j>high+1;j--) { temp[j]=temp[j-1]; temp[high+1] = tempVar; } } } for(int l=0;l<temp.length;++l) { System.out.println("discountSort 第"+l+"个 :"+temp[l]); } } ---------------------------------------------------------------------- ------------------------------------------------------------------- |
|
返回顶楼 | |