贴一个数据结构老师布置的作业(各种排序) c 语言实现

作业要求是能实现直接插入排序,折半插入排序,希尔排序,冒泡排序,快速排序,直接选择排序,堆排序,归并排序,基数排序

那天弄了大半天的,调试什么的 ·····最后搞起了


// 12.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
#include <iostream>
#include <algorithm> 
using namespace std;

int a[15];            //排序数组
int len = 15 ;		  //数组长度

void insertsort(int *a );				 //插入排序函数
void Binsertsort(int *a );					//折半插入排序函数
void bubble_sort(int *a);					//冒泡排序
void quick_sort(int *a , int low , int high) ;		//快速排序
int  one_quick_sort(int *a , int low , int high) ;   //一趟快速排序
void select_sort(int *a);				//直接选择排序
void merge_sort(int *a , int low , int high);		//归并排序
void msort(int *a , int low , int high,int mid);	//归并排序调用函数
void head_sort(int *a);								//堆排序函数
void head_adgust(int *a , int low , int high);		//	堆排序调用函数
int  max_select_sort(int *a, int t);			//选择最大数
void shell_insert(int *a , int dk);				//希尔排序调用函数
void shell_sort(int *a);						//希尔排序函数
void dadix_sort(int *a);						//技术排序函数
int cmp1(int a,int b);							//sort()函数里面的比较函数
int cmp2(int a,int b);							//sort()函数里面的比较函数
void rand_sort(int *a) ;			   	 //随机产生函数
void display(int *a) ;					  //打印数组

