You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
p9gzyw2xs 1a9bb3a937
Update README.md
4 years ago
README.md Update README.md 4 years ago
冒泡排序 Add 冒泡排序 4 years ago
堆排序 Add 堆排序 4 years ago
归并排序 Add 归并排序 4 years ago
快速排序 Add 快速排序 4 years ago
桶排序 Add 桶排序 4 years ago

README.md

1.冒泡排序(O(n^2),n为数的个数)

#include<stdio.h>
int main()
{
	int a[100],i,j,t,n;
    scanf("%d", &n);//输入数据个数
   for(i = 1; i <= n; i ++)//输入数据
   {
  		scanf("%d", &a[i]);
   }
   for(i = 1; i < n; i ++)//冒泡排序
   	for(j = 1; j <= n - i; j ++)
    {
    	if(a[j] < a[j + 1])//比较,交换
        {
        t = a[j];
        a[j] = a[ j + 1];
        a[j + 1] = t;
        }
     for(i = 1; i <= n; i ++)//输出
     	printf("%d ", a[i]);
    }
    return 0;
}

2.桶排序(时间复杂度 O(n+m)n 为待排序的元素的个数m 为桶的个数)

include<stdio.h>
int main()
{
   int a[11],i,j,t;
   for(i = 0; i <= 10; i ++)
   		a[i]=0//初始化为0
   for(i = 0; i  <= 5; i ++)//循环输入5个数
   {
   	scanf("%d",&t);//把每一个数读到变量t中
   	a[t]++;//进行计数
   }
	for(i = 0; i <= 10; i ++)//依次判断a[0]-a[10]
    for(j = 1; j <= a[i];j++)
    	printf("%d ", i);
	return 0;
}

3.快速排序(时间复杂度O(nlogn))

#include<stdio.h>
int a[101], n;//定义成全局变量,方便在子函数中使用;
void quicksort(int left, int right)
{
	int i, j, t, temp;
   if(left > right)
   	return;
   temp = a[left];//temp中存的基准数
   i = left;
   j = right;
   while(i != j)
   {
   //顺序很重要,要先从右往左找
   	while(a[j] > temp && i < j)
    	j --;
   //再从左往又找
   	while(a[i] <= temp && i < j)
    	i ++;
    //交换两个数所在数组中的位置
		if(i < j)
        {
        	t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
	}
   //最终将基准数归位
   a[left] = a[i];
   a[i] = temp;
   quicksort(left, i - 1);//继续处理左边的,这里是一个递归的过程
   quicksort(i + 1, right);//继续处理右边的
}
int main()
{
	int j ,i, t;
    //读入数据
    scanf("%d", &n)
    for(int i = 1; i <= n; i ++)
    	scanf("%d", &a[i]);
    quicksort(1,n);
    for(int i = 1; i <= n; i ++)
    	printf("%d ",a[i]);
    return 0;
}

4.堆排序时间复杂度O(nlogn)

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e6+6;
int size1=0,h[N],n,m=0,hp[N],ph[N];
void heap_swap(int a, int b)
{
	swap(ph[hp[a]],ph[hp[b]]);
	swap(hp[a],hp[b]);
	swap(h[a],h[b]);
}
void down(int u)
{
	int t=u;
	if(u*2<=size1&&h[u*2]<h[t]) t=u*2;
	if(u*2+1<=size1&&h[u*2+1]<h[t]) t=u*2+1;
	if(u!=t)
	{
		heap_swap(u,t);
		down(t);
	}
}
void up(int u)
{
	while(u/2&&h[u/2]>h[u])
	{
		heap_swap(u/2,u);
		u/=2;
	}
}
int main()
{
	scanf("%d",&n);
	while(n--)
	{
		char op[10];
		int k,x;
		scanf("%s",op);
		if(!strcmp(op,"I"))
		{
			scanf("%d",&x);
			size1++;
			m++;
			ph[m]=size1,hp[size1]=m;
			h[size1]=x;
			up(size1);
		}
		else if(!strcmp(op,"PM"))printf("%d\n",h[1]);
		else if(!strcmp(op,"DM"))
		{
			heap_swap(1,size1);
			size1--;
			down(1);
		}
		else if(!strcmp(op,"D"))
		{
			scanf("%d",&k);
			k=ph[k];
			heap_swap(k,size1);
			size1--;
			down(k),up(k);
		}
		else
		{
			scanf("%d %d",&k,&x);
			k=ph[k];
			h[k]=x;
			down(k),up(k);
		}
	}
	return 0;
}

归并排序时间复杂度O(nlogn)

void merge(int r[],int s[],int left,int mid,int right)
{
    int i,j,k;
    i=left;
    j=mid+1;
    k=left;
    while((i<=mid)&&(j<=right))
        if(r[i]<=r[j])
        {
            s[k] = r[i];
            i++;
            k++;
        }
        else
        {
            s[k]=r[j];
            j++;
            k++;
        }
        while(i<=mid)
            s[k++]=r[i++];
        while(j<=right)
            s[k++]=r[j++];
}
void merge_sort(int r[],int s[],int left,int right)
{
    int mid;
    int t[20];
    if(left==right)
        s[left]=r[right];
    else
    {
        mid=(left+right)/2;
        merge_sort(r,t,left,mid);
        merge_sort(r,t,mid+1,right);
        merge(t,s,left,mid,right);
    }
}
int main()
{
    int a[10];
    int i;

    for(i=0;i<10;i++)
        scanf("%d",&a[i]);
    merge_sort(a,a,0,9);

    for(i=0;i<10;i++)
        printf("%d ",a[i]);
    return 0;
}