计算机等级考试二级辅导c++常用排序算法

计算机等级考试二级辅导c++常用排序算法,第1张

计算机等级考试二级辅导c++常用排序算法,第2张

//选择排序方法selectionsort (int arr [],int n)
template
void selection sort(t arr[],int n)
{
int小索引;//下标表中最小的元素
int pass,j //下标用于扫描子表
T temp;//用于交换表格元素的临时变量

//pass的范围为0 ~ n-2
for(pass = 0;Pass{
//从下标Pass开始扫描子表
small index = pass;

//j遍历整个子表arr[pass+1]到arr[n-1]
for(j = pass+1;J //如果找到更小的元素,则将位置赋给small index
If(arr[j]small index = j;
//如果smallIndex和pass不在同一个位置
/,则用arr[pass]
if(smallIndex!= pass)
{
temp = arr[pass];
arr[pass]= arr[small index];
arr[small index]= temp;
}
}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *可以在每个子表中定位最小和元素
,分别放在子表的开头和结尾。
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *实现
模板
void deselsort (tarr [],int n)
{
int小索引,大索引;//表中最小值和元素的索引
int leftpass = 0,rightpass = n-1,I,J;//subscript
T temp用于从表的左右两侧扫描子表;//用于交换元素的临时变量

While (leftPass{
//从左右两侧扫描子表
small index = left pass;
large index = right pass;
//j和我遍历整个子表ARR[left pass]~ ARR[right pass]
for(I = left pass+1;I //如果找到更小的元素,则将位置赋给small index
If(arrsmall index = I;

//如果smallIndex和leftPass不在同一个位置
/,则用arr[pass]
if (smallIndex!= left pass)
{
temp = arr[left pass];
arr[left pass]= arr[small index];
arr[small index]= temp;

}

for(j = right pass-1;j >左传球;j-)
if(arr[j]> arr[large index])
large index = j;

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » 计算机等级考试二级辅导c++常用排序算法

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情