排序算法

1.冒泡排序---bubble_sort

   bubble.h 头文件

#ifndef _BUBBLE_H_
#define _BUBBLE_H_

void bubble_sort(int *data,int count);

#endif
    bubble.cpp 源文件
#include "Bubble.h"
/*两数进行比较交换*/
void compare_exchange(int *lesser,int *greater)
{
	int temp = 0;

	if (*lesser > *greater)
	{
		temp = *lesser;

		*lesser = *greater;

		*greater = temp;
	}
}
/*冒泡排序:相邻的两数依次比较,将最大值移动到最右侧*/
void bubble_sort(int *data,int count)
{
	int i = 0,j = 0;

	for (i = 0;i < count - 1;i++)
	{
		for (j = 0;j < (count - 1 - i);j++)
		{
			compare_exchange(&data[j],&data[j+1]);
		}
	}
}

2.直接插入排序---direct_insert_sort

   direct.h头文件

#ifndef _DIRECT_H_
#define _DIRECT_H_

void direct_insert_sort(int *data,int count);

#endif
    direct.cpp 源文件
#include "direct.h"

/*直接插入排序*/
void direct_insert_sort(int *data,int count)
{
	int i = 0,j = 0;
	int temp = 0;
	//算法不够优,无效操作较多,时间
#if 0
	if(count == 0 || count == 1)
	{
		return;
	}

	compare_exchange(&data[0],&data[1]);

	for (i = 2;i < count;i++)
	{
		j = i;

		while (j != 0)
		{
			compare_exchange(&data[j-1],&data[j]);

			j--;
		}
	}
#endif
for (i = 1;i < count;i++)
	{
		temp = data[i];

		j = i - 1;

		while (temp < data[j] && j >= 0)
		{
			data[j+1] = data[j];   //数据右移

			j--;
		}

		data[j+1] = temp;         //插入数据
	}
}

3.快速排序---quick_sort

    quick.h 头文件

#ifndef _QUICK_H_
#define _QUICK_H_

void quick_sort(int *data,int count);

#endif 
      quick.cpp 源文件

/*快速排序的分组操作*/
int quick_sort_grouping(int BaseVal,int *data,int start,int end)
{
	int i = 0,j = 0;

	int BasePos = start;

	int temp = 0,flag = 0;

	i = start;
	j = end-1;

	while (i < j)
	{
		//查找小于基准值的数据坐标j
		while (i < j && BaseVal < data[j])
			j--;

		//小于基准值的数据放到 i 位置
		if (i < j)
		{
			data[i] = data[j];
			i++;
		}

		//查找大于基准值的数据坐标 i
		while(i < j&& data[i] < BaseVal)
			i++;

		//大于基准值得数据放到 j 位置 
		if (i < j)
		{
			data[j] = data[i];
			j--;
		}
	}

	data[i] = BaseVal;

	if (start < i)
	{
		BaseVal = data[start];
		quick_sort_grouping(BaseVal,data,start,i);
	}
	if (i < end)
	{
		BaseVal = data[i + 1];
		quick_sort_grouping(BaseVal,data,i+1,end);
	}
#if 0
	int LeftStart = 0,LeftEnd = 0;
	int RightStart = 0,RightEnd = 0;

	if (flag == 2)
	{
		return flag;
	}

	for (i = start;i < end;i++)
	{
		//大于基准值的不动;小于等于基准值的放到基准值左侧
		if (BaseVal > data[i])
		{
			temp = data[i];
			
			//基准值以及大于其的数值右移
			j = i;
			
			while (j > BasePos)
			{
				data[j] = data[j-1];

				j--;
			}

			//小于等于基准值的放到其位置
			data[BasePos] = temp;

			BasePos++;
		}
	}

	//左侧递归
	LeftStart = start;
	LeftEnd = BasePos;
	
	BaseVal = data[LeftStart];
	if(LeftStart < LeftEnd)
	{
		flag = quick_sort_grouping(BaseVal,data,LeftStart,LeftEnd);
	}
	else
	{
		flag++;
	}

	//右侧递归
	RightStart = BasePos+1;
	RightEnd = end;
	BaseVal = data[RightStart];
	if(RightStart < RightEnd)
	{
		flag = quick_sort_grouping(BaseVal,data,RightStart,RightEnd);
	}
	else
	{
		flag++;
	}
#endif
	return flag;
}
/*快速排序:第一个数据做基准,分其他数据为两组,小于等于基准以及大于等于基准;
                两组分别重复以上操作*/
