Click here to Skip to main content
13,795,074 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Here is a code for insertion sort which does not sort the array elements that i enter..
Help please!!

#include <iostream>
#
void insertion()
{
    int j = 0, i=0,temp,A[12],N;
    std::cout<<"Enter the no of elements";

    std::cin>>N;

    std::cout<<"Enter the array elements";

      std::cin>>A[i];

    for(i=1;i<N-1;i++) {

        i = j - 1;

        temp = A[i];

        while (temp > A[j] && j >= 0) {
            A[j + 1] = A[j];
            j--;
        }
        j++;
        A[j] = temp;
    }
    j--;
    std::cout<<A[temp];

}



int main() {

    insertion();

    return 0;

}



What I have tried:



Swapping the positon of main and the function
Posted 13-Oct-18 9:46am
Updated 13-Oct-18 12:40pm
Comments
phil.o 13-Oct-18 15:23pm
   
Put a breakpoint at the beginning of your insertion function, then launch a debug session and watch, line by line, the content of your variables. You will be able to spot several issues. Debugging is far from optional for a developer, it is one of the core skills to apply and master. Moreover, you may even find it quite funny actually, because you will be able to see exactly what is going on, instead of randomly moving your functions around and hope for the problem to disappear. The placement of functions relative to each other is not relevant in C/C++; whether you define the main function before or after the insertion function will not affect the final result.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Quote:
Here is a code for insertion sort which does not sort the array elements that i enter..

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

To fix your code:
#include <iostream>

void insertion_sort(int a[], size_t size)
{
  for (size_t i = 1; i < size; ++i)
  {
    int temp = a[i];
    int j = i-1;
    while ( temp < a[j] && j >= 0)
    {
      a[j+1] = a[j];
      --j;
    }
    a[j+1] = temp;
  }
}

enum { max_size = 12 };

int main()
{
  int a[max_size];
  size_t size;

  std::cout<<"Enter the no of elements ";
  std::cin>>size;

  if ( size > max_size)
    return -1;

  std::cout<<"Enter the array elements\n";

  for (size_t n=0; n<size; ++n)
    std::cin >> a[n];

  insertion_sort( a, size);

  for (size_t n=0; n<size; ++n)
    std::cout << a[n] << " ";
  std::cout << "\n";
}



I would use std::vector, however:
#include <iostream>
#include <vector>

void insertion_sort(std::vector <int> & v)
{
  size_t size = v.size();
  for (size_t i = 1; i < size; ++i)
  {
    int temp = v[i];
    int j = i-1;
    while ( temp < v[j] && j >= 0)
    {
      v[j+1] = v[j];
      --j;
    }
    v[j+1] = temp;
  }
}

int main()
{
  std::vector<int> v;
  size_t size;

  std::cout<<"Enter the no of elements ";
  std::cin>>size;

  v.resize(size);
  std::cout<<"Enter the array elements\n";

  for (auto & x: v)
    std::cin >> x;

  insertion_sort( v );

  for (const auto & x : v)
    std::cout << x << " ";
  std::cout << "\n";
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.181207.3 | Last Updated 13 Oct 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100