更新时间:2022-09-21 16:26:33
常用的排序
1、插入排序
插入排序的基本思想是:每次将一个待排序的记录按其关键字大小插入前面已经排好序的子记录中的适当位置,直到全部记录插入完成为止。常用的插入排序有直接插入排序和希尔排序。
2、归并排序
归并排序(Merge Sort)是利用“归并”技术来进行排序。归并排序是将若干个已经排序的序列合并成一个有序的序列。
3、冒泡排序
数字之间的两两比较,并且根据结果进行位置调整.
C++ Code:
#include "stdafx.h" #include <iostream> using namespace std;
void BubbleSort( int * pData, int count)
{ int iTemp;
for ( int i = 1; i < count; i++)
{
for ( int j = count - 1; j >= i; j --)
{
if (pData[j] < pData[j-1])
{
iTemp = pData[j-1];
pData[j-1] = pData[j];
pData[j] = iTemp;
}
}
}
} int _tmain( int argc, _TCHAR* argv[])
{ printf( "---冒泡排序---" );
int data[] = {11, 41, 52,63, 23,45,32 };
BubbleSort(data,7);
for ( int i = 0; i < 7; i++)
{
cout<< data[i] << " " ;
}
return 0;
} |
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/03/27/1997238.html,如需转载请自行联系原作者