题解 | #牛牛的排序#
牛牛的排序
http://www.nowcoder.com/practice/26a0c92e9266443887a3bf81aff8e188
#include <stdio.h> #include <malloc.h>
int* sort(int* p, int size) { int i, j; int temp = 0; for (i=0; i< size; i++) { for (j=0; j<size-i-1; j++) { if (p[j] > p[j+1]) { temp = p[j]; p[j] = p[j+1]; p[j+1] = temp; } } }
return p;
}
int main() { int n; scanf("%d", &n); int* p = (int*)malloc(n*sizeof(int)); int *re = p;
int m=0;
int i;
while ((scanf("%d ", &m)) != EOF)
{
*p = m;
p++;
}
p = re;
sort(p, n);
for (i=0; i<n-1; i++) printf("%d ", p[i]);
printf("%d\n", p[n-1]);
return 0;
}