Thread: Sorting algorithms

  1. #1
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24

    Sorting algorithms

    I have a code
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main
    {
    int n;
    int num[100];
    int L;
    int desnum[100], k;
    int i, i, temp;
    printf("\nEnter the total number of marks to be entered: ");
    scanf("%d",&n);
       clrscr();
    for(L=0;L<n;L++)
    {
    printf("\n Enter the marks of student %d : ", L+1);
    scanf("%d",&num[L]);
    }
    for(k=0;k<n;k++)
    {
          desnum[k]= num[k]
    }
    for(i=0;i<n-1;i++)
    {
    for(j = i+1; j<n; j++)
    {
    if(desnum[i]<desnum[j])
    {
                temp = desnum[i];
                desnum[i]= desnum[j];
                desnum[j]= temp;
    }
    }
    }
    for(i=0;i<n;i++)
    printf("\nNumber at [%d] is %d", i, desnum[i]);
    }
    Can you tell me effection of this section
    Code:
    for(k=0;k<n;k++)
     {
           desnum[k]= num[k]
     }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That loop copies one array into another.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by MK27 View Post
    That loop copies one array into another.
    Oh. But i think don't need that loop . Code still have not error.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yeah you need that loop.... look at what happens next...

    This is one of the problems you run into when you don't write your own code...
    You end up with programs that you don't know how to fix.

  5. #5
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    Yeah you need that loop.... look at what happens next...

    This is one of the problems you run into when you don't write your own code...
    You end up with programs that you don't know how to fix.
    No . That is a examples in book .

    I think code only like this :
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int n;
        int num[100];
        int L;
        int i,j,temp;
        printf("\nEnter the total number of marks to be entered: ");
        scanf("%d",&n);
        for(L=0;L<n;L++)
        {
            printf("\n Enter the marks of student %d: ",L+1);
            scanf("%d",&num[L]);
        }
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(num[i]<num[j])
                {
                  temp = num[i];
                  num[i] = num[j];
                  num[j]= temp;
                }
            }
        }
        for(i=0;i<n;i++)
            printf("\n Number at [%d] is [%d]",i,num[i]);
    }
    Why we need copies array to another array ?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ToNy_ View Post
    I think code only like this :
    Looks good. Bubblesort, right?

    Why we need copies array to another array ?
    Perhaps so you could compare the sorted array to the unsorted one? Anyway, you are correct, you don't have to have to copies for this sort.

    Actually, you don't need a temp variable either:

    Code:
                  num[i] += num[j];
                  num[j] = num[i] - num[j];
                  num[i] -= num[j]
    Last edited by MK27; 12-14-2011 at 09:13 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by MK27 View Post
    Looks good. Bubblesort, right?
    Actually, you don't need a temp variable either:

    Code:
                  num[i] += num[j];
                  num[j] = num[i] - num[j];
                  num[i] -= num[j]
    I have known that. Is that assign swap two variables?
    Like this :
    B = A-B;
    A = A-(-B)
    B = A +B

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ToNy_ View Post
    I have known that. Is that assign swap two variables?
    Like this :
    B = A-B;
    A = A-(-B)
    B = A +B
    Looks like the same thing only different, yeah. I'm actually not sure of the real value; I think doing the arithmetic makes it more expensive processor wise than using the temp variable, which is silly just to save one int in memory. I could be wrong about that tho. Either way it is not a big deal.

    I just like it because I am easily impressed by math tricks
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    Looks like the same thing only different, yeah. I'm actually not sure of the real value; I think doing the arithmetic makes it more expensive processor wise than using the temp variable, which is silly just to save one int in memory. I could be wrong about that tho. Either way it is not a big deal.

    I just like it because I am easily impressed by math tricks
    You may be right... since it's possible optimizations will hold the temp value in a CPU register but if you're changing the value of variables, it has to write them out and read them back in, to preserve the memory data.

  10. #10
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Thank a lots Mk27 vs CommomTater . Today i have learned swap two variables and Bubbles sorting algorithms .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Sorting Algorithms
    By ashir30000 in forum C++ Programming
    Replies: 9
    Last Post: 05-21-2009, 09:27 AM
  3. Sentinel in sorting algorithms
    By Albinoswordfish in forum C Programming
    Replies: 2
    Last Post: 12-17-2008, 03:21 AM
  4. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  5. recommendations on sorting algorithms
    By btq in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2002, 11:36 AM