sort函数代码(c语言 编写函数sort(int*x,int n)函数)

本文目录
c语言 编写函数sort(int*x,int n)函数
#include《stdio.h》
#include《stdlib.h》
void sort(int*x,int n)
{
int i,j,t;
for(i=0;i《n-1;i++)
for(j=0;j《n-i-1;j++)
if(abs(*(x+j))》abs(*(x+j+1))){
t=*(x+j);
*(x+j)=*(x+j+1);
*(x+j+1)=t;
}
}
int main()
{
int n,i;
scanf("%d",&n);
int x;
for(i=0;i《n;i++)
scanf("%d",&x);
sort(x,n);
for(i=0;i《n;i++)
printf("%d ",x);
return 0;
}
编写函数sort()
’其功能是将形参一维数组按照从小到大排序
’。编写程序。定义一维数组,输入一维数组元素值。
’调用sort()函数排序 。并输出排序后的一维数组
Dim a(100) As Integer
Private Sub sort(ByVal s As Integer, ByVal t As Integer)
If s 》= t Then Exit Sub
i = s
j = t
r = Int((i + j) / 2)
Key = a(r)
While i 《 j
While a(i) 《 Key ’不取等号 即遇到相同大小的数字也不跳过 对其处理(如取等号 则跳过,不对其处理)
i = i + 1
Wend
While a(j) 》 Key
j = j - 1
Wend
If i 《= j Then ’取等号 即i、j两指针重合时也对其处理 防止程序死循环
temp = a(i)
a(i) = a(j)
a(j) = temp
i = i + 1
j = j - 1
End If
Wend
Call sort(s, j)
Call sort(i, t)
End Sub
Private Sub Command1_Click()
For i = 1 To 100
Randomize
a(i) = Int(Rnd * 100 + 1)
Next
Call sort(1, 100)
For i = 1 To 100
Print a(i);
If i Mod 10 = 0 Then Print
Next
End Sub
c 排序—sort()函数
代码片段:
struct STU{
char name;
int chinese, math, english;
int total;
}stu;
bool cmp(STU a, STU b)
{
return a.total 《 b.total;
}
void sort()
{
std::sort(stu, stu + n, cmp);//n是总共的学生个数
printf("name\t\tchinese\tmath\tenglish\ttotal\n");
for (int i = 0; i 《 n; i++) {
printf("%12s", stu.name);
printf("%8d", stu.chinese);
printf("%8d", stu.math);
printf("%8d", stu.english);
printf("%8d", stu.total);
printf("\n");
}
}
void menu()
{
int a;
cin 》》 a;
switch(a) {
case 6 : sort();
}
}

更多文章:
怎么看有没有装jdk(在windows系统下怎么查看有没有安装jdk)
2025年11月1日 17:15
websocket浏览器兼容(websocket怎样兼容ie8)
2025年5月24日 04:30
excel乘法函数怎么输入(excel表格函数乘法 excel表格里如何乘法函数公式)
2025年8月8日 11:45
苏州公司网站设计(苏州网站建设流程是什么苏州专业网站策划哪家最好)
2025年8月26日 05:15
constructed environment翻译(翻译下面这段文字)
2025年9月26日 03:00
java字符串截取后两位(JAVA截取所有指定字符后面的字符串)
2025年12月5日 23:15
墨迹天气插件(vivo手机的桌面插件在哪里能找到,我安装墨迹天气,设置桌面时钟,都不知道装在哪)
2026年6月5日 02:15
excludefrom的用法(excluded from boot order什么意思)
2026年3月26日 11:45
intellectual enjoyment翻译(英语翻译一下这段话哦)
2026年4月28日 00:15














