Can someone please explain this algorithm to me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using namespace System;
	void main()
	{
		 
            int n;
            int key;
            int i;
            int A[] = { 66,55,444,33,22,111};
			n = sizeof(A)/sizeof(int);
            for (int ii = 0; ii < n; ii++)
			{ Console::Write("{0},", A[ii]); }
             for (int j = 1; j < n; j++)
            {
                key = A[j];
                i = j - 1;
				while (i > -1 && A[i] > key)
                {
                    A[i + 1] = A[i];
                    i--;
                  
                }
              	 A[i + 1] = key;
                }
			Console::WriteLine();
			Console::WriteLine("==========");
            for (int ii = 0; ii < n; ii++)
			{ Console::Write("{0},", A[ii]); }
			Console::WriteLine();
           
        }

My teacher sent me this, and it absolutely confuses me, can someone explain this in few words?
Bubble sort?
No, this is Insertion sort.
http://en.wikipedia.org/wiki/Insertion_sort
Either bubble sort or insertion, you probably noticed that English is not my native language so I have trouble understanding articles on wikipedia, which is why I need someone to explain this to me in basic, simple English, I know you guys hate these kind of stuff but I seriously have no where else to go.
btw, is that C++ or C#?
@chipp
btw, is that C++ or C#?


It is C++ with C#.:) and named as C++/CLI.
It's C++, C# wouldn't stomach int a[].
noobletplusplus
It's C++, C# wouldn't stomach int a[].


Please, read what I already wrote. It is C++/CLI.
Guys
Can you tell me what does this mean

>n = sizeof(A)/sizeof(int);

And what's sizeof(int)?
closed account (Dy7SLyTq)
sizeof() is a function that is passed a value and returns how much space it takes up
closed account (zb0S216C)
"sizeof" is not a function, it's an operator. Programmers tend to use the functional notation which can lead them to believe that "sizeof" is a function. For instance, either way is legal C++:

1
2
sizeof int;
sizeof( int );

The same can be said for "return" except the operator part.

Wazzak
Last edited on
closed account (Dy7SLyTq)
ooohhh that makes sense
n = sizeof(A)/sizeof(int)
This calculates how many items (of type int) are in array A;

sizeof(A) - returns the number of bytes array "A" takes up
sizeof(int) - returns the number of bytes "int" takes up

Thus from your code above, the value for n = 6

---
Rajinder Yadav <labs@devmentor.org>

DevMentor Labs
http://labs.devmentor.org
Creating Amazing Possibilities
Topic archived. No new replies allowed.