void quick_sort(int *data,int count)
{
	int i = 0;

	int BasePos = 0;
	int BaseVal = data[BasePos];

	int start = BasePos;
	int end = count;

	int flag = 0;

	flag = quick_sort_grouping(BaseVal,data,start,end);
}

4.选择排序---select_sort

   select.h 头文件

#ifndef _SELECT_H_
#define _SELECT_H_

void select_sort(int *data,int count);

#endif
     select.cpp 源文件

#include "Select.h"

/*两数进行比较交换*/
void compare_exchange1(int *lesser,int *greater)
{
	int temp = 0;

	if (*lesser > *greater)
	{
		temp = *lesser;

		*lesser = *greater;

		*greater = temp;
	}
}
/*选择排序:在待排序的数据中选出最小的放在左端*/
void select_sort(int *data,int count)
{
	int i = 0,j = 0;

	for (i = 0;i < count;i++)
	{
		for (j = i+1;j < count;j++)
		{
			compare_exchange1(&data[i],&data[j]);
		}
	}
}
5. 希尔排序---shell_sort

    shell.h 头文件

#ifndef _SHELL_H_
#define _SHELL_H_

void shell_sort_arith(int *data,int count);

#endif
      shell.cpp 源文件

#include "Shell.h"

/*希尔排序:按固定增量(n/2)分组,同组直接插入排序*/
void shell_sort_arith(int *data,int count)
{
	int d = count/2;

	int temp = 0,i = 0,j = 0;

	while(d >= 0)
	{
		//直接插入排序
		for (i = d + 1;i < count;i++)
		{
			temp = data[i];

			j = i - 1;

			while (temp < data[j]&&j >= d)
			{
				data[j+1] = data[j];

				j--;
			}

			data[j+1] = temp;
		}

		if (0 == d)
		{
			break;
		}

		//分组
		d = d/2;
	}

}


6. 排序主函数 ---main

    main_sort.h 头文件

#ifndef _MAIN_SORT_H_
#define _MAIN_SORT_H_

//#include "stdafx.h"
#include 
//#include
//using namespace std;


#include "Direct.h"
#include "Shell.h"
#include "Bubble.h"
#include "Quick.h"
#include "Select.h"

//进行排序的数字个数
#define sort_data_num  10
//排序算法的个数
#define sort_arith_num  5

/*排序算法的接口函数*/
void (*sort_fun[sort_arith_num])(int *data,int count) = 
{
	direct_insert_sort,shell_sort_arith,bubble_sort,quick_sort,select_sort
};


#endif
      main_sort.cpp 源文件

#include "main_sort.h"

int main(void)
{
	int data[sort_data_num] = {10,5,85,7,65,14,20,25,15,55};

	int i = 0;

	int sort_arith = 0;

	char SortStr[sort_arith_num][20] = {"直接插入排序","希尔排序","冒泡排序","快速排序","选择排序"};
#if 0
	printf("请输入10个数据: \n");

	for (i = 0;i < sort_data_num;i++)
	{
		scanf("%d",&data[i]);
	}
#endif 
	//输出排序前数据
	printf("排序前数据为: \n");

	for (i = 0;i < sort_data_num;i++)
	{
		printf("%d  ",data[i]);
	}

	printf("\n");

	//clock_t StartTime = clock();

	//使用某种排序算法进行排序
	sort_arith = 4;
	sort_fun[sort_arith](data,sort_data_num);
	
	//clock_t EndTime = clock();

	//输出排序后的数据
	printf("%s, ",SortStr[sort_arith]);
	printf("按从小到大排序后数据为: \n");

	for (i = 0;i < sort_data_num;i++)
	{
		printf("%d  ",data[i]);
	}

	printf("\n");

	//printf("Execution Time: %f\n",(EndTime - StartTime)/(double)CLOCKS_PER_SEC );

	return 0;
}








  

你可能感兴趣的:(C语言,排序算法,插入排序,冒泡排序)