Day13 排序复习

快排

#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int a[105],n;

void quickSort(int low, int high){
    if(high<=low) return;
    int tmp = a[low];
    int l=low,h=high;

    while(lowtmp && high>low) high--;
        a[low] = a[high];
        while(a[low]<=tmp && low

堆排序

#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int a[105],n;

//调整堆
void push_down(int root,int len){
    int tmp = a[root];

    while(true){
        int child = root*2;
        if(child>len){

            break;
        }
        if(root*2+1<=len){
            if(a[root*2]=1; i--){
        push_down(i,len);
    }

    while(true){
        swap(a[1],a[len]);
        push_down(1,--len);
        if(len == 1) break;
    }
}

int main(){
    scanf("%d",&n);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    HeapSort(n);
    for(int i=1; i<=n; i++){
        printf("%d ",a[i]);
    }
    return 0;
}

归并排序

#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int a[105],n;

void Merge(int l,int r){
    int b[105];
    for(int i=l; i<=r; i++) b[i] = a[i];

    int mid = (l+r)/2;
    int posl=l,posr=mid+1,k=l;
    while(posl<=mid && posr<=r){
        if(b[posl]


你可能感兴趣的:(数据结构)