【发布时间】:2021-06-17 13:29:41
【问题描述】:
void SelectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
PrintArray(&arr[i], n);
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
Swap(&arr[min_idx], &arr[i]);
}
}
void PrintArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
this is the output I'm getting
我正在尝试打印排序过程的每次迭代,我已经分别测试了排序功能和打印功能,它们都可以工作,我尝试将打印功能放在循环中的不同位置,但没有工作。我是 c 和一般编程的新手,所以如果你也可以向我解释这些步骤,我将不胜感激。 谢谢
【问题讨论】:
标签: c sorting selection-sort function-definition