排序算法之一: 基本的选择,插入,冒泡排序

1013人阅读 评论(1) 收藏 举报

三种排序入门级别算法。








#include "stdafx.h"
 #include <Windows.h>
 #include <iostream>
 
 // 打印数据
 void ShowNums(int colNum, int totalNum, int *pData);
 
 // 创建随机数据
 void ProduceRandomNum(int num, int range_min, int range_max, int* &pData);
 
 // 插入排序
 void InsertionSort(int* &pData, int num);
 
 // 选择排序
 void SelectionSort(int* &pData, int num);
 
 // 冒泡排序
 void BubbleSort(int* &pData, int num);
 
 // 打乱排序
 void Shuffle(int* &pData, int num, int seed);
 
 
 
 
 void SelectionSort(int* &pData, int num)
 {
 	int minNum;
 	int minNumNO;
 	int temp;
 
 	for (int i = 0; i < num - 1; i ++)
 	{
 		minNum = pData[i];
 		minNumNO = i;
 		for (int j = i; j < num; j ++)
 		{
 			minNum = minNum <= pData[j] ? minNum : pData[j];
 			if (minNum == pData[j])
 			{
 				minNumNO = j;
 			}
 		}
 		temp = pData[i];
 		pData[i] = minNum;
 		pData[minNumNO] = temp;
 	}
 }
 
 void InsertionSort(int* &pData, int num)
 {
 	int nCur;
 	int nTmp;
 	int nTmp2;
 	bool bGet = false;
 	for ( int  i = 1; i < num; i ++)
 	{	
 		nCur  = pData[i];
 		bGet = false;
 		for (int j = 0; j < i; j ++)
 		{
 			if (!bGet && nCur < pData[j])
 			{
 				nTmp = pData[j];
 				pData[j] = nCur;
 				bGet = true;
 			}
 			if (bGet == true)
 			{
 				nTmp2 = pData[j + 1];
 				pData[j + 1] = nTmp;
 				nTmp = nTmp2;
 			}
 		}
 	}
 }
 
 void BubbleSort(int* &pData, int num)
 {
 	int nCur;
 	int nNex;
 	bool bNotChange = true;
 	for (int i = 0; i < num; i ++)
 	{
 		bNotChange = true;
 		for (int j = 1; j < num - i; j ++)
 		{
 			if (pData[j - 1] > pData[j])
 			{
 				nNex = pData[j];
 				pData[j] = pData[j - 1];
 				pData[j - 1] = nNex;
 				bNotChange = false;
 			}
 		}
 		if (bNotChange)
 		{
 			break;
 		}
 	}
 }
 
 void Shuffle(int* &pData, int num, int seed)
 {
 	srand(seed);
 	int nTemp;
 	for (int i = 0; i < num; i ++)
 	{
 		int j = (double) rand() / (RAND_MAX + 1) * num;
 		nTemp = pData[j];
 		pData[j] = pData[i];
 		pData[i] = nTemp;
 	}
 }
 
 
 void ShowNums(int colNum, int totalNum, int *pData)
 {
 	std::string str;
 	std::string strTmp;
 	char cTmp[256];
 	for (int i = 0; i < totalNum; i ++)
 	{
 		sprintf(cTmp, "%d\t", pData[i]);
 		strTmp = cTmp;
 		str += strTmp;
 		if ((i + 1) % colNum == 0)
 		{
 			str += "\n";
 		}
 	}
 	str += "\n";
 	std::cout<<str.c_str()<<std::endl;
 }
 
 void ProduceRandomNum(int num, int range_min, int range_max, int* &pData)
 {
 	int n = GetTickCount();
 	srand(n);
 	for (int i = 0; i < num; i ++)
 	{
 		pData[i] = (double)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min;
 	}
 }
 
 
 int _tmain(int argc, _TCHAR* argv[])
 {
 	int nTotalNum = 5;
 	int *pData = new int[nTotalNum];
 
 	ProduceRandomNum(nTotalNum, 0, 1000, pData);
 	std::cout<<"原始数据:"<<std::endl;
 	ShowNums(10, nTotalNum, pData);
 
 	SelectionSort(pData, nTotalNum);
 	std::cout<<"选择排序"<<std::endl;
 	ShowNums(10, nTotalNum, pData);
 
 	Shuffle(pData, nTotalNum, GetTickCount() + 1);
 	std::cout<<"随机排序"<<std::endl;
 	ShowNums(10, nTotalNum, pData);
 
 	InsertionSort(pData, nTotalNum);
 	std::cout<<"插入排序"<<std::endl;
 	ShowNums(10, nTotalNum, pData);
 
 	Shuffle(pData, nTotalNum, GetTickCount() + 2);
 	std::cout<<"随机排序"<<std::endl;
 	ShowNums(10, nTotalNum, pData);
 
 	BubbleSort(pData, nTotalNum);
 	std::cout<<"冒泡排序"<<std::endl;
 	ShowNums(10, nTotalNum, pData);
 
 
 	if (pData)
 	{
 		delete []pData;
 		pData = NULL;
 	}
 	return 0;
 }
 
 //原始数据:
 //587     269     210     260     508
 //
 //选择排序
 //210     260     269     508     587
 //
 //随机排序
 //269     210     508     587     260
 //
 //插入排序
 //210     260     269     508     587
 //
 //随机排序
 //587     269     508     260     210
 //
 //冒泡排序
 //210     260     269     508     587
 //
 //请按任意键继续. . .
 




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:171868次
    • 积分:3202
    • 等级:
    • 排名:第11785名
    • 原创:95篇
    • 转载:50篇
    • 译文:10篇
    • 评论:36条
    博客专栏
    文章分类
    站长统计