Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
How can i optimize my code? to perform it? Like don't use that variants or types ...
C++
#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>
Posted
Comments
Richard MacCutchan 24-Jul-14 7:00am    
There are lots of different sort algorithms; I suggest you Google for them and find the one that will work best for you.
m.r.m.40 24-Jul-14 9:08am    
Yes, Thank you.
Actually i'm working on My programming skill, trying to find out the best way of programming insertion sort for integers.
Which i already know there could be better algorithm such as counting, bucket, radix ...

Thanks,
Stefan_Lang 25-Jul-14 4:36am    
The thing with algorithms is that there is usually not a 'best' algortihm for anything, until you specify what it is used for, and what are the properties of the data they should be applied to. If the insertion happens maybe five times in the lifetime of the container that hosts a thousand elements, then you couldn't care less about the performance. If the container is being constructed through insertion however, you should strongly consider using a different data structure - such as a list - that better fits the usage of your data!

Next thing to consider is: what are your performance requirements - do you have a need to guarantee a result within a certain amount of time, or are you just concerend about the average performance? Some algorithms may be better suited at either of the two, but ceratinly not at both. E. g. for sorting, Quicksort is very good on average, but bad for the worst case.

Another thing that may or may not be an issue is memory: do the data fit easily into memory, allowing to construct temporary helper containers of similar size, or do you need to wotk in-place? Can you even load the entirety of the data into memory or just chunks of it? Only some algorithms may be suitable for working on local subsets of data. Others may require to look at the entire set in memory.

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 use std::list for that purpose and focus on the details of your sort algorithm - no point in reinventing the wheel list ;-)
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900