我的选择排序代码
#include <stdio.h>
void selection_sort(int a[], int n);
int main()
{
int size;
printf("Enter the size of array: ");
scanf("%d",&size);
int b[size],i = 0;
printf("Enter %d integers to be sorted: ",size);
while(i++ < size)
scanf("%d",&b[i]);
selection_sort(b, size);
printf("Sorted integers(by selection sort) are: ");
for(int i = 0; i < size; i++)
printf("%d",b[i]);
return 0;
}
void selection_sort(int a[], int n)
{
while(n >= 0 )
{
if(n == 0)
break;
else
{
int i = 0, c = 0;
int largest = a[0];
while(i++ < n)
if(largest < a[i])
{
c = i ;
largest = a[i];
}
int temp = a[--n];
a[n] = largest;
a[c] = temp;
selection_sort(a, n);
}
}
}
关于按升序对数组进行排序
3 4 1 2
给出奇怪的输出
2293388 4 3 0
我检查了很多次,但未能解决问题。我应该怎么做才能正常工作?
使用的算法:
1. 搜索数组中的最大元素。
2. 将最大元素移动到数组的最后一个位置。
3. 递归调用自身对数组的前n -1 个元素进行排序。
请不要提供任何其他解决方案,否则我会感到困惑。