可供复用的小函数

十大排序算法

冒泡排序

void bubble_sort(int* arr, int n) {
    int i = n - 1, j;  // i: 共n-1趟, j:每趟从1到n-1
    int temp;
    if (n > 1) {
        while(i > 0) {
            j = 1;
            while (j < n) {
                if (arr[j-1] > arr[j]) {
                    int temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                }
                ++j;
            }
            --i;
        }
    }
}

选择排序

void select_sort(int* arr, int n) {
    int i = 0, j, min_index;  // i位置准备放每趟最小的数, j进行一趟为排序部分的扫描
    int temp;
    if (n > 1) {
        while (i < n-1) {
            j = i;
            min_index = j;
            while (j < n) {
                if (arr[j] < arr[min_index]) {
                    min_index = j;
                }
                ++j;
            }
            if (i != min_index) {
                temp = arr[i];
                arr[i] = arr[min_index];
                arr[min_index] = temp;
            }
            ++i;
        }
    }
}

插入排序

void insert_sort(int* arr, int n) {
    int i = 1, j;
    int left, right, mid;  // 用于二分插入的辅助变量
    int temp;
    if (n > 1) {
        while (i < n) {
            left = 0;
            right = i-1;
            temp = arr[i];
            while (left < right) {
                mid = left + (right - left) / 2;
                if (temp < arr[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }  // while结束时, 正确的插入位置可能是left或left+1

            int pos = arr[left] < temp ? left+1 : left;
            for (j = i; j >= pos; --j) {
                arr[j] = arr[j-1];
            }
            arr[pos] = temp;
            ++i;
        }
    }
}

希尔排序

void shell_sort(int* arr, int n) {
    int step;  // 以step为间隔进行分组
    int i, j;   //i: 插入排序中每次作为待插入元素的位置, j: 遍历有序部分的指针
    int temp;
    step = n/2;
    while (step >=1 ) {
        i = step;  // 从该组的第二个元素开始
        while (i < n) {
            temp = arr[i];
            j = i - step;
            while (j >= 0 && temp < arr[j]) {
                arr[j+step] = arr[j];
                j -= step;
            }
            // 停下的时候j位置的元素是从后往前的第一个不大于temp的数,所以应该插在j位置之后
            arr[j+step] = temp;
            ++i;
        }
        step /= 2;
    }
}

归并排序

void merge(int* arr, int mid, int n, int* buffer) {
    //int* temp = (int*)malloc(sizeof(int)*n);
    int p = 0, p1 = 0, p2 = mid;
    memcpy(temp, arr, sizeof(int)*n);
    while(p1 < mid && p2 < n) {
        if (temp[p1] < temp[p2]) {
            arr[p++] = temp[p1++];
            ++p1;
        } else {
            arr[p++] = temp[p2++];
        }
    }
    while (p1 >= mid && p2 < n) {
        arr[p++] = temp[p2++];
    }
    while (p2 >= n && p1 < mid) {
        arr[p++] = temp[p1++];
    }
    free(temp);
}


void merge_sort(int* arr, int n) {
    if (n == 1) {
        return ;
    }
    int mid = n/2;
    merge_sort(arr, mid);
    merge_sort(arr+mid, n-mid);
    merge(arr, mid, n);
}

快速排序

int partition_sort(int* arr, int begin, int end) {
    int i = begin, j = end;
    int key = arr[begin];
    while (i < j) {
        while (i < j && arr[j] > key) {
            --j;
        }
        arr[i] = arr[j];
        while (i < j && arr[i] <= key) {
            ++i;
        }
        arr[j] = arr[i];
    }
    arr[i] = key;
    return i;
}

void internal_quick_sort(int* arr, int begin, int end) {
    if (begin >= end) {
        return ;
    }
    int pivot = partition_sort(arr, begin, end);
    internal_quick_sort(arr, begin, pivot-1);
    internal_quick_sort(arr, pivot+1, end);
}

void quick_sort(int* arr, int n) {
    internal_quick_sort(arr, 0, n-1);
}

查找类

二分查找

int search(int* nums, int numsSize, int target){
    int left = 0, right = numsSize-1, mid;
    int ret_index = -1;
    while (left <= right) {
        mid = (left + right) / 2;
        if (target < nums[mid]) {
            right = mid - 1;
        } else if (target > nums[mid]) {
            left = mid + 1;
        } else {
            ret_index = mid;
            break;
        }
    }
    return ret_index;
}

打印类

打印8位二进制

以标准输出流打印一个字节的二进制形式。

#define NEWLINE printf("\n")

static void printByte_Binary(unsigned char byte) {
    char zero_tag = 0;
    if (byte == 0) {
        zero_tag = 1;
        printf("0");
    }
    if (!zero_tag) {
        int i;
        char ch[8] = {'0', '0', '0', '0', '0', '0', '0', '0'};
        for (i = 0; i < 8; ++i) {
            ch[7 - i] |= (byte & 0x01);
            byte >>= 1;
        }
        char found_first_non_zero = 0;
        for (i = 0; i < 8; ++i) {
            if (!found_first_non_zero) {
                if (ch[i] == '1') {
                    printf("%c", ch[i]);
                    found_first_non_zero = 1;
                }
            } else {
                printf("%c", ch[i]);
            }
        }
    }
    NEWLINE;
}
posted @ 2022-06-19 16:16  ZenonX  阅读(36)  评论(0编辑  收藏  举报