C語言中的排序算法

一. 直接插入排序
void insertSort( int[] a){
       for( int i=1;i<a.length; i++){
            if (a[i]<a[i-1]){
               temp = a[i];   //1 
               a[i] = a[i-1]; //2

               // 繼續和前面的進行比較
                for( int j=i-2; j>=0; j--){
                     if(temp < a[j])
                         a[j+1] =a[j]; //3
               }
               a[j+1] = temp; //4
           }
      }
}

算法(簡要描述):
1. temp保存被比較的數值
2. 前一位數值移動到被比較的數值的位置
3. 前面的繼續日後移動
4. 把被比較的數值放到適當的位置
直接插入排序


二.冒泡排序
  冒泡排序,就是從最底那個開始往上比較,遇到比它小的就交換,至關於過五關看六將,不斷地向前衝。接着循環第二個...
+-----+     void bubbleSort( int[] a){
| a[6] |             //每一個都進行冒泡(一個一個來)
+-----+             for ( int i=0; i<a.length; i++){
| a[5] |    
+-----+                     //和後面的每一個都進行比較(過五關看六將)
| a[4] |                     for ( int j=i; j<a.length-1; j++){
+-----+                             if (a[j]>a[j+1]){
| a[3] |                                    temp = a[j];
+-----+                                    a[j] = a[j+1];
| a[2] |                                    a[j+1] = temp;
+-----+                            }
| a[1] |                    }
+-----+            }
| a[0] |    }
+-----+

三.選擇排序
  選擇排序,就是選擇最小的,而後置換,循環再找到最小的,再置換...
void selectSort( int[] a){
         for ( int i=0; i<a.length; i++){
                small = i;

                 //找出最小的
                 for ( int j=i+1; j<a.lenth; j++){
                         if (a[small]>a[j]){
                                small = j;
                        }
                }

                 //置換位置
                 if (i != small){
                        temp = a[small];
                        a[small] = a[i];
                        a]i] = temp;
                }
        }
}

四.快速排序
  快速排序的基本過程:
  獲得樞軸索引:compare首先從high位置向前搜索找到第一個小於compare值的索引,並置換(這時high索引位置上的值爲compare 值);而後從low位置日後搜索找到第一個大於compare值的索引,並與high索引上的值置換(這時low索引位置上的值爲compare值);重複這兩步直到low=high爲止。
  獲得樞軸索引後,則遞歸進行樞軸兩邊的隊列的排序....
void quickSort( int[] a, int low, int high) {
        p = get(a, low, high);
        quickSort(a, low, p-1);
        quickSort(a, p+1, high);
}

int get( int[] a, int low, int high){ 
     compare = a[low]; 

      while(low < high){  //不管如何置換, 被置換的都包含compare的值
          while(low<high && a[high]>=compare)    
              high--; 

          //在 low<high 的狀況下找到a[high]<compare並置換 
         temp = a[low];
         a[low] = a[high];
         a[high] = temp;

          while(low<high && a[low]<=compare) 
              low++; 

          //在 low<high 的狀況下找到a[low]>compare並置換 
          temp = a[low]; 
          a[low] = a[high]; 
          a[high] = temp; 
     } 
      return low; //while(low==hight)中止循環, 並返回樞軸位置
}

五.二分查找
  二分查找原理很容易懂,想象爲二叉查找樹就明白了。
int binarySearch( int[] a, int value){
         int low = 0;
         int high = a.length-1;
        
         while(low <= high){
                mid = (low+high)/2; //**
                 if (a[mid] == value)
                         return mid;
                 else if (a[mid] > value)
                        high = mid-1;
                 else
                        low = mid +1;
        }
         return -1;
}
    ** mid = (low + high) / 2; 有問題,當low + high大於int範圍時就會溢出的。Sun的jdk裏面的二分查找源碼原先也有一樣的問題。    解決的方法是mid = low/2 + high/2。這樣用2先除一下,就不會溢出了
相關文章
相關標籤/搜索