insertion sort error

This is a discussion on insertion sort error within the C++ Programming forums, part of the General Programming Boards category; hi, i have this code for insertion sort. the only problem i have with it is that the very first ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    84

    insertion sort error

    hi,

    i have this code for insertion sort. the only problem i have with it is that the very first item in the array is not being sorted. i can't put my finger on it. could someone please tell me where i went wrong?

    barneygumble742

    Code:
    #include <iostream>
    using namespace std;
    
    int *ins_sort(int *array, int size);
    
    int main()
    {
    int i=0, size=0, x=0;
    
    int A[] = {5, 12, 2, -3, 0, 1, 1, 2, 4, 12};
    
    	size = sizeof(A)/sizeof(A[0]);
    	cout << "this array has " << size << " items." << endl << endl;
    
    	ins_sort(A, size);
    
    	for(i=0; i<size; i++)
    	{
    		cout << A[i] << endl;
    	}
    
    return 0;
    }
    
    int *ins_sort(int *array, int size)
    {
    int i=0, j=0, key=0;
    int *start = array;
    
    	for(i=1; i<size; i++)
    	{
    		key = array[i];
    		j = i-1;
    		while (j>0 && array[j]>key)
    		{
    			array[j + 1] = array[j];
    			j--;
    		}
    		array[j+1] = key;
    	}
    
    return start;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The first time through your for loop, j = 0. Does your while loop then execute? Does array[0] ever get examined?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    for(i=1; i<size; i++)
    That should start at zero, although I realize that's not possible because of this line:
    Code:
    j = i-1;
    I would use an if to handle the first case (i==0).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    Code:
    while (array[j]>key)
    hi...that is the change i made and it works perfectly now.

    in the for loop, it had the same problem even when @ i=1;

    thanks,
    barneygumble742

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21