小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

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




题目



解决代码及点评

/*
	希尔排序
*/

#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调试器”运行


程序运行结果









posted on 2013-12-22 22:52  牛栏山1  阅读(81)  评论(0编辑  收藏  举报

导航