排序

#include<bits/stdc++.h>
using namespace std;
int array[15]={0,1,9,3,5,4,6,8,7,10,2};
void swap(int *arr,int i,int j){
	int temp=arr[i];
	arr[i]=arr[j];
	arr[j]=temp;
}

void BubbleSort(int *arr,int n){
	int i,j;
	for(i=1;i<=n;++i){
		for(j=n;j>=i;--j){
			if(arr[j]<arr[j-1])
				swap(arr,j,j-1);
		}
	}
}

void SelectSort(int *arr,int n){
	int i,j,min;
	for(i=1;i<n;++i){
		min=i;
		for(j=i+1;j<=n;++j)
		{
			if(arr[j]<arr[min])
				min=j;
		}
		if(i!=min)
			swap(arr,i,min);
		// cout<<"\nThe "<<i<<" sort: ";
		// for(int i=1;i<=10;++i)
		// 	cout<<arr[i]<<" ";
	}
	cout<<endl;
}

void InsertSort(int *arr,int n){
	int i,j;
	for(i=2;i<=n;++i){
		if(arr[i]<arr[i-1])
		{
			arr[0]=arr[i];
			for(j=i-1;arr[j]>arr[0];--j)
				arr[j+1]=arr[j];
			arr[j+1]=arr[0];
		}
		// cout<<"\nThe "<<i-1<<" sort: ";
		// for(int i=1;i<=10;++i)
		// 	cout<<arr[i]<<" ";
	}
}

void ShellSort(int *arr,int n){
	int i,j;
	//int cnt=1;
	int increment = n;
	do{
		increment = increment/3+1;
		for(i=increment+1;i<=n;++i){
			if(arr[i]<arr[i-increment]){
				arr[0]=arr[i];
				for(j=i-increment;j>0&&arr[j]>arr[0];j-=increment)
					arr[j+increment]=arr[j];
				arr[j+increment]=arr[0];
			}
			// cout<<"\nThe "<<cnt++<<" sort: ";
			// for(int i=1;i<=10;++i)
			// 	cout<<arr[i]<<" ";
		}
	}while(increment>1);
}


int Partition(int *arr,int low,int high){
	int pivotkey=arr[low];
	while(low<high){
		while(low<high&&arr[high]>=pivotkey){
			high--;
		}
		swap(arr,low,high);
		while(low<high&&arr[low]<pivotkey){
			low++;
		}
		swap(arr,low,high);
	}
	return low;
}
void QuickSort(int *arr,int low,int high){
	int pivot;
	if(low<high){
		pivot=Partition(arr,low,high);
		// cout<<"\nThe next sort: ";
		// for(int i=1;i<=10;++i)
		// 	cout<<arr[i]<<" ";
		QuickSort(arr,low,pivot-1);
		QuickSort(arr,pivot+1,high);
	}
}


void HeapAdjust(int *arr,int s,int m){
	//调整arr[s...m]使其构成一个大顶堆
	int temp,j;
	temp = arr[s];
	for(j=s*2;j<=m;j*=2){
		if(j<m&&arr[j]<arr[j+1])
			j++;
		if(arr[j]<temp)break;
		arr[s]=arr[j];
		s=j;
	}
	arr[s]=temp;
}
void HeapSort(int* arr,int n){
	int i;
	for(i=n/2;i>0;i--)
		HeapAdjust(arr,i,n);

	for(i=n;i>1;--i){
		swap(arr,1,i);
		cout<<"\nThe next sort: ";
		for(int j=1;j<=10;++j)
			cout<<arr[j]<<" ";
		HeapAdjust(arr,1,i-1);
	}
}


int main(){
	cout<<"Before sort: ";
	for(int i=1;i<=10;++i)
		cout<<array[i]<<" ";
	HeapSort(array,10);
	cout<<"\nAfter sort: ";
	for(int i=1;i<=10;++i)
		cout<<array[i]<<" ";
	return 0;
}

冒泡 快排 堆排 插入 选择 希尔

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值