新聞中心
蘭州大學(xué)C語言程序通過定義函數(shù)實現(xiàn)冒泡排序過程?
這個問題我大學(xué)考試的時候也遇到過:
創(chuàng)新互聯(lián)建站是專業(yè)的城關(guān)網(wǎng)站建設(shè)公司,城關(guān)接單;提供做網(wǎng)站、成都網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行城關(guān)網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
關(guān)于將冒泡排序過程單獨寫成一個函數(shù),再在主函數(shù)中通過調(diào)用該函數(shù)實現(xiàn)對任意輸入的多個數(shù)據(jù)(數(shù)據(jù)個數(shù)不超過100個)進(jìn)行排序,并輸出排序結(jié)果。
答案如下:
#include? stdio.h
//冒泡排序
void bubble_sort(int a[],int n);
//輸出排序
void output(int a[], int n);
int main()
{
int number[100],count=0;
char y;
printf("請輸入任意個數(shù),按回車鍵結(jié)束\n");
do
{
scanf("%d",number[count++]);
}while((y=getchar()) != '\n');// 這里用來判斷是否輸入了回車
bubble_sort(number,count);
output(number,count);
return 0;
}
答案過程太多了,我只寫了一半,剩余的一半在這里!
蘭州大學(xué)C語言程序通過定義函數(shù)實現(xiàn)冒泡排序過程
C語言課程作業(yè),通過定義函數(shù)實現(xiàn)冒泡排序過程。
void sortMaopao(int *arr,int n)
{
int i,j,tmp;
if(n100)
return ;
for(i=0;in-1;i++)
{
for(j=i+1;jn;j++)
{
if(*(arr+i)*(arr+j))
{
tmp=*(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)=tmp;
}
}
}
}
這個函數(shù)可以幫你實現(xiàn)由大到小排序,如果由小到大的話只需把if(*(arr+i)*(arr+j))這一句里面的 改成 就行了,輸出的話可以在主函數(shù)調(diào)用之后再打印輸出就行。
c語言怎么用自定義函數(shù)排序
#include stdio.h
#include string.h#define NUM 3
struct student
{
char name[20]; /*姓名*/
long num; /*12位學(xué)號*/
double sum; /*總分*/
};
void Create_Students(struct student stu[NUM])
{
struct student *p;
printf("請輸入學(xué)生姓名 學(xué)號(12位) 總分:\n");
for( p = stu; p stu+NUM; p++)
{
scanf("%s %d %lf",p-name,p-num,p-sum); }
}
void Order_Students(struct student stu[NUM])//起泡法
{
int i,j;
struct student temp;
for(i=NUM-1;i=0;i--)
for(j=0;ji;j++)
if(stu[j].sumstu[j+1].sum)
{
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1]=temp;
}
}
void main()
{
int i=1;
struct student *p;
struct student stu[NUM];
Create_Students(stu);
Order_Students(stu);
printf("%-20s %-13s %-6s %4s\n","姓名","學(xué)號(12位)","總成績","名次");
for(p=stu;pstu+NUM;p++,i++)
{
printf("%-20s %-13.0d %-8.2f %2d\n",p-name,p-num,p-sum,i);
}
}//你參考參考,嘿
網(wǎng)頁題目:c語言冒泡排序自定義函數(shù),冒泡排序c語言函數(shù)調(diào)用
標(biāo)題鏈接:http://fisionsoft.com.cn/article/hdijhi.html