zoukankan      html  css  js  c++  java
  • sicily 6495. 整数数组排序

    Description
    对输入的n个整数进行排序,按照升序输出。
    1 <= n <= 10000

    Input
    每个case第一行输入一个数,n(当n=0时,结束)
    接下来输入n个整数

    Output
    每个case输出一行,这一行是所有数字按照从小到大排序后的结果,数字与数字之间用空格隔开

    冒泡法:

    View Code
     1 #include<stdio.h>
     2 
     3 void bubbleSort( int array[], int size );
     4 void printArray( const int array[], int size );
     5 
     6 int main()
     7 {
     8     int n;
     9     int i;
    10     int array[10001] = {0};
    11     
    12     
    13     while ( scanf("%d", &n) && n != 0 )
    14     {
    15         for ( i = 0; i < n; i++ )
    16         {
    17             scanf( "%d", &array[i] );
    18         }
    19         
    20         bubbleSort( array, n );
    21         
    22         printArray( array, n );
    23         
    24     }
    25     
    26     return 0;
    27 } 
    28 
    29 void bubbleSort( int array[], int size )
    30 {
    31     int i, j;
    32     int temp;
    33     
    34     for ( i = 1; i < size; i++ )
    35     {
    36         for ( j = 0; j < size - 1; j++ )
    37         {
    38             if ( array[j] > array[j+1] )
    39             {
    40                 temp = array[j];
    41                 array[j] = array[j+1];
    42                 array[j+1] = temp;
    43             }
    44         }
    45     }
    46     
    47     return;
    48 }
    49 
    50 
    51 void printArray( const int array[], int size )
    52 {
    53     int i;
    54     
    55     printf( "%d", array[0] );
    56     
    57     for ( i = 1; i < size; i++ )
    58     {
    59         printf( " %d", array[i] );
    60     } 
    61     
    62     printf( "\n" );
    63     
    64     return;
    65 }

    选择法:

    View Code
     1 #include<stdio.h>
     2 void selectionSort( int array[], int size );
     3 void printArray( const int array[], int size );
     4 
     5 int main()
     6 {
     7     int n;
     8     int i;
     9     int array[10001] = {0};    
    10     
    11     while ( scanf("%d", &n) && n != 0 )
    12     {
    13         for ( i = 0; i < n; i++ )
    14         {
    15             scanf( "%d", &array[i] );
    16         }
    17         
    18         selectionSort( array, n );
    19         
    20         printArray( array, n );
    21         
    22     }
    23     
    24     return 0;
    25 } 
    26 
    27 void selectionSort( int array[], int size )
    28 {
    29     int i, j;
    30     int min;
    31     int temp;
    32     
    33     for ( i = 0; i < size - 1; i++ )
    34     {
    35         min = i;
    36         for ( j = i + 1 ; j < size; j++ )
    37         {
    38             if ( array[j] < array[min] )
    39             {
    40                 min = j;
    41             }
    42         }
    43         
    44         if ( i != min )
    45         {
    46             temp = array[i];
    47             array[i] = array[min];
    48             array[min] = temp;
    49         }
    50     }
    51     
    52     return;
    53 }
    54 
    55 void printArray( const int array[], int size )
    56 {
    57     int i;
    58     
    59     printf( "%d", array[0] );
    60     
    61     for ( i = 1; i < size; i++ )
    62     {
    63         printf( " %d", array[i] );
    64     } 
    65     
    66     printf( "\n" );
    67     
    68     return;
    69 }
  • 相关阅读:
    html里特殊标签,试下就知道是什么了
    Eclipse 中文乱码问题
    PHP中的MYSQL常用函数总结
    PHP中$_SERVER的详细用法
    JQUERY获取radio,checkbox,select值
    jquery基础教程七 选择器(selectors 的xpath语法应用)
    php培训班的课程表 可自学参考
    php中trim()的特殊用法
    80后
    JQuery Attributes 方法说明
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2797711.html
Copyright © 2011-2022 走看看