zoukankan      html  css  js  c++  java
  • C语言“快速排序”函数写法

     代码是:C语言中快速排的写法,要加入头文件   <stdlib.h>

           qsort(数组名, 长度, 数据类型大小,比较算子 );

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int cmp(const void *a, const void *b)
    {
    	return *(int *)a-*(int *)b;      //从小到大进行排序   例如输出: 1 2 3 4 5
    }
    
    int main()
    {
    	int a[100];
    	int n;
    	int i, j;
    	while(scanf("%d", &n)!=EOF)
    	{
    		for(i=0; i<n; i++)
    		{
    			scanf("%d", &a[i] );
    		}
    		qsort(a, n, sizeof(int), cmp);
    
    		for(j=0; j<n; j++)
    		{
    			printf("%d%c", a[j], i==n-1?'
    ':' ' );
    		}
    	}
    	return 0;
    }
    

      

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int cmp(const void *a, const void *b)
    {
    	return *(int *)b-*(int *)a;   //从大到小进行排序   例如输出: 5 4 3 2 1
    }
    
    int main()
    {
    	int a[100];
    	int n;
    	int i, j;
    	while(scanf("%d", &n)!=EOF)
    	{
    		for(i=0; i<n; i++)
    		{
    			scanf("%d", &a[i] );
    		}
    		qsort(a, n, sizeof(int), cmp);
    
    		for(j=0; j<n; j++)
    		{
    			printf("%d%c", a[j], i==n-1?'
    ':' ' );
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    正则表达式在NLP中应用
    spring boot中 异常:Error resolving template "xxx", template might not exist or might not be accessible...解决办法
    毕业设计6
    毕业设计5
    毕业设计4
    毕业设计3
    毕业设计2
    毕业设计1
    支付宝架构
    Javaee应用架构
  • 原文地址:https://www.cnblogs.com/yspworld/p/3996343.html
Copyright © 2011-2022 走看看