基于visual Studio2013解决面试题之1404希尔排序

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               



题目



解决代码及点评

  1.   
  1.   
  1. <code class="language-cpp">/* 
  2.     希尔排序 
  3. */  
  4.   
  5. #include <iostream>  
  6.   
  7. using namespace std;  
  8. const int N=10;  
  9. void shell_sort(const int len, int *array)  
  10. {  
  11.     int j,i,key;  
  12.     int gap=0;  
  13.     if( len <= 0 || array == NULL )  
  14.         return;  
  15.     while( gap <= len )  
  16.     {  
  17.   
  18.         gap = gap*3+1;  
  19.   
  20.     }  
  21.     while( gap > 0 )  
  22.   
  23.     {  
  24.   
  25.         for( i=gap; i<len; i++ )  
  26.         {  
  27.             j = i-gap;  
  28.             key = array[i];  
  29.             while ( (j >= 0) && (array[j] > key) )  
  30.             {  
  31.                 array[j+gap] = array[j];  
  32.                 j = j-gap;  
  33.             }  
  34.             array[j+gap] = key;  
  35.         }  
  36.         //display_array(len,array,gap);  
  37.         gap = (gap - 1)/3;  
  38.   
  39.     }  
  40.   
  41. }  
  42.   
  43. int main()  
  44. {  
  45.   
  46.     int array[N];  
  47.     for(int i=0;i<10;i++)  
  48.     {  
  49.         array[i]=rand()%100;  
  50.         cout<<array[i]<<" ";  
  51.     }  
  52.     shell_sort(N-1,array);  
  53.     cout<<endl;  
  54.     for(int i=0;i<10;i++)  
  55.     {  
  56.   
  57.         cout<<array[i]<<" ";  
  58.     }  
  59.     system("pause");  
  60.     return 0;  
  61. }  
  62. </code>  
/* 希尔排序*/#include <iostream>using namespace std;const int N=10;void shell_sort(const int len, int *array)int j,i,key;    int gap=0if( len <= 0 || array == NULL )  returnwhile( gap <= len ) {  gap = gap*3+1; } while( gap > 0 ) {  for( i=gap; i<len; i++ )  {   j = i-gap;   key = array[i];   while ( (j >= 0) && (array[j] > key) )   {    array[j+gap] = array[j];    j = j-gap;   }   array[j+gap] = key;  }  //display_array(len,array,gap);  gap = (gap - 1)/3; }}int main()int array[N]; for(int i=0;i<10;i++) {  array[i]=rand()%100;  cout<<array[i]<<" "; } shell_sort(N-1,array); cout<<endlfor(int i=0;i<10;i++) {  cout<<array[i]<<" "; } system("pause"); return 0;}

代码下载及其运行

代码下载地址:http://download.csdn.net/detail/yincheng01/6704519

解压密码:c.itcast.cn


下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:

1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”


2)在下拉框中选择相应项目,项目名和博客编号一致

3)点击“本地Windows调试器”运行


程序运行结果








           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/fdgugfv/article/details/83989434