• 排序


    排序

    1、选择排序

    代码实现:
    /*
    	2017年6月19日14:46:17
    	功能:选择排序的降序排序
    */
    #include"stdio.h"
    #define N 8
    int main()
    {
    	int i, j, m, temp;
    	int a[N];
    	for(i = 0; i < N; i++)
    	{
    		printf("please input a number :" );
    		scanf("%d",&m);
    		a[i] = m;
    	}
    
    	printf("The old array is: ");
    	printf("
    ");
    	for(i = 0; i < N; i++)
    	{
    		printf("%d	",a[i] );
    	}
    	printf("
    " );
    
    	for(i = 0;i < N; i++)
    	{	for(j = i+1; j < N; j++)
    		{
    			if(a[i] < a[j])
    			{
    				temp = a[i];
    				a[i] = a[j];
    				a[j] = temp;
    			}
    		}
    	}
    
    	printf("The new array is: ");
    	printf("
    ");
    	for(i = 0; i < N; i++)
    	{
    		printf("%d	",a[i] );
    	}
    	printf("
    " );
    	
    	return 0;
    }
    /*
    	总结:
    	在VC++6.0中显示的结果为:
    	————————————————————————————————
    	please input a number :2
    	please input a number :4
    	please input a number :8
    	please input a number :9
    	please input a number :6
    	please input a number :1
    	please input a number :9
    	please input a number :5
    	The old array is:
    	2       4       8       9       6       1       9       5
    	The new array is:
    	9       9       8       6       5       4       2       1
    	————————————————————————————————
    */
    

    2、冒泡排序

    代码实现:
    /*
    	2017年6月19日15:17:40
    	功能:冒泡排序的降序排序
    */
    #include"stdio.h"
    #define N 8
    int main()
    {
    	int a[N];
    	int i, j, m, temp;
    	for(i = 0; i < N; i++)
    	{
    		printf("please input a number :" );
    		scanf("%d",&m);
    		a[i] = m;
    	}
    
    	printf("The old array is : ");
    	printf("
    ");
    	for(i = 0; i < N; i++)
    	{
    		printf("%d	",a[i]);	
    	}
    	printf("
    " );
    
    	for(i = 0; i < N; i++)
    	{	for(j = 0; (j < N - i)&&(j+1 < N - i); j++)
    		{
    			if(a[j]< a[j+1])
    			{
    				temp = a[j];
    				a[j] = a[j+1];
    				a[j+1] = temp;	
    			}
    		}
    	}
    
    	printf("The new array is : ");
    	printf("
    ");
    	for(i = 0; i < N; i++)
    	{
    		printf("%d	",a[i]);	
    	}
    	printf("
    " );	
    	return 0;
    }
    /*
    	总结:
    	在VC++6.0中显示的结果为:
    	————————————————————————————————
    	please input a number :2
    	please input a number :4
    	please input a number :8
    	please input a number :4
    	please input a number :9
    	please input a number :5
    	please input a number :3
    	please input a number :1
    	The old array is :
    	2       4       8       4       9       5       3       1
    	The new array is :
    	9       8       5       4       4       3       2       1
    	————————————————————————————————
    */
    

    3、直接插入排序

    代码实现:
    /*
    	2017年6月21日10:51:16
    	功能:直接插入排序的降序
    */
    #include"stdio.h"
    #define N 8
    int main()
    {
        int i, j, m, temp;
        int a[N];
        for(i = 0; i < N; i++)
        {
            printf("please input a number :" );
            scanf("%d",&m);
            a[i] = m;
        }
     
        printf("The old array is: ");
        printf("
    ");
        for(i = 0; i < N; i++)
        {
            printf("%d	",a[i] );
        }
        printf("
    " );
    
    	for(i = 1; i < N; i++)
    	{
    		for(j = 0; j < i; j++ )
    		{
    			if(a[j] < a[i])
    			{		
    				temp = a[j];
    				a[j] = a[i];
    				a[i] = temp;
    				i = j;
    			}
    
    		}
    		
    	}
    
        printf("The new array is: ");
        printf("
    ");
        for(i = 0; i < N; i++)
        {
            printf("%d	",a[i] );
        }
        printf("
    " );
         
        return 0;
    }
    /*
    	总结:
    	在VC++6.0中的显示结果:
    	————————————————————————————————
    	please input a number :2
    	please input a number :7
    	please input a number :5
    	please input a number :4
    	please input a number :7
    	please input a number :5
    	please input a number :4
    	please input a number :9
    	The old array is:
    	2       7       5       4       7       5       4       9
    	The new array is:
    	9       7       7       5       5       4       4       2
    	————————————————————————————————
    */   
    

      

      

      

  • 相关阅读:
    react绑定事件
    js基础
    浅谈vue中index.html、main.js、App.vue、index.js之前的关系以及加载过程
    jmeter+influxdb+grafana搭建压测监控平台
    javascript中的constructor
    javascript es6新增语法之`${}`
    微服务网关 ----- Nginx 和 Zuul 的区别
    Spring AOP SpringBoot集成
    Spring AOP 面向切面编程入门
    Vue中$refs与$emit的区别及使用场景实例
  • 原文地址:https://www.cnblogs.com/wxt19941024/p/7054631.html
Copyright © 2020-2023  润新知