C 程序:排序数组元素
原文:https://www.studytonight.com/c/programs/array/sort-array-element-program
下面是一个对数组中的数组元素进行排序的程序。
#include<stdio.h>
#include<conio.h>
void sorting(int *x, int y);
void main()
{
int a[20], i, c, n;
clrscr();
printf("Enter number of elements you want to sort: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
sorting(a, n);
for(i = 0; i <n; i++)
printf("%d\t", a[i]);
getch();
}
void sorting(int *x, int y)
{
int i, j, temp;
for(i = 1; i <= y-1; i++)
{
for(j = 0; j < y-i; j++)
{
if(*(x+j) > *(x+j+1))
{
temp = *(x+j);
*(x+j) = *(x+j+1);
*(x+j+1) = temp;
}
}
}
}
输入要排序的元质数量:6 5 3 4 2 1 6 1 2 3 5 6