Insertion and selection sort

This is a discussion on Insertion and selection sort within the C Programming forums, part of the General Programming Boards category; Hi, I'm having a problem while sorting the values using the selection sort and inserion sort alogos. Please someone tell ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    11

    Insertion and selection sort

    Hi,
    I'm having a problem while sorting the values using the selection sort and inserion sort alogos. Please someone tell me where I'm mistaking...?

    Here's the code for selection sort:

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main (void)
    {
    clrscr();
    int array[10],x,y;
    for (x=0;x<=9;x++)
    {
    printf("Enter Number: ");
    scanf("%d",&array[x]);
    }
    for(x=0;x<10;x++)
    {
    int i = x;
    for(int y=x; y<10; y++)
    {
    if(array[i]<array[y])
    {
    i=y;
    }
    }
    int t=array[x];
    array[x]=array[i];
    array[i]=t;
    }
    printf ("\nAFTER SELECTION SORTING");
    printf ("\n***** ********* *******");
    for (x=0;x<=9;x++)
    {
    printf("\n%d",&array[x]);
    }
    getch();
    }
    and here's the code for insertion sort:
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main(void)
    {
    int array[10],x,y,z=0;
    clrscr();
    for (x=0;y<=9;y++)
    {
    printf("ENTER NUMBER: ");
    scanf("%d",&array[x]);
    }
    for (x=0;x<=9;x++)
    {
    if (array[0]>array[x])
    {
    z=array[x];
    array[x]=array[y];
    array[y]=z;
    for (y=x;y<=0;y--)
    {
    array[y]=array[y-1];
    }
    array[0]=z;
    }
    }
    printf ("\nAFTER INSERTION SORTING");
    printf ("\n******  ********************");
    for (x=0;x<=9;x++)
    {
    printf("\n\t%d",array[x]);
    }
    getch();
    }
    Sorry for disturbing you guys with such basic things. Please reply me as soon as possible.

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > #include<conio.h>
    This isn't a standard header file. If you replace getch() with getchar(), then there is no need for it.
    Also read the FAQ entry on stopping your DOS window from closing at the end of the program.

    > void main(void)
    main returns int - see the FAQ

    > clrscr();
    > int array[10],x,y;
    C doesn't support mixed declarations and statements. I'd say you're using a C++ compiler to compile C code.
    This will cause you problems later on.

    > for(int y=x; y<10; y++)
    Ditto - this kind of loop declaration is a C++ feature.
    It's also a C99 feature, but since you used conio.h, I figure you're using something old like TurboC

    > for (x=0;x<=9;x++)
    The more usual form is
    for (x=0;x<10;x++)
    because 10 is the size of your arrays.

    It would be better if you had say
    #define ASIZE 10
    then later on
    for (x=0;x<ASIZE;x++)

    Oh, and your indentation sucks.
    Try and make it so it looks more like this
    Code:
    for (x=0;x<=9;x++)
    {
        printf("\n\t%d",array[x]);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    there should be C code found here - but it deals with arrays :
    basic C version for insertion sort
    (arrays only)


    selection sort

    While we're on the same topic - does anyone have links to source code or lect material regarding sorting lists using merge, selection and quick sort? I.e. applying this material to linked lists? I know its alot of pointer nitty gritty but I crashed the compiler trying out a merge sort for a doubly linked list.

    edit: fixed. Thanks dwks
    Last edited by trickae2; 08-29-2006 at 04:41 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    "[url = " -> "[url=" (no spaces).

    While we're on the same topic - does anyone have links to source code or lect material regarding sorting lists using merge, selection and quick sort? I.e. applying this material to linked lists? I know its alot of pointer nitty gritty but I crashed the compiler trying out a merge sort for a doubly linked list.
    You might try aihorizon.com (although I think they only have C++ code there, which is difficult for a C progammer to read, containing as it does templates.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    Thanks i'll check it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Insertion Sort on Array of Structs
    By n0r3gr3tz in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 08:28 AM
  3. Insertion sort
    By Hypercase in forum C Programming
    Replies: 9
    Last Post: 08-31-2004, 04:02 AM
  4. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  5. selection sort?
    By dharh in forum C Programming
    Replies: 4
    Last Post: 03-17-2002, 01:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21