Insertion Sorting

I literally don't understand anything about coding in C++ (I'm new to coding in general, and my professor unfortunately isn't very helpful.) My assignment for today is: "Write a C++ program to sort a list of 10 integers using insertion sort." I understand what insertion sorting is and I've watched about a million tutorials so far and cannot figure out how to write a c++ code for it. Can somebody explain to me how to do this assignment in C++?
That is an advanced program to try to start with.

You say you do not understand anything.
So let me ask: can you code the hello world program?
If yes, can you give us a better idea of what you know? Have you not had a class already or several weeks of classes if this is your first? I am having trouble thinking anyone would ask this as your first assignment on your first week of your first class.
Last edited on
I wouldn’t classify an insertion sort as anything anywhere near “advanced”.

In fact, I’d call it “beginner”.

I could even see this being your very first assignment. More on insertion sort:
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/insertion-sort/


You are in university now, so you aren’t going to have how to do everything handed to you. You’ll be expected to get C++ set up and running and be able to create and compile a simple program all on your own.

As jonnin said, get a hello world program running.

1
2
3
4
5
6
#include <iostream>

int main()
{
  std::cout << "Hello world!\n";
}

Once you get that to compile and execute, then you can work on adding an array of ten integers. You do not specify whether those integers are static, randomly-generated, or obtained from the user (which I recommend), but pick one and do it. Also figure out how to print them. Both the getting and the printing require a loop.

Once you get that far, then you can consider sorting the array. This will require two loops, one inside the other. Remember to think about the indexing of the array — very much like looping over the elements to print, except now you have a loop inside the outer loop that counts backwards, and you have to know how to swap elements in the array.

For example, given an array of integers:

    1 2 4 5 6 7 4

To insert the last 4 into the correct spot, keep swapping until it is in the correct space:

    1 2 4 5 6 4 7
    1 2 4 5 4 6 7
    1 2 4 4 5 6 7

If you need extra help, go to your university's help center. The computer lab often has someone who is willing to help you as well. If necessary, schedule time to talk to your professor during his office hours and he can direct you in the best way to catch up. (Swallow any pride or shame that may be keeping you from him; he doesn’t want you to fail the course any more than you do.)

Once you get some code working, post here as well. We will help you understand and fix it.

(We just aren’t very keen on doing your work for you.)
well, one would expect him to have seen an array, a loop, a function, etc. before taking this on.
Its not 'super advanced' but its a bit much if he really, actually has zero background to date.
Agreed about his seeing an array, loop, function, etc.
It’s still first semester 101 stuff.
Registered users can post here. Sign in or register to post.