int main(int argc, char* argv[])
{
	srand(unsigned(time(NULL)));			//产生随即种子

	cout<<"      *******  1.插入排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	insertsort(a);
	display(a);
	cout<<endl;

	cout<<"      *******  2.折半插入排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	Binsertsort(a);
	display(a);
	cout<<endl;

	cout<<"      *******  3.冒泡排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	bubble_sort(a);
	display(a);
	cout<<endl;

	cout<<"      *******  4.快速排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	quick_sort(a, 0 ,len-1);
	display(a);
	cout<<endl;
	
	cout<<"      *******  5.选择排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的函数"<<endl;
	select_sort(a);
	display(a);
	cout<<endl;
	
	cout<<"      *******  6.归并排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	merge_sort(a , 0 , len);
	display(a);
	cout<<endl;

	cout<<"      *******  7.堆排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	head_sort(a );
	display(a);
	cout<<endl;

	cout<<"      *******  8.希尔排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	shell_sort(a);
	display(a);
	cout<<endl;
	
	cout<<"      *******  9.基数排序*********"<<endl;
	cout<<"随机产生数据:"<<endl;
	rand_sort(a);
	display(a);
	cout<<"排序之后的数据"<<endl;
	dadix_sort(a);
	display(a);
	cout<<endl;
	return 0;
}
void insertsort(int *a )
{
	int temp;
	for (int i = 1 ; i < len ; ++i )
	{
		if (a[i - 1] > a[i])
		{
			temp = a[i];
			a[i] = a[i-1];
			for (int j = i - 1 ; temp < a[j] ; --j )
			{
				a[j+1] = a[j];
			}
			a[j+1] = temp;
		}
	}
}
void Binsertsort(int *a )   //折半插入排序函数
{
	int temp;
	int low,high,mid;
	for (int i = 1 ; i < len ; ++i )
	{
		temp = a[i];
		low = 0 ; 
		high = i- 1 ;
		while (low <= high)
		{
			mid = (low + high ) / 2 ;
			if ( temp < a[mid])
			{
				high = mid -1 ;
			}
			else
				low = mid+1;
		}
		for (int j = i - 1 ; j > high  ; --j )
		{
			a[j+1] = a[j];
		}
		a[j+1] = temp ;
		
	}
}
void bubble_sort(int *a)      //冒泡排序
{
	int temp;
	for (int i = 1 ; i < len   ; ++i )
	{
		for (int j = 0  ; j < len - i  ; ++j  )
		{
			if (a[j] > a[j+1] )
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp ;
			}
		}
	}
}
void quick_sort(int *a , int low , int high) 		//快速排序
{
	if (low < high )
	{
		int mid;
		mid = one_quick_sort(a , low , high );
		quick_sort(a , low , mid - 1 );
		quick_sort(a , mid+1 , high );
	}
}
int one_quick_sort(int *a , int low , int high)    //一趟快速排序
{
	int temp;
	while (low < high )
	{
		while ( a[low ] <= a[high] && low < high )
		{
			--high;
		}
		temp = a[low];
		a[low] = a[high];
		a[high] = temp ;
		while (a[low] <= a[high] && low <high )
		{
			++low;
		}
		temp = a[high] ;
		a[high] = a[low];
		a[low] = temp;
	}
	return low;
}
void select_sort(int *a)				//直接选择排序
{
	int n,temp;
	int t = len - 1 ;
	for (int j = len - 1 ; j >= 0 ; --j,--t )
	{
		n = max_select_sort(a , t);
		if (j != n )
		{
			temp = a[n];
			a[n] = a[j];
			a[j] = temp ;
		}
	}
}
int  max_select_sort(int *a, int t)			//选择最大数
{
	int temp = a[0];
	int flag = 0 ;
	for (int i = 0 ; i <= t; ++i )
	{
		if (a[i] > temp )
		{
			temp = a[i];
			flag = i ;
		}
	}
	return flag;
}
void merge_sort(int *a , int low , int high)		//归并排序
{
	if (low < high)
	{
		int mid = (low + high) / 2 ;
		merge_sort(a , low , mid);
		merge_sort(a , mid + 1 , high);
		msort(a , low , high , mid);
	}
}
void msort(int *a , int low , int high,int mid)	//归并排序调用函数
{
	int i = low,j = mid+1,*b,r = 0;
	b = new int[high - low];
	while(i <= mid && j <= high)
	{
		if (a[i] <= a[j])
		{
			b[r] = a[i];
			++r ; 
			++i;
		}
		else
		{
			b[r] = a[j];
			++j;
			++r;
		}
	}
	while (i <= mid )
	{
		b[r] = a[i];
		++r;
		++i ;
	}
	while (j <= high)
	{
		b[r] = a[j];
		++j;
		++r ;
	}
	for (i = low ,j = 0 ; i <= high ; ++i ,++j )
	{
		a[i] = b[j];
	}
}
void head_sort(int *a)          //推排序
{
	int temp ;
	for (int i = len / 2 - 1 ; i>= 0  ; --i )
	{
		head_adgust(a , i , len);
	}
	for ( i = len - 1 ; i >= 0 ; --i )
	{
		temp = a[0];
		a[0] = a[i];
		a[i] = temp;
		head_adgust(a , 0 , i - 1 );
	}
}
void head_adgust(int *a , int low , int high)    //调整的最大堆
{
	int temp ; 
	for (int i = 2 * low + 1 ; i < high ; i = i * 2 +1  )
	{
		if (a[i] < a[i+1])
		{
			++i;
		}
		if (a[low] > a[i])
		{
			break;
		}
		else
		{
			temp = a[low];
			a[low] = a[i];
			a[i] = temp;
			low = i ;
		}
	}
}
void shell_insert(int *a , int dk)        希尔插入函数
{
	int temp;
	for (int i = dk  ; i < len ; ++i )
	{
		if (a[i] < a[i-dk])
		{
			temp  = a[i];
			for (int j = i - dk ; j >= 0 && temp < a[j] ; j = j-dk )
			{
				a[j+dk] = a[j];	
			}
			a[j+dk] = temp ;
		}
	}
}
void shell_sort(int *a)       //希尔排序     
{
	int dks[3] = { 4 ,3, 1};     //定义步长
	for (int i = 0 ; i < 3 ; ++i )
	{
		shell_insert(a , dks[i]);
	}
}
void dadix_sort(int *a)       //基数排序 
{
	sort(a , a + 15 , cmp2);
	sort(a , a + 15 , cmp1);
}
int cmp1(int a,int b)       //个位数比较函数
{
	if (a / 10 < b /10)
	{
		return 1;
	}
	return 0;
}
int cmp2(int a,int b)      //十位数比较函数
{
	if (a % 10 < b % 10 )
	{
		return 1;
	}
	return 0;
}
void rand_sort(int *a)     //产生随机数据函数
{
	for (int i = 0 ; i < len ; ++i )
	{
		a[i] = rand()%100;
	}
}
void display(int *a)      //打印排序好之后的函数
{
	for (int i = 0 ; i < len ; ++i )
	{
		cout<<a[i] << "  ";
	}
	cout<<endl;
}

最后发现  其实STL用起来真的不错,以前学习的时候没怎么注意,但是现在感觉 却是很爽!!特别是基数排序(当然只有它用了STL),就几行代码



你可能感兴趣的:(数据结构,c,insert,语言,merge,作业)