冒泡排序快速排序C语言

//冒泡排序
void BubbleSort(ElemType A[], int n) {
    int i, j, temp;
    int flag=1;
    for (i = 1; i <= n - 1&&flag; i++) {
        flag = 0;//表示在本趟冒泡是否发生交换的标志
        for (j = 1; j <= n - i; j++) 
            if (A[j] > A[j + 1]) {
                temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
                flag = 1;//发生交换
            }
        if (flag == 0)
            return;
    }
}
//快速排序
int QKPass(ElemType A[], int low, int high) {
    ElemType pivot = A[low];
    while (low < high)
    {
        while (low < high && A[high] >= pivot) --high;
        A[low] = A[high];
        while (low < high && A[low] <= pivot) ++low;
        A[high] = A[low];
    }
    A[low] = pivot;
    return low;
}
void QuickSort(ElemType A[], int low, int high) {
    if (low < high) {
        int pivotpos = QKPass(A, low, high);
        QuickSort(A, low, pivotpos - 1);
        QuickSort(A, pivotpos + 1, high);
    }
}
void InputA(int* A, int n) {
    int i;
    for (i = 1; i <= n; i++) {
        scanf_s("%d", &A[i]);
    }
}
void PrintA(int *A,int n) {
    for (int i = 1; i <= n; i++) {
        printf("%2d", A[i]);
    }
}
int main() {
    int low = 0, high = 7;
    int A[100];
    InputA(A, 8);
    //BubbleSort(A, 8);
    //ShellSortw(A, 8);
    //BinSort(A, 8);
    InsertSort(A, 8);
    for (int i = 1; i <= 8; i++) {
        printf("%2d", A[i]);
    }
    system("pause");
    return 0;
}

 

上一篇:Integer小知识


下一篇:快排的改进