資源簡(jiǎn)介
本文件是7種常用排序算法的實(shí)現(xiàn)(C++),包括冒泡排序、選擇排序、直接插入排序、希爾排序、堆排序、歸并排序以及快速排序。代碼詳細(xì)有注釋且有測(cè)試用例。
代碼片段和文件信息
#include
using?namespace?std;
#include
/**
*?冒泡排序初始程序
*?@param?[in]?a:數(shù)組名
*?@param?[in]?n:數(shù)組大小
*?@return
*/?
void?BubbleSort1(int?a[]?int?n)??
{??
for?(int?i?=?0;?i? for?(int?j?=?1;?j? if?(a[j?-?1]?>?a[j])??//若順序不合適則交換數(shù)據(jù)
swap(a[j?-?1]?a[j]);??
}??
/**
*?冒泡排序改進(jìn)程序
*?@param?[in]?a:數(shù)組名
*?@param?[in]?n:數(shù)組大小
*?@return
*/??
void?BubbleSort2(int?a[]?int?n)??
{????
bool?bSwap=true;??
while?(bSwap)??//bSwap為false退出循環(huán)
{??
bSwap?=?false;??
for?(int?i?=?1;?i? {
if?(a[i?-?1]?>?a[i])??
{??
swap(a[i?-?1]?a[i]);??
bSwap?=?true;??//有數(shù)據(jù)交換則置bSwap為true
}??
}
n--;??
}??
}?
/**
*?選擇排序程序
*?@param?[in]?a:數(shù)組名
*?@param?[in]?n:數(shù)組大小
*?@return
*/?
void?Selectsort(int?a[]
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????目錄???????????0??2014-04-11?22:45??7種排序算法(C++)\
?????目錄???????????0??2014-04-11?22:44??7種排序算法(C++)\Sort\
?????目錄???????????0??2014-04-11?22:44??7種排序算法(C++)\Sort\Debug\
?????文件????????6258??2014-04-11?22:44??7種排序算法(C++)\Sort\Debug\BuildLog.htm
?????文件?????????663??2014-04-11?22:31??7種排序算法(C++)\Sort\Debug\Sort.exe.em
?????文件?????????728??2014-04-11?22:31??7種排序算法(C++)\Sort\Debug\Sort.exe.em
?????文件?????????621??2014-04-11?22:44??7種排序算法(C++)\Sort\Debug\Sort.exe.intermediate.manifest
?????文件??????????65??2014-04-11?22:44??7種排序算法(C++)\Sort\Debug\mt.dep
?????文件???????60468??2014-04-11?22:44??7種排序算法(C++)\Sort\Debug\sort.obj
?????文件??????175104??2014-04-11?22:44??7種排序算法(C++)\Sort\Debug\vc90.idb
?????文件??????217088??2014-04-11?22:44??7種排序算法(C++)\Sort\Debug\vc90.pdb
?????文件????????3910??2014-04-08?15:49??7種排序算法(C++)\Sort\Sort.vcproj
?????文件????????1409??2014-04-11?22:45??7種排序算法(C++)\Sort\Sort.vcproj.mxq-PC.mxq.user
?????文件????????8308??2014-04-11?22:44??7種排序算法(C++)\Sort\sort.cpp
?????文件?????1674240??2014-04-11?22:45??7種排序算法(C++)\Sort.ncb
?????文件?????????878??2014-04-08?15:47??7種排序算法(C++)\Sort.sln
?????文件???????15360??2014-04-11?22:45??7種排序算法(C++)\Sort.suo
評(píng)論
共有 條評(píng)論