C++ 排序

冒泡排序

void bubblesort(int a[],int n)
{
	for(int i=0;i<n-1;i++)
		{
			int temp=0;
			for(int j=1;j<n-i;j++)
			{
				if(a[j-1]>a[j])
				{
					swap(a[j-1],a[j]);
					flag=1;
				}
			}
			if(flag==0)
			return;
		}
}

插入排序

//顺序表
void insertsort(int *a[],int n)
{
	for(int i=1;i<n;i++)
	{
		int num=a[i];
		int j=i-1;
		while(j>0&&num<a[j])
			{
				a[j+1]=a[j];
				j--;
			}
		a[j+1]=num;	
	}
}
//链表
void insertsort(LinkList &L)
{
	LNode *p=L->next->next;
	L->next->next=NULL;
	while(p)
	{
		LNode *head=L;
		while(head->next!=NULL&&head->next->data<p->data)
			head=head->next;
		LNode *q=p->next;
		p->next=head->next;
		head->next=p;
		p=q;	
	}
}

折半插入排序

void binaruinsertsort(inr a[],int n)
{
	for(int i=1;i<n;i++)
	{
		int num=a[i];
		int low=0,high=i-1;
		while(low<=high)
		{
			int mid=(low+high)/2;
			if(num<a[mid])
				high=mid-1;
			else
				low=mid+1;	
		}
		for(int j=i-1;j>=high+1;j--)
			a[j+1]=a[j];
		a[high+1]=num;	
	}
}

快速排序

int partition(int a[],int low;int high)
{
	while(low<high)
	{
		int pivot=a[low];
		while(low<high&&a[high]>pivot)
			high--;
		a[low]=a[high];
		while(low<high&&a[low]<pivot)
			low++;
		a[high]=a[low];		
	}
	a[low]=pivot;
	return low;
}
void quicksort(int a[],int low,int high)
{
	while(low<high)
	{
		int p=partition(a,low,high);
		quicksort(a,low,p-1);
		quicksort(a,p+1,high);
	}
}

选择排序

void selectsort(int a[],int n)
{
	for(int i=0;i<n-1;i++)
	{
		int min=i;
		for(int j=i+1;j<nj++)
		{
			if(a[j]<a[min])
				min=j;
		}
	if(min!=i)
		swap(a[min],a[i]);	
	}
}

归并排序

void mergesort(int a[],int low,int high)
{
	if(low==high)
		return;
	int mid=(low+high)/2;
	mergesort(a,low,mid);
	mergesort(a,mid+1,high);
	int c[10];
	int i=low,j=mid+1,k=i;
	while (i<=mid&&j<=high)
	{
		if(a[i]<=a[j])
			c[k++]=a[i++];
		else
			c[k++]=a[j++];
	}
	while(i<=mid)
		c[k++]=a[i++];
	while(j<=high)
		c[k++]=a[j++];
	for(int i=low;i<=high;i++)
		a[i]=c[i];
}

运行代码


#include "stdafx.h"
#include<iostream>
using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode,*LinkList;
void display(LNode *l);
void print(int a[],int n);
void creat(LinkList &l)
{
	l=new LNode;
	LNode *p,*r=l;
	int a[9]={9,7,3,1,6,4,8,2,5};
	for(int i=0;i<9;i++)
	{
		p=new LNode;
		p->data=a[i];
		p->next=NULL;
		r->next=p;
		r=p;
	}
	cout<<"创建链表:";
	display(l);
}
void InsertSort(LinkList &l)
{
	LNode *p=l->next->next;
	l->next->next=NULL;
	while(p)
	{
		LNode *h=l;
		while(h->next!=NULL&&h->next->data<p->data)
			h=h->next;
		LNode *q=p->next;
		p->next=h->next;
		h->next=p;
		p=q;
	}
	cout<<"插入排序:";
	display(l);
}
void insertsort(int a[],int n)
{
	for(int i=1;i<n;i++)
	{
		int num=a[i];
		int j=i-1;
		while(j>=0&&num<a[j])
		{
			a[j+1]=a[j];
			j--;
		}
		a[j+1]=num;
	}
	cout<<"插入排序:";
	print(a,n);
}
void BinarySrot(int a[],int n)
{
	for(int i=1;i<n;i++)
	{
		int num=a[i];
		int low=0,high=i-1;
		while (low<=high)
		{
			int mid=(low+high)/2;
			if(num<a[mid])
				high=mid-1;
			else
				low=mid+1;
		}
		for(int j=i-1;j>=high+1;j--)
			a[j+1]=a[j];
		a[high+1]=num;
	}
	cout<<"折半排序:";
	print(a,n);
}
void bubblesort(int a[],int n)
{
	for(int i=0;i<n-1;i++)
	{
		int flag=0;
		for(int j=1;j<n-i;j++)
			if(a[j-1]>a[j])
			{
				swap(a[j-1],a[j]);
				flag=1;
			}
		if(flag==0)
		{
			cout<<"冒泡排序:";
			print(a,n);
			return;
		}
	}
}
int partition(int a[],int low,int high)
{
	int pivot=a[low];
	while(low<high)
	{
		while (low<high&&a[high]>=pivot)
			high--;
		a[low]=a[high];
		while(low<high&&a[low]<=pivot)
			low++;
		a[high]=a[low];
	}
	a[low]=pivot;
	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 selectsort(int a[],int n)
{
	for(int i=0;i<n-1;i++)
	{
		int min=i;
		for(int j=i+1;j<n;j++)
		{
			if(a[j]<a[min])
				min=j;
		}
		if(min!=i)
			swap(a[min],a[i]);
	}
	cout<<"选择排序:";
	print(a,n);
}
void mergesort(int a[],int low,int high)
{
	if(low==high)
		return;
	int mid=(low+high)/2;
	mergesort(a,low,mid);
	mergesort(a,mid+1,high);
	int c[9];
	int i=low,j=mid+1,k=i;
	while (i<=mid&&j<=high)
	{
		if(a[i]<=a[j])
			c[k++]=a[i++];
		else
			c[k++]=a[j++];
	}
	while(i<=mid)
		c[k++]=a[i++];
	while(j<=high)
		c[k++]=a[j++];
	for(int i=low;i<=high;i++)
		a[i]=c[i];
}
int _tmain(int argc, _TCHAR* argv[])
{
	LinkList l;int a[9]={9,7,3,1,6,4,8,2,5};
	creat(l);
	InsertSort(l);
	cout<<endl<<"初始数组:";
	print(a,9);
	insertsort(a,9);
	BinarySrot(a,9);
	bubblesort(a,9);
	selectsort(a,9);
	quicksort(a,0,8);
	cout<<"快速排序:";
	print(a,9);
	mergesort(a,0,8);
	cout<<"归并排序:";
	print(a,9);
	system("pause");
	return 0;
}
void display(LNode *l)
{
	LNode *p=l->next;
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl<<endl;
}
void print(int a[],int n)
{
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
	cout<<endl<<endl;
}

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值