所有的排序、查找算法

  1. import javax.mail.Part;  
  2.   
  3. /** 
  4.  *   顺序查找、排序 
  5.  * 
  6.  * @author 曾修建 
  7.  * @version 创建时间:2014-7-30 下午04:15:10 
  8.  */  
  9. public class SequentialSearch {  
  10.   
  11.   
  12.     public static void main(String[] args) {  
  13.         Integer[] a = {1,2,3,4,5,7,6,88};  
  14.   
  15.   
  16.         //二分查找非递归  
  17.         System.out.println( "二分查找非递归实现   位置  :  "+ Binary_Seach(a,0));  
  18.         //二分查找递归实现  
  19.         System.out.println("二分查找递归实现     :" +Binary(a,0,a.length,1));  
  20.         //插值查找  
  21.         System.out.println(" 插值查找位置   :  "+Insert_Search(a,3));  
  22.   
  23.         //斐波拉契查找  
  24.   
  25.         //稠密索引  (家里放的东西记录到笔记本上)/  
  26.         //分块索引(图书馆的书)  
  27.         //倒排索引  
  28.         Integer[] a_sort = {1,33,3,14,5,71,6,88};  
  29.         //希尔排序  
  30.         Shell_Sort(a_sort);  
  31.         //冒泡排序  
  32.         Bubble_Sort(a_sort);  
  33.         //插入排序  
  34.         Insert_Sort(a_sort);  
  35.         //简单选择排序  
  36.         Select_Sort(a_sort);  
  37.         //快速排序  
  38.         Quick_Sort(a_sort);  
  39.         //堆排序  
  40.         Heap_Sort(a_sort);  
  41.         //归并排序  
  42.         Merge_Sort(a_sort);  
  43.   
  44.         for (int i = 0; i < a.length; i++) {  
  45.             if (a[i]==8) {  
  46.                 System.out.println("找到了");  
  47.             }  
  48.         }  
  49.         int i=0;  
  50.         if (a[0]==1) {  
  51.             System.out.println("找到了");  
  52.             //return 0;  
  53.         }  
  54.         a[0]=1;  
  55.         while (a[i]!=1) {  
  56.             i++;  
  57.         }  
  58.         //return i;  
  59.     }  
  60.     //堆排序  
  61.      public static void Heap_Sort(Integer[] array) {    
  62.         // 对数组进行筛选,建成一个大顶堆    
  63.         double len = array.length - 1;    
  64.         for (int i = (int) Math.floor(len/2); i > 0; i--) {    
  65.             heapAdjust(array, i, array.length - 1);    
  66.         }    
  67.         for (int i = array.length - 1; i > 0; i--) {    
  68.             // 将堆顶元素与最后一个元素调换位置,即输出最大值    
  69.            // swap(array, 1, i);    
  70.             int temp;    
  71.             temp = array[1];    
  72.             array[1] = array[i];    
  73.             array[i] = temp;  
  74.             // 将最后一位剔出,数组最大下标变为i-1。自队顶至叶子进行调整,形成一个新堆,此过程称为筛选    
  75.             heapAdjust(array, 1, i - 1);    
  76.         }    
  77.         System.err.println();    
  78.         System.err.println("堆排序 :");    
  79.         for (int i = 1; i < array.length; i++) {    
  80.             System.err.print(array[i] + "   ");    
  81.         }   
  82.     }    
  83.     
  84.     // 建堆函数,认为【s,m】中只有 s    
  85.     // 对应的关键字未满足大顶堆定义,通过调整使【s,m】成为大顶堆=====================================================    
  86.     public static void heapAdjust(Integer[] array, int s, int m) {    
  87.         // 用0下标元素作为暂存单元    
  88.         array[0] = array[s];    
  89.         // 沿孩子较大的结点向下筛选    
  90.         for (int j = 2 * s; j <= m; j *= 2) {    
  91.             // 保证j为较大孩子结点的下标,j < m 保证 j+1 <= m ,不越界    
  92.             if (j < m && array[j] < array[j + 1]) {    
  93.                 j++;    
  94.             }    
  95.             if (!(array[0] < array[j])) {    
  96.                 break;    
  97.             }    
  98.             // 若S位较小,应将较大孩子上移    
  99.             array[s] = array[j];    
  100.             // 较大孩子的值变成S位的较小值,可能引起顶堆的不平衡,故对其所在的堆进行筛选    
  101.             s = j;    
  102.         }    
  103.         // 若S位较大,则值不变;否则,S位向下移动至2*s、4*s、。。。    
  104.         array[s] = array[0];    
  105.     }    
  106.     
  107.     // 交换函数=====================================================    
  108.     public static void swap(Integer[] array, int i, int j) {    
  109.         int temp;    
  110.         temp = array[i];    
  111.         array[i] = array[j];    
  112.         array[j] = temp;    
  113.     }    
  114.       
  115.     //归并排序  
  116.     public static void Merge_Sort(Integer []a) {  
  117.         if (a.length>0) {  
  118.             M_Sort(a,a,0,a.length-1);  
  119.         }  
  120.         System.err.println();  
  121.         System.err.println("归并排序 :  ");  
  122.         for (int i = 0; i < a.length; i++) {  
  123.             System.err.print(a[i]+"   ");  
  124.         }  
  125.     }  
  126.     //归并排序实现  
  127.     public static void M_Sort(Integer []a,Integer []td, int low , int high) {//拆分  
  128.         int mid ;  
  129.         Integer [] td2 = new Integer[a.length];  
  130.         if (low == high) {  
  131.             td[low] = a[high];   
  132.         }  
  133.           
  134.         else {  
  135.             mid =(low+high)/2;  
  136.             M_Sort(a, td2, low, mid);  
  137.             M_Sort(a, td2, mid+1, high);  
  138.             Merge(a, td, low, mid, high);  
  139.         }  
  140.     }  
  141.     //归并排序合并  
  142.     public static void  Merge(Integer []a , Integer []td ,int low , int mid, int high) {  
  143.         int m , n , k;  
  144.         for ( m = low ,n = mid ,k=low ; m < mid && n < high ; k++) {  
  145.             td[k] = a[m] < a[n] ? a[m++]:a[n++];  
  146.         }  
  147.         if (m < mid) {  
  148.             for (int i = 0; i < mid-m; i++) {  
  149.                 td[k+i] = a[m+i];  
  150.             }  
  151.         }  
  152.         if (n<high) {  
  153.             for (int i = 0; i < high-n; i++) {  
  154.                 td[k+i] = a[n+i];  
  155.             }  
  156.         }  
  157.         System.err.println();  
  158.         System.err.println("归并排序 :  ");  
  159.         for (int i = 0; i < td.length; i++) {  
  160.             System.err.print(td[i]+"   ");  
  161.         }  
  162.     }  
  163.       
  164.     //快速排序  
  165.     public  static void Quick_Sort(Integer a[]) {  
  166.         if (a.length>0) {  
  167.             Sort(a,0,a.length-1);  
  168.         }  
  169.         System.err.println();  
  170.         System.err.println("快速排序 :  ");  
  171.         for (int i = 0; i < a.length; i++) {  
  172.             System.err.print(a[i]+"   ");  
  173.         }  
  174.     }  
  175.     //快排实现  
  176.     public static void Sort(Integer []a, int low, int high) {  
  177.   
  178.         if (low<high) {  
  179.             int position = Part(a , low , high);  
  180.             Sort(a, low, position-1);  
  181.             Sort(a, position+1, high);  
  182.         }  
  183.           
  184.   
  185.     }  
  186.     //快排方法  
  187.     public static int Part(Integer []a,int low , int high){  
  188.         int i = low , j = high ,po = a[low];   
  189.         while (i<j) {  
  190.             while (i<j && po < a[j]) {  
  191.                 j--;  
  192.             }  
  193.   
  194.             a[i] = a[j];//比中轴小的记录移到低端    
  195.   
  196.             while (i<j && po >a[i]) {  
  197.                 i++;  
  198.             }  
  199.             a[j] = a[i];//比中轴大的记录移到高端    
  200.   
  201.   
  202.         }  
  203.     //  a[low] =po;   //中轴记录到尾    
  204.         return i;  
  205.     }  
  206.     //简单选择排序  
  207.     public static void Select_Sort(Integer []a) {  
  208.         for (int i = 0; i < a.length; i++) {  
  209.             int min =i;  
  210.             for (int j = i+1; j < a.length; j++) {  
  211.                 if (a[min] > a[j]) {  
  212.                     min = j;  
  213.                 }  
  214.             }  
  215.             if (min!=i) {  
  216.                 int temp = a[min];  
  217.                 a[min] = a[i];  
  218.                 a[i] = temp;  
  219.             }  
  220.         }  
  221.         System.err.println();  
  222.         System.err.println("简单选择 :  ");  
  223.         for (int i = 0; i < a.length; i++) {  
  224.             System.err.print(a[i]+"   ");  
  225.         }  
  226.   
  227.     }  
  228.     //插入排序  
  229.     public static void  Insert_Sort(Integer []a) {  
  230.         for (int i = 1; i < a.length; i++) {  
  231.             if (a[i]<a[i-1]) {  
  232.                 for (int j = i; j >=0; j--) {  
  233.                     if (a[i]<a[j]) {  
  234.                         int temp = a[i];  
  235.                         a[i] = a[j];  
  236.                         a[j] = temp;  
  237.                     }  
  238.                 }  
  239.             }  
  240.         }  
  241.         System.err.println();  
  242.         System.err.println("插入 :  ");  
  243.         for (int i = 0; i < a.length; i++) {  
  244.             System.err.print(a[i]+"   ");  
  245.         }  
  246.     }  
  247.   
  248.     //希尔排序  
  249.     public static void  Shell_Sort(Integer []a) {  
  250.         int step =a.length;  
  251.         do {  
  252.             step = step/3+1;  
  253.             for (int i = step+1; i < a.length; i++) {  
  254.                 if (a[i]<a[i-step]) {  
  255.                     int temp = a[i];  
  256.                     a[i] = a[i-step];  
  257.                     a[i-step] =temp;  
  258.                 }  
  259.             }  
  260.         } while (step>1);  
  261.         System.err.println();  
  262.         System.err.println("希尔  :  ");  
  263.         for (int i = 0; i < a.length; i++) {  
  264.             System.err.print(a[i]+"   ");  
  265.         }  
  266.   
  267.     }  
  268.   
  269.     //冒泡排序  
  270.     public static void Bubble_Sort(Integer []a ) {  
  271.         for (int i = 1; i < a.length; i++) {  
  272.             for (int j = a.length-1; j >=i; j--) {  
  273.                 if (a[j]<a[j-1]) {  
  274.                     int temp = a[j];  
  275.                     a[j] = a[j-1];  
  276.                     a[j-1] = temp;  
  277.                 }  
  278.             }  
  279.         }  
  280.         System.err.println();  
  281.         System.err.println("冒泡  :  ");  
  282.         for (int i = 0; i < a.length; i++) {  
  283.             System.err.print(a[i]+"   ");  
  284.         }  
  285.     }  
  286.   
  287.     //插值查找  
  288.     public static int  Insert_Search(Integer []a , int k) {  
  289.         int low = 0 , high = a.length-1 , mid=-1;  
  290.         while (low<=high) {  
  291.             mid = low+((k-a[low])/(a[high]-a[low]))*(high-low);  
  292.             if (a[mid] == k) {  
  293.                 return mid;  
  294.             }  
  295.             else if (a[mid] > k) {  
  296.                 high = mid-1;  
  297.             }  
  298.             else {  
  299.                 low = mid+1;  
  300.             }  
  301.         }  
  302.         return -1;  
  303.     }  
  304.   
  305.     //递归实现二分查找  
  306.     public static int  Binary(Integer []a , int low , int high ,int k) {  
  307.         int mid = (low+high)/2;  
  308.         if (a[mid] == k) {  
  309.             return mid;  
  310.         }  
  311.         else if (a[mid] > k) {  
  312.             return Binary(a, low , mid-1, k);  
  313.         }  
  314.         else {  
  315.             return Binary(a, mid + 1, high, k);  
  316.         }  
  317.     }  
  318.   
  319.     //非递归实现二分查找  
  320.     public static int Binary_Seach(Integer[] a , int k){  
  321.         int low = 0 , high = a.length , mid=-1;  
  322.         while (low<=high) {  
  323.             mid = (low+high)/2;  
  324.             if (a[mid]==k) {  
  325.                 return mid;  
  326.             }  
  327.             else if (a[mid]>k) {  
  328.                 high = mid-1;  
  329.             }  
  330.             else {  
  331.                 low = mid+1;  
  332.             }  
  333.         }  
  334.         return -1;  
  335.     }  
  336. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值