我必须跟踪此选择排序中的所有比较,但是当我这样做时,仅返回 1 作为 1000 列表中的值。我不确定我是否正确实现了它,但我确定我正确放置了比较计数/ . 通常,选择排序具有固定数量的关键比较,但我们的讲师禁止使用公式来跟踪它们。我很好奇为什么这个输出不断返回:
comp = 1
swap = 1
template <class elemType>
void selectionSort(elemType list[], int length)
{
int loc, minIndex;
int first;
int last, second;
int swaps =0;
int comp = 0;
minIndex = first;
for (loc = 0; loc < length; loc++)
{
comp+=1;
for(loc = first +1; loc<=last; loc++)
{
comp+=1;
if(list[loc]<list[minIndex])
minIndex=loc;
comp+=1;
}
elemType temp;
temp= list[first];
list[first]= list[second];
list[second] = temp;
swaps+=1;
}
// comp = (length *(length-1)/2);
cout<<"swaps= "<<swaps<<endl;
cout<<"comps= "<<comp<< endl;
}
任何想法表示赞赏