3203 数组做函数参数----排序函数--C语言版

3203: 数组做函数参数----排序函数--C语言版

时间限制: 1 Sec  内存限制: 128 MB
提交: 253  解决: 151
[提交][状态][讨论版][命题人:smallgyy]

题目描述

定义一个函数来完成对参数数组中元素的排序工作,函数声明如下:
void sort(int array[ ]);//该函数完成对array数组的排序工作
在以下程序的基础上,完成对sort函数的定义,提交时,只提交sort函数的定义即可。
#include <stdio.h>
void sort(int array[ ]);//该函数完成对array数组的排序工作
int main()
{
int a[10];
int i;
for(i=0;i<10;i++) //为数组元素赋值
scanf("%d",&a[i]);
sort(a); //对a数组进行排序
for(i=0;i<10;i++) //输出数组元素
printf("%d ",a[i]);
return 0;
}

输入

输入十个为未排序的整数

输出

输出已经排好序的十个整数

样例输入

1 3 5 7 9 0 2 4 6 8

样例输出

0 1 2 3 4 5 6 7 8 9

提示


可以使用任何排序方法

 

#include <stdio.h>
void sort(int array[ ]);//该函数完成对array数组的排序工作
int main()
{
    int a[10];
    int i;
    for(i=0; i<10; i++) //为数组元素赋值
        scanf("%d",&a[i]);
    sort(a); //对a数组进行排序
    for(i=0; i<10; i++) //输出数组元素
        printf("%d ",a[i]);
    return 0;
}
void sort(int a[])
{
    int i, j, temp;
    for(i = 0; i < 9; ++i)
    {
        for(j = i + 1; j < 10; ++j)
        {
            if(a[i] > a[j])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
}

  本题主要练习一下选择排序

转载于:https://www.cnblogs.com/mjn1/p/9898167.html

常用数组排序算法 1. 2. 3.publicclassSortAll{ 4. 5./** 6.*冒泡排序,选择排序,插入排序,希尔(Shell),快速排序排序Java的实现 */ 7.publicstaticvoidmain(String[]args){ 8.int[]i={1,5,6,12,4,9,3,23,39,403,596,87}; 9.System.out.println("----冒泡排序的结果:"); 10.maoPao(i); 11.System.out.println(); 12.System.out.println("----选择排序的结果:"); 13.xuanZe(i); 14.System.out.println(); 15.System.out.println("----插入排序的结果:"); 16.chaRu(i); 17.System.out.println(); 18.System.out.println("----希尔(Shell)排序的结果:"); 19.shell(i); 20.} 21. // 冒泡排序 1.publicstaticvoidmaoPao(int[]x){ 2.for(inti=0;i<x.length;i++){ 3.for(intj=i+1;jx[j]){ 5.inttemp=x[i]; 6.x[i]=x[j]; 7.x[j]=temp; 8.} 9.} 10.} 11.for(inti:x){ 12.System.out.print(i+""); 13.} 14.} // 选择排序 1.publicstaticvoidxuanZe(int[]x){ 2.for(inti=0;i<x.length;i++){ 3.intlowerIndex=i; 4.//找出最小的一个索引 5.for(intj=i+1;j<x.length;j++){ 6.if(x[j]<x[lowerIndex]){ 7.lowerIndex=j; 8.} 9.} 10.//交换 11.inttemp=x[i]; 12.x[i]=x[lowerIndex]; 13.x[lowerIndex]=temp; 14.} 15.for(inti:x){ 16.System.out.print(i+""); 17.} 18.} 19. 20.//插入排序 21.publicstaticvoidchaRu(int[]x){ 22.for(inti=1;i0;j--){ 24.if(x[j]0;increment/=2){ 4.//每个组内排序 5.for(inti=increment;i=increment;j-=increment){ 9.if(temp=hi) 7.return; 8. 9.//确定指针方向的逻辑变量 10.booleantransfer=true; 11. 12.while(lo!=hi){ 13.if(a[lo]>a[hi]){ 14.//交换数字 15.inttemp=a[lo]; 16.a[lo]=a[hi]; 17.a[hi]=temp; 18.//决定下标移动,还是上标移动 19.transfer=(transfer==true)?false:true; 20.} 21. 22.//将指针向前或者向后移动 23.if(transfer) 24.hi--; 25.else 26.lo++; 27. 28.} 29. 30.//将数组分开两半,确定每个数字的正确位置 31.lo--; 32.hi++; 33.quickSort(a,lo0,lo); 34.quickSort(a,hi,hi0); 35.}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值