Insert sort help

T

temp999

I was at this web page: http://http://members.shaw.ca/ipatters/ and i
was wondering if someone could help me with an insert sort algorithm. I
made it all the way to Part 6 until things got tough.

The insert sort is explained but, I still don't get it. Why does it
have the name insert sort when it seems to merge stuff...? Maybe I'm
wrong. Ideas?

Frankeez.
 
R

Robert W Hand

I was at this web page: http://http://members.shaw.ca/ipatters/ and i
perhaps,

http://members.shaw.ca/ipatters/BeginC_6.html

was wondering if someone could help me with an insert sort algorithm. I
made it all the way to Part 6 until things got tough.

The insert sort is explained but, I still don't get it. Why does it
have the name insert sort when it seems to merge stuff...? Maybe I'm
wrong. Ideas?

The author gives a rather detailed explanation of his code. What did
you not understand? As far as the name, he offers this comment line,
" /* Move the others down and insert it. */".

Best wishes,

Bob
 
C

Carsten Hansen

temp999 said:
I was at this web page: http://http://members.shaw.ca/ipatters/ and i
was wondering if someone could help me with an insert sort algorithm. I
made it all the way to Part 6 until things got tough.

The insert sort is explained but, I still don't get it. Why does it
have the name insert sort when it seems to merge stuff...? Maybe I'm
wrong. Ideas?

Frankeez.

Here is a rewrite of the program. Maybe the output makes it easier to
understand the algorithm.

Carsten Hansen


#include <stdio.h>

void print_array(int array[], int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%5d", array);
}
printf("\n");
}

#define SIZE 10

int main(void)
{
int Num[SIZE] = {234, 212, 0, 21, 14, 175, 998, 401, 1232, 110};
int i, j;
int tmp;

printf("Original array\n");
print_array(Num, SIZE);

/* Insert Sort */
for (i = 1; i < SIZE; i++)
{
printf("\nNext i = %d. Move Num[%d] = %d to its right position.\n",
i, i, Num);
print_array(Num, SIZE);
for (j = i; (j > 0) && (Num[j - 1] > Num[j]); j--)
{
tmp = Num[j];
Num[j] = Num[j - 1];
Num[j - 1] = tmp;

print_array(Num, SIZE);
}
}

printf("\nSorted array\n");
print_array(Num, SIZE);

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top