Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Insert sort help

Reply
Thread Tools

Insert sort help

 
 
temp999
Guest
Posts: n/a
 
      08-11-2003
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.

 
Reply With Quote
 
 
 
 
Robert W Hand
Guest
Posts: n/a
 
      08-11-2003
On Mon, 11 Aug 2003 08:09:16 GMT, temp999 <(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
 
 
 
Carsten Hansen
Guest
Posts: n/a
 
      08-11-2003

"temp999" <(E-Mail Removed)> wrote in message
news:MaIZa.725772$(E-Mail Removed). ca...
> 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[i]);
}
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[i]);
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;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP!! anyone ??can help me about my project "quick sort implemented with shell sort? comsciepartner General Computer Support 0 10-06-2008 01:02 PM
Re: When will Thunderbird support sort in place (in context sort)? Ron Natalie Firefox 0 02-02-2006 04:38 AM
Array sort function sorts on chars not numbers ... help ! how to sort numbers GIMME Javascript 5 07-26-2004 01:28 AM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments