posts - 1,  comments - 25,  trackbacks - 0

          2.1-1這題可以參照書上17自己給出過程,這里就略去了。

          2.1-2 先給出書上insertion-sort的C源代碼吧,然后再給出按照非升序的代碼:

          課本中(非降序的)insertion-sort代碼:

           

          1. void insertion_sort(int *A, int n)  
          2. {  
          3.     int i,j;  
          4.     int key; 
          5.  
          6.     for(i = 1; i < n; i++)  
          7.     {  
          8.         j = i - 1;  
          9.         key = A[i]; 
          10.  
          11.         while(j >= 0 && A[j] > key)  
          12.         {  
          13.             A[j+1] = A[j];  
          14.             j = j - 1;  
          15.         } 
          16.  
          17.         A[j+1] = key;  
          18.     }  

           

          在這題中,只要講非降序改成非升序排序,所以改后代碼如下:

           

          1. void insertion_sort(int *A, int n)  
          2. {  
          3.     int i,j;  
          4.     int key; 
          5.  
          6.     for(i = 1; i < n; i++)  
          7.     {  
          8.         j = i - 1;  
          9.         key = A[i]; 
          10.  
          11.         while(j >= 0 && A[j] < key)  
          12.         {  
          13.             A[j+1] = A[j];  
          14.             j = j - 1;  
          15.         } 
          16.  
          17.         A[j+1] = key;  
          18.     }  

           

          2.1-3這題給出偽代碼:

           

          1. int find(int *A , int n, int v) 
          2.  
          3.  
          4.     int  i = 0; 
          5.  
          6.     for( ; i < n; i++) 
          7.  
          8.     { 
          9.  
          10.         if(v == A[i]) 
          11.  
          12.             return i; 
          13.  
          14.     } 
          15.  
          16.   
          17.  
          18.     return NIL; 
          19.  

           

          2.1-4直接給出代碼:

           

          1. /*在A[]和B[]中,數組的最低位對應與二進制的高位,即如果一個二進制數是011100,用數組表示就是A[] = {0,1,1,1,0,0}*/ 
          2.  
          3. void add(int *A ,int *B, int *C, int n)  
          4. {  
          5.     int i, a, c = 0;  
          6.     int s;  
          7.     for(i = n - 1; i >= 0 ; i--)  
          8.     {  
          9.         s = A[i] + B[i];  
          10.          
          11.         C[i+1] = (s + c) % 2;  
          12.         c = (s + c) / 2;  
          13.     } 
          14.  
          15.     C[0] = c;  

           

          posted on 2012-07-23 22:12 Daniel 閱讀(235) 評論(0)  編輯  收藏 所屬分類: CoreJava
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 普定县| 神池县| 乌拉特后旗| 临江市| 苍南县| 台中县| 璧山县| 改则县| 北宁市| 新宁县| 邛崃市| 西畴县| 密山市| 古交市| 平谷区| 宜兰市| 梁河县| 玉林市| 福鼎市| 阿拉善右旗| 乡宁县| 绥阳县| 浮梁县| 布尔津县| 富宁县| 永新县| 横山县| 夏津县| 柯坪县| 长泰县| 宽甸| 金华市| 新化县| 申扎县| 巍山| 东台市| 岱山县| 西峡县| 石门县| 佛冈县| 宜城市|