整体算法描述:
录入一串整数,通过调用selection_sort函数和sort函数来排序这些整数。
selection_sort函数描述:
1.确定第n个数组元素;2.递归调用本身
sort函数描述:
搜索数组找出最大的元素,并返回到数组最后
#include <stdio.h>
#define N 3
void selection_sort(int n, int a[]);
int sort(int n, int a[]);
int main()
{
int a[N];
printf("Enter numbers:");
for (int j = 0;j < N;j++){
scanf_s("%d", &a[j]);
}
selection_sort(N, a);
for (int j = 0;j < N;j++ ) {
printf("%d\t", a[j]);
}
}
void selection_sort(int n,int a[]) {
if (n < 0) return;
sort(n, a); //get a[n]
selection_sort(n - 1, a);
}
int sort(int n, int a[]) {
int m;
for (int i = 0; i < n - 1; i++)
{
if (a[n - 1] < a[i]) {
m = a[i];
a[i] = a[n - 1];
a[n - 1] = m;
}
}
return a[n];
}
转载需注明