本文介绍了我该如何优化它?插入排序。 C ++。 Vs2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何优化我的代码?执行它?就像不要使用那些变体或类型...

Hi,
How can i optimize my code? to perform it? Like don't use that variants or types ...

#include "stdafx.h"
#include <iostream>
using namespace std;

void displayvalues(int k,int i,int arr[],int arraysize)//display values
{
			 cout<<"\tk :"<<k;
			 cout<<"\ti :"<<i;
			 cout<<"\tarr[i] :"<<arr[i];
			 cout<<"\tarr[i+1] :"<<arr[i+1];		
			 cout<<"\narray arr :";
			for( int i = 0 ; i < arraysize ; i++ )	
	{
		 cout<<arr[i];
		if(i < arraysize-1)
			 cout<<" - ";
	}			
}

void insertionsort(int arr[], int arraysize)//sort the array
{
	int counter=0;
	int cmp=0;
	for(int j = 1; j < arraysize; j++)
	{
		cmp++;
		int k = arr[j];
		int i = j-1;
		while( arr[i] > k && i >= 0 )
		{
			cmp++;
			 cout<<"\nj :"<<j;
			 cout<<"\tarr[j] :"<<arr[j];
			arr[i+1] = arr[i];
			displayvalues(k,i,arr,arraysize);//display values
			i--;
			counter++; //iterate counter
		}
		arr[i+1] = k;
		 cout<<"\nj :"<<j;
			 cout<<"\tarr[j] :"<<arr[j];
		displayvalues(k,i,arr,arraysize);	//display values
		counter++; //iterate counter
		cmp++;
		}
	 cout<<"\niterated "<<counter<<" times\n";//displays iterations
	 cout<<"\n***\n comparisons number = "<<cmp<<"\n***";
}

void showvalues(int arr[],int arraysize)//show the array
{
	 cout<<"\narray :\n";
	for( int i = 0 ; i < arraysize ; i++ )	
	{
		 cout<<arr[i];
		if(i < arraysize-1)
			 cout<<" - ";
	}
	
	getchar();
}

int main()
{		
	 cout<<"\nPlease enter your array length :";	
	int arraysize;
	 cin>>arraysize;
	int* arr=new int[arraysize];

	 cout<<"\nPlease enter values :\n";
	for( int i = 0 ; i < arraysize ; i++ )	
		 cin>>arr[i];
	
	showvalues(arr,arraysize);//show values

	insertionsort(arr,arraysize);//sort values

	showvalues(arr,arraysize);//show values
}</iostream>

推荐答案



这篇关于我该如何优化它?插入排序。 C ++。 Vs2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:58