排序(新排版)

冒泡排序

void bubbleSort(int a[], int count) {
    int temp = 0;

    if (a == NULL) return;
    if (!count) return;

    for (int i=0; i

插入排序

void insertSort(int a[], int count) {
    int i = 1, j = 0;
    int temp = 0;

    for (; i0 && temp < a[j-1]) {
            a[j] = a[--j]; 
        }
        if (i != j) {
            a[j] = temp;
        }
    }
}

二分插入排序

void binaryInsertSort(int a[], int count) {
    int i, j;
    int left, middle, right, temp = 0;
    
    if (!a || count <= 0) return;
    for (i=1; ileft) {
            a[j] = a[--j];
        }
        a[j] = temp;
    }
}

希尔排序

void shellSort(int a[], int count) {
    int stepLength = count / 2;
    int i, j, k;
    
    if (!a || count <= 0) return;
  
    while(stepLength) {
         for (i=0; i= stepLength && temp < a[k-stepLength]) {
                       a[k] = a[k-stepLength];
                       k -= stepLength;
                  }

                if (k != j) a[k] = temp;
             }
        }      

        stepLength /= 2;
    }
}

选择排序

void selectSort(int a[], int count) {
    int i, j;
    int temp, minIndex = 0;

    for (i=0; i

快速排序

void quickSort(int a[], int count) {
    if (!a || !count) return;
    _quickSort(a, 0, count-1);
}

void _quickSort(int a[], int beginIndex, int endIndex) {
    int i = beginIndex, j = endIndex;
    int flag = 0;

    flag = a[beginIndex];

    if (beginIndex >= endIndex) return;

    while(ii && a[j]>flag) { j--; }

        if (i j) {
        _quickSort(a, j+1, end);
    }
}

你可能感兴趣的:(排序(新排版))