【数据结构】——排序

固定格式
#include<bits/stdc++.h>
using namespace std;
void Print(int array[],int len){//输出函数
	for(int i=0;i<len;i++){
		printf("%d ",array[i]);
	}
	printf("\n");
}
int main(){
	int array[10] = {50,26,38,80,70,90,8,30,40,20};
	int len = sizeof(array) / sizeof(int);
	printf("初始序列:\n");
	Print(array,len);
	xxxxxx(array,len);//排序函数
	printf("排序后序列:\n");
	Print(array,len);
}

插入排序

直接插入排序

#include<bits/stdc++.h>
using namespace std;
void Print(int array[],int len){//输出函数
	for(int i=0;i<len;i++){
		printf("%d ",array[i]);
	}
	printf("\n");
}
void InsertSort(int array[],int len){
    int i,j;
    for(i=1;i<len;i++){
        int temp=array[i];
        for(j=i-1;temp<array[j]&&j>=0;j--)//注意这里要写j>=0,用于终止循环*
            array[j+1]=array[j];//向后挪位
        array[j+1]=temp;
    }
    printf("排序完成\n");
}
int main(){
	int array[10] = {4,3,10,5,6,7,1,2,8,9};
	int len = sizeof(array) / sizeof(int);
	printf("初始序列:\n");
	Print(array,len);
	InsertSort(array,len);
	printf("排序后序列:\n");
	Print(array,len);
}

希尔排序


void ShellSort(int a[],int size){
    int gap=size;//步长
    while(gap>=1){
        gap=gap/2;
        int i;
        int t,j;
        for(i=gap;i<size;i++){
            if(a[i]<a[i-gap]){
                t=a[i];
                for(j=i-gap;j>=0&&t<a[j];j-=gap){//寻找合适的点
                    a[j+gap]=a[j];//后移,插入
                }
                a[j+gap]=t;
            }
        }
    }
}

交换排序

Bubble排序

void ShellSort(int a[],int size){
    int gap=size;//步长
    while(gap>=1){
        gap=gap/2;
        int i;
        int t,j;
        for(i=gap;i<size;i++){
            if(a[i]<a[i-gap]){
                t=a[i];
                for(j=i-gap;j>=0&&t<a[j];j-=gap){//寻找合适的点
                    a[j+gap]=a[j];//后移,插入
                }
                a[j+gap]=t;
            }
        }
    }
}


快速排序

int Partition(int a[],int low,int high){
    int t=a[low];
    while(low<high){
        while(low<high&&a[high]>=t)//比中枢大不移动
            high--;
        a[low]=a[high];
        while(low<high&a[low]<=t)
            low++;
        a[high]=a[low];
    }
    a[low]=t;
    return low;//中枢最终的位置
}
void QuickSort(int a[],int low,int high){
    if(low<high){
        int p=Partition(a,low,high);
        QuickSort(a,low,p-1);
        QuickSort(a,p+1,high);
    }
}


双向起泡

奇数趟时,从前向后比较相邻的关键字,遇到逆序交换。偶数趟时,从后向前比较相邻元素的关键字,遇到逆序即交换。
void BubbleD(int a[],int n){
    int low=0;
    int high=n-1;
    bool f=true;
    int i,j;
    while(low<high&&f){
        f=false;
        for(i=low;i<high;i++){
            if(a[i]>a[i+1]){
                swap(a[i],a[i+1]);
                f=true;
            }
        }
        high--;
        for(i=high;i>low;i--){
            if(a[i]<a[i-1]){
                swap(a[i],a[i-1]);
                f=true;
            }
        }
        low++;
    }
}

线性表,元素不同,把所有奇数移动到偶数前(时间最少,辅助空间最少)

void JO(int a[],int low,int high){
    int t=a[low];
    while(low<high){
        while(low<high&&(a[high]%2==0))
            high--;
        a[low]=a[high];
        while(low<high&&(a[low]%2!=0))
            low++;
        a[high]=a[low];
    }
    a[low]=t;
}

在数组中找到第k小的数

int  KSmall(int a[],int low,int high,int k){
    int p=a[low];
    int low_t=low;
    int high_t=high;
    while(low<high){
        while(low<high&&a[high]>=p)
            high--;
        a[low]=a[high];
        while(low<high&&a[low]<=p)
            low++;
        a[high]=a[low];

    }
    a[low]=p;
    //以上为快排,以下为本次的
    if(low==k)
        return a[low];
    else if(low>k)
        return KSmall(a,low_t,low-1,k);
    else if(low<k)
        return KSmall(a,low+1,high_t,k);
}

荷兰国旗问题:设有一个劲由红、白、蓝三种颜色的条块组成的条块序列,使这些条块按红、白、蓝三种颜色的顺序排好。时间复杂度O(n)

void Hlan(int a[],int n){
    int i=0;
    int j=0;
    int k=n-1;
    while(j<=k){
        switch(a[j]){
            case 0:swap(a[j],a[i]);
                i++;
                j++;
                break;
            case 1:j++;
                 break;
            case 2:swap(a[j],a[k]);
                k--;
        }
    }
}

选择排序

简单选择排序

void SelectSort(int a[],int n){
    int min;
    int i,j;
    for(int i=0;i<n-1;i++){//注意是n-1,前n-1个排好序,最后一个自然就排好了!!
        min=i;//记录最小的元素的位置
        for(j=i+1;j<n;j++)
            if(a[j]<a[min])
                min=j;//找到最小元素的位置
            if(min!=i)
                swap(a[i],a[min]);
    }
}

归并排序

#include<bits/stdc++.h>
using namespace std;
void Print(int array[],int len){//输出函数
	for(int i=0;i<len;i++){
		printf("%d ",array[i]);
	}
	printf("\n");
}
void Merge(int a[],int low,int mid,int high){
    int b[11];
    int i,j,k;
    for(k=low;k<=high;k++){
        b[k]=a[k];
    }
    for(i=low,j=mid+1,k=i;i<=mid&&j<=high;k++){
            if(b[i]>b[j]){
                a[k]=b[j];
                j++;
            }
            else{
                a[k]=b[i];
                i++;
            }
    }
    while(i<=mid){
        a[k]=b[i];
        i++;
    }
    while(j<=high){
        a[k]=b[j];
        j++;
    }
}
void MergeSort(int a[],int low,int high){
    if(low<high){
        int mid=(low+high)/2;
        MergeSort(a,low,mid);
        MergeSort(a,mid+1,high);
        Merge(a,low,mid,high);//归并
    }
}
int main(){
	int array[11] = {48,23,39,80,73,15,8,13,40,99};
	//int len = sizeof(array) / sizeof(int);
	int len=10;
	int k=6;
	printf("初始序列:\n");
	Print(array,len);
	MergeSort(array,0,9);
	printf("排序后序列:\n");
	Print(array,len);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值