排序数字。 [英] Sorting the numbers.
本文介绍了排序数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要编写一个程序来排序命令行参数(数字)。
我尝试了什么:
i need to write a program to sort command line arguments (Numbers).
What I have tried:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int i, j, t, n, pos, a[100];
n = argc - 1;
printf("\nNumber of elements in the input array is : %d\n", n);
printf("\nThe input array is\n\t");
for (i = 1; i <= n; i++)
{
a[i - 1] = atoi(argv[i]);
printf("%d ", a[i - 1]);
}
for (i = 0; i<n; i++)
{
pos = i;
for (j = i + 1; j<n; j++)
{
if (a[pos] > a[j])
{
pos = j;
}
}
if (pos != i)
{
t = a[i];
a[i] = a[pos];
a[pos] = t;
}
}
printf("\n\n The sorted array is \n\t");
for (i = 0; i<n; i++)
{
printf("%d ", a[i]);
}
return 0;
}
推荐答案
有不同的排序算法,所以这个 cprogramming教程为您提供了关于排序的精彩课程。
提示:使用Google
There are different sorting algorhitms, so this tutorial at cprogramming gives you a fine lesson about sorting.
tip: use Google
这篇关于排序数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文