Insert sort help

Discussion in 'C Programming' started by temp999, Aug 11, 2003.

  1. temp999

    temp999 Guest

    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.
     
    temp999, Aug 11, 2003
    #1
    1. Advertising

  2. On Mon, 11 Aug 2003 08:09:16 GMT, temp999 <> 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
     
    Robert W Hand, Aug 11, 2003
    #2
    1. Advertising

  3. "temp999" <> wrote in message
    news:MaIZa.725772$...
    > 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;
    }
     
    Carsten Hansen, Aug 11, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    0
    Views:
    568
    nobody
    Jun 1, 2004
  2. JerryJ
    Replies:
    11
    Views:
    1,432
    Dave Moore
    Apr 28, 2004
  3. John Black
    Replies:
    6
    Views:
    2,113
    John Harrison
    May 28, 2004
  4. Navin
    Replies:
    1
    Views:
    766
    Ken Schaefer
    Sep 9, 2003
  5. GIMME
    Replies:
    5
    Views:
    201
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page