选择按降序排序 [英] Selection sort in descending order
本文介绍了选择按降序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<iostream.h>
(main) void
{
int i, j, first, temp;
int numLength = num.length( );
for (i= numLength - 1; i > 0; i--)
{
first = 0;
for (j=1; j<=i; j++)
{
if (num[j] < num[first])
first = j;
}
temp = num[first];
num[first] = num[i];
num[i] = temp;
}
return;
}
[edit]已添加代码块,HTML编码 - OriginalGriff [/ edit]
[edit]Code block added, HTML encoded - OriginalGriff[/edit]
推荐答案
你的代码不会编译,即使你使用的是古老的编译器(为什么你不使用更现代的C ++
编译器?)。
你的意思是:
Your code won''t compile, even on the ancient compiler you are using (why don''t you use a more modernC++
compiler?).
You probably mean:
#include <iostream.h>
// the selection sort function
void sort(int num[], int numLength);
// usage sample
int main()
{
int v[] = {0,7,-5, 10000, 45};
const int SIZE = sizeof(v)/sizeof(v[0]);
sort(v, SIZE);
for (int i=0; i<SIZE; i++)
cout << v[i] << " ";
cout << endl;
}
// sort implementation
void sort(int num[], int numLength)
{
int i, j, first, temp;
if ( numLength < 2) return;
for (i = numLength - 1; i > 1; i--)
{
first = 0;
for (j=1; j<=i; j++)
{
if (num[j] < num[first])
first = j;
}
temp = num[first];
num[first] = num[i];
num[i] = temp;
}
看起来像是我的作业。但谷歌搜索会列出你的结果。
检查这里 [ ^ ]
Looks like a homework to me. but google search would list your result.
Check here[^]
这根本不是问题。我要说的一件事是,iostream.h不是C ++。只包括没有.h的iostream。除此之外,这是一个代码转储。如果您有问题,请编辑此帖子并提出问题。如果没有,它将被删除。
This is not a question at all. The one thing I will say is, iostream.h is not C++. Just include iostream without the .h. Apart from that, this is a code dump. If you have a question, edit this post, and ask it. If not, it will be deleted.
这篇关于选择按降序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文