排序

排序算法

快速排序

void quicksort(int left,int right) {
    int i=left,j=right,tmp=a[left],t;
    if(left>right) return;
    while(i!=j) {
        while(a[j] >= tmp && iwhile(a[i] <= tmp && iif(ileft] = a[i];
    a[i] = tmp;
    quicksort(left,i-1);
    quicksort(i+1,right);

}

稳定性:不稳定;
时间复杂度:O(nlogn);


归并排序

void mergeing(int *left,int left_size,int *right,int right_size) {
    int tmp[MAXSIZE];
    int i=0,j=0,k=0;
    while(iif(left[i] < right[j])
            tmp[k++] = left[i];
        else
            tmp[k++] = right[j];
    }
    while(ileft[i++];
    while(jright[j++];
    for(int m=0;m<(left_size+right_size);m++)
        left[m] = tmp[m];
}
void mergesort(int a[],int n) {
    if(n>1) {
        int *left = a;
        int left_size = n/2;
        int *right = a + n/2;
        int right_size = n - left_size;

        mergesort(left,left_size);
        mergesort(right,right_size);

        mergeing(left,left_size,right,right_size);

    }
}

稳定性:稳定
时间复杂度:O(nlogn)


冒泡排序

for(int i=0;i
    for(int j=0;j
        if(a[j]>a[j+1]) {
            int tmp = a[j];
            a[j] = a[j+1];
            a[j+1] = tmp;
        }
    }
}

稳定性:稳定;
时间复杂度:O(n^2);


选择排序

for(int i=0;i
    int minIndex = i;
    for(int j=i+1;j
        if(a[j] < a[minIndex])
            minIndex = j;
    }
    int tmp = a[i];
    a[i] = a[minIndex];
    a[minIndex] = a[i];
}

稳定性:不稳定;
时间复杂度:O(n^2);


直接插入排序

void insertionSort(int a[],int n) {
    for(int i=1;iint j = i-1;
        int tmp = a[i];
        while(j>=0 && tmp < a[j]) {
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = tmp;
    }
}

稳定性:稳定
时间复杂度:O(n^2)


折半插入排序

void binaryInsertionSort(int a[],int n) {
    int i,j,tmp,hight,low,mid;
    for(i=1;i0;
        hight = i-1;
        tmp = a[i];
        while(low <= hight) {
            mid = (low+hight)/2;
            if(a[mid] > tmp)
                hight = mid-1;
            else
                low = mid+1;
        }
        for(j=i-1;j1;j++)
            a[j+1] = a[j];
        a[j+1] = tmp;

    }

}

稳定性:稳定
平均时间复杂度:O(n^2)


希尔排序

void shellSort(int a[],int n) {
    int gap = n/2;
    while(gap >= 1) {
        for(int i=gap;iint j = i-gap;
            int tmp = a[i];
            while(j>=0 && tmp < a[j]) {
                a[j+gap] = a[j];
                j -= gap;
            }
            a[j+gap] = tmp;
        }
        gap /= 2;
    }
}

稳定性:不稳定
平均时间复杂度:O(n^1.3)


堆排序

void swap(int a[],int i,int j) {
    int tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
}

void heapity(int a[],int i,int n) {            //调整堆
    int left = 2*i+1,right = 2*i+2,largest = i;
    if(left > n && a[left] > a[largest])
        largest = left;
    if(right >n && a[right] > a[largest])
        largest = right;
    if( largest != i) {
        swap(a,i,largest);
        heapity(a,largest,n);
    }
}

void buildHeap(int a[],int n) {     //建立大顶堆
    for(int i=n/2;i>=0;i--) {
        heapity(a,i,n);
    }
}

void HeapSort(int a[],int n) {
    buildHeap(a,n);
    for(int i=n-1;i>=0;i--) {
        swap(a,0,i);
        n--;
        heapity(a,0,n);
    }
}

稳定性:不稳定
平均时间复杂度:O(nlogn)

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