zoukankan      html  css  js  c++  java
  • 希尔排序

    #include<stdio.h>
    #define MAX 250 
    
    int R[MAX];
    //某一确定增量d的希尔排序
    void Shell_Pass(int d,int n)
    {
    	int i,j;
    	for(i=d+1;i<=n;i++)
    	{
    		if(R[i]<R[i-d])
    		{
    			R[0]=R[i];
    			j=i-d;
    			do{
    				R[j+d]=R[j];
    				j=j-d;
    			}while(j>0&&R[0]<R[j]);
    			R[j+d]=R[0];
    		}
    	}
    	
     } 
     
     void Shell_Sort(int n)
     {
     	int d=n;
     	do{
     		d=d/3+1;
     		Shell_Pass(d,n);
    	 }while(d>1);
     }
     
     int main()
     {
     	int n,i,d;
     	printf("希尔排序示例:
    ");
     	printf("Please input n above 1 and below %d
    ",MAX);
     	scanf("%d",&n);
     	if(n<1||n>MAX)
     	{
     		printf("The n you input is not right!BYE!");
     		return 0;
    	 }
    	 printf("Please input the array one by one:
    ");
    	 for(i=1;i<=n;i++)
    	 {
    	 	scanf("%d",&R[i]);
    	 }
    	 printf("The array you input is :
    ");
    	 for(i=1;i<=n;i++)
    	 {
    	 	printf("%d ",R[i]);
    	 }
    	 Shell_Sort(n);
    	 printf("The array after Shell order is:
    ");
    	 for(i=1;i<=n;i++)
    	 {
    	 	printf("%d ",R[i]);
    	 }
    	 return 0;
     }
    

      

  • 相关阅读:
    vue-cli的npm run build的常见问题
    es6 Symbol
    es6 对象的扩展
    es7 函数绑定
    es6 箭头函数
    学习weex遇见非常奇怪的问题
    微信
    java面试题
    PHP面试题
    Android
  • 原文地址:https://www.cnblogs.com/qysqys/p/5386728.html
Copyright © 2011-2022 走看看