快速排序,数组去重

/**
 * Quick Sort, 
 * 
 * int main() { 
 *     int array[8] = {1,3,6,5,4,2,8,7};
 *     fts_quick_sort(array, 0, 7);
 * }
 *
 * @param arr
 *   The pointer of array to sort
 * @param low
 *   The array start index 
 * @param high
 *   The array end index
 *
 */
static inline void
fts_quick_sort(int *arr, int low, int high)
{
    if (low < high) {
        int i = low;
        int j = high;
        int k = arr[low];
        
        while (i < j) {
            
            while (i < j && arr[j] >= k) {
                j--;
            }
            
            if (i < j) {
                arr[i++] = arr[j];
            }
            
            while (i < j && arr[i] < k) {
                i++;
            }
            
            if (i < j) {
                arr[j--] = arr[i];
            }
        }
        
        arr[i] = k;
        
        fts_quick_sort(arr, low, i - 1);
        
        fts_quick_sort(arr, i + 1, high);
    }
}

/**
 * Uniq
 *
 * @param arr
 *   The pointer of array, it must be sorted;
 * @param len
 *   The array length
 *
 */
static inline int
fts_uniq_array(int *arr, int len)
{
    int i = arr[0];
    int nb = 1;
    
    for (int j = 0; j < len; j++) {
        if (i != arr[j]) {
            nb++;
            i = arr[j];
        }
    }
    
    return nb;
}

static inline int
fts_digit_nb(int num)
{
    int n = 1;
    
    while (num /= 10) {
        n++;
    }
    
    return n;
}

 

posted @ 2021-11-02 11:56  撒欢  阅读(128)  评论(0编辑  收藏  举报