标签:
#include <iostream> using namespace std; void ShellSort(int arr[],int arr_size)//数组元素的个数 { int gap=arr_size/2; int cur=0; while(gap>=1) { while(cur<gap) { for(int index=cur+gap;index<=arr_size-1;index+=gap) { while(index>cur) { if(arr[index-gap]>arr[index]) { swap(arr[index-gap],arr[index]); index-=gap; }else{ break; } } } cur++; } gap=gap/2; cur=0; } } void ShellSort1(int arr[],int arr_size)//数组元素的个数 { for(int gap=arr_size/2;gap>=1;gap=gap/2) { for(int cur=0;cur<gap;cur++) { for(int index=cur+gap;index<=arr_size-1;index+=gap) { while(index>cur) { if(arr[index-gap]>arr[index]) { swap(arr[index-gap],arr[index]); index-=gap; }else{ break; } } } } } } int main() { int arr[]={1,3,4,6,7,34,54,14,654,87657}; ShellSort1(arr,10); for(int i=0;i<10;i++) { cout<<arr[i]<<" "; } cout << "Hello world!" << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/GW17195/p/5726216.html