我该如何优化它?插入排序。 C ++。 Vs2012 [英] How Do I Optimize It? Insertion Sort. C++. Vs2012
本文介绍了我该如何优化它?插入排序。 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>
推荐答案
如果您只是在寻找一个相当不错的解决方案,请不要自己实现,而是使用STL库: http://www.cplusplus.com/reference/algorithm/sort/ [ ^ ]
至于为了学习如何做东西而优化你的代码,关于插入的事情是它在类似数组的数据结构上效率相当低!更有效的实现将使用列表。您可以为此目的简单地使用std :: list
并专注于排序算法的细节 - 重新发明wheel列表没有意义; - )
If you're just looking for a reasonably good solution, don't implement it yourself, use the STL library instead: http://www.cplusplus.com/reference/algorithm/sort/[^]
As for optimizing your code for the purpose of learning how to do 'stuff', the thing about insertion is that it's rather inefficient on an array-like data structure! A much more efficient implementation would use a list instead. You could simply usestd::list
for that purpose and focus on the details of your sort algorithm - no point in reinventing thewheellist ;-)
从选择正确的算法中获得的最佳优化。每个算法都有不同的优缺点。
在维基百科你可以阅读更多关于并找出答案。
避免复制对象并使用const和指针。大多数工作可以而且将会比现代编译器和优化器更好。尝试不同的设置。
The best optimization you will get from choosing the right algorithm. The have all different pros and cons of EVERY algorithm.
In wikipedia you can read more about and find out.
Avoid copying objects around and work with const and pointers. Most of the job can and will do than a modern compiler and optimizer. Try different settings.
这篇关于我该如何优化它?插入排序。 C ++。 Vs2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文