基於visual Studio2013解決面試題之1404希爾排序
阿新 • • 發佈:2018-11-12
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
題目
解決程式碼及點評
[cpp]
view plain
copy
print
[cpp]
view plain
copy
print
?
[cpp]
view plain
copy
print
?
- <code class="language-cpp">/*
- 希爾排序
- */
- #include <iostream>
- using namespace std;
- const int N=10;
- void shell_sort(const int len, int *array)
- {
- int
- int gap=0;
- if( len <= 0 || array == NULL )
- return;
- while( 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<<endl;
- for(int i=0;i<10;i++)
- {
- cout<<array[i]<<" ";
- }
- system("pause");
- return 0;
- }
- </code>
/* 希爾排序*/#include <iostream>using namespace std;const int N=10;void shell_sort(const int len, int *array){ int j,i,key; int gap=0; if( len <= 0 || array == NULL ) return; while( 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<<endl; for(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偵錯程式”執行
程式執行結果