常见排序算法集锦

一般而言,常见的排序算法包括以下几类:

  • 交换排序:冒泡排序 快速排序
  • 选择排序:简单选择排序 堆排序
  • 插入排序:直接插入排序 shell排序

交换排序

  • 冒泡排序
void bubbleSort(int a[], int length) {
    for (int i = 0; i < length-1; i++) {
        for (int j = 0; j < length-i-1; j++) {
            if (a[j] > a[j+1]) {swaps(a, j, j+1);
            }
        }
    }
}
  • 快速排序
void quickSort(int low, int high, int a[]) {
    int top = low;
    int tail = high;
    int temp = a[top];//中间值
    if (low >= high) {
        return;
    }
    while (top < tail) {
        
        //里面的两个while顺序与temp取top还是tail有关
        while (a[tail] >= temp && top < tail) tail--;
        a[top] = a[tail];
        
        while(a[top] <= temp && top < tail) top++;
        a[tail] = a[top];
    }
    int middle = top;
    a[middle] = temp;
    quickSort(low, middle-1, a);
    quickSort(middle+1, high, a);
}

选择排序

  • 简单选择排序
void selectSort(int a[], int length) {
    for (int j = 0; j < length-1; j++) {
        int max = j;
        for (int i = j+1; i < length; i++) {
            if (a[max] > a[i]) {
                max = i;
            }
        }
        if (max != j) {
            swaps(a, j, max);
        }
    }
}
  • 堆排序
void adjustHeap(int a[], int i, int length) {
    int k = 2*i + 1;//左孩子
    int temp = a[i];
    
    for (; k < length; k = 2*k+1) {
        if (k+1 < length && a[k] < a[k+1]) {//有右孩子且右孩子大于左孩子
            k++;
        }
        if (a[k] > temp) {//孩子大于父节点
            a[i] = a[k];
            i = k;
        } else {
            break;
        }
    }
    a[i] = temp;
   
}


// 堆排序
void heapSort(int a[], int length) {
 
    // 构建大根堆
    for (int i = length/2 -1; i >= 0; i--) {
        adjustHeap(a, i, length);
    }
    
    for (int j = length-1; j > 0; j--) {
        swaps(a, 0, j);
        adjustHeap(a, 0, j);
    }
}

插入排序

  • 直接插入排序
void insertSort(int a[], int length) {
    for (int i = 1; i < length; i++) {
        if (a[i] < a[i-1]) {
            int temp = a[i];//要插入的数
            int j = i-1;
            for (; j >= 0 && a[j] > temp; j--) {
                a[j+1] = a[j];
            }
            a[j+1] = temp;
        }
    }
}
  • 希尔排序
void shellSort(int a[], int length) {
    for (int gap = length/2; gap > 0; gap /= 2) {
        for (int i = gap; i < length; i++) {
            int temp = a[i];
            int j = i-gap;
            while (j >= 0 && a[j] > temp) {
                a[j+gap] = a[j];
                j -= gap;
            }
            a[j+gap] = temp;
        }
    }
}

其他排序

  • 归并排序
void mergeSort(int a[], int low, int high) {
    int mid = (low+high)/2;
    if (low < high) {
        mergeSort(a, low, mid);
        mergeSort(a, mid+1, high);
    }
}

void merge(int a[], int low, int mid, int high) {
    int leftLenth = mid-low+1;
    int rightLength = high-mid+1;
    int *leftArr = (int *)malloc(leftLenth*sizeof(int));
    int *rightArr = (int *)malloc(rightLength*sizeof(int));
    
    for (int i = 0; i < leftLenth-1; i++) {
        leftArr[i] = a[i];
    }
    for (int j = 0; j < rightLength-1; j++) {
        rightArr[j] = a[mid+1+j];
    }
}

你可能感兴趣的:(常见排序算法集锦)