Thread: sorting question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    sorting question

    I have a linked list I am trying to sort... I wrote this function to sort it.... I go through it logically and I don't see why it wouldn't work.... does anyone know a better, simpler way to sort a linked list to change it to an ascending order?

    this isn't working for me, but I dont know why:

    Code:
    void sort_list(STUDENT* stud) {
         
         STUDENT* first;
         STUDENT* second;
         STUDENT temp_student;
         
         first = stud;
            
         while( first != NULL ) {
                
                second = first -> studentPtr ;
                
                while( second != NULL ) {
                       
                       if ( (first -> ID) < (second -> ID) ) {
                            
                            temp_student.ID = first->ID;
                            strcpy(temp_student.name,first->name);
                            first->ID = second->ID;
                            second->ID=temp_student.ID;
                            strcpy(second->name,temp_student.name);
                            
                            strcpy(temp_student.name,"");
                            temp_student.ID = 0;
                            
                            }
                               second = second -> studentPtr;
                             }
                               first = first -> studentPtr;
                            }
                            }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    I would like all opinion

    With your methed you might be moving big segments of memory.
    ( You sort the data not the list).
    I would use an array of pointers, with each points to a cell.
    Than simply qsort this array. and build the list from the array.


    Code:
    void  sort_list(STUDENT  *stud, int  n_elm)
    {
    STUDENT   **ppstud;
    int       i, j;
    
    
    if ( ! stud || ! stud->studentPtr )
        return;
    
    if ( ! ( ppstud = malloc(n_elm*sizeof(STUDENT*)) ) )
       {
        printf("error malloc\n");
        return;
       }
    
    i = 0;
    for ( stud ; stud ; stud->studentPtr )
        ppstud[i++] = stud; 
    
    qsort(ppstud, i, sizeof(STUDENT*), comp_int);
    for ( j = 0 ; j < i - 1 ; ++j )
       ppstud[j]->studentPtr = ppstud[j+1];
    ppstud[j]->studentPtr = NULL;
    
    stud = ppstud[0];
    free(ppstud);
    return(stud);
    }
    
    
    int comp_int(const void *a, const void *b)
    {
    return(   (*(STUDENT**)a)->ID,  (*(STUDENT**)b)->ID  );
    }

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    for ( stud ; stud ; stud->studentPtr )
    This does nothing except loop forever if stud isn't NULL. It's the same as writing:
    Code:
    for ( ; stud ; )
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    You are dead wrong

    This is a linked list !!!

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    No, I am definitely not dead wrong. It has nothing to do with the fact that it's a linked list you're "attempting" to walk through. The value of stud never changes throughout your loop so every element in your ppstud array will get set to the same value. Since stud never changes you'll soon overrun the bounds of your ppstud array and start stomping all over memory you shouldn't until the program crashes with a segmentation fault or whatever your OS reports when you touch memory you shouldn't. At any rate, it's not going to do what you want.

    Go ahead and try it if you don't believe me. You need to read up on for() loops.
    Last edited by itsme86; 11-22-2005 at 08:45 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Sorry U are right.

    Sorry again missed it. That is wrong :

    for ( stud ; stud ; stud->studentPtr )

    correct :
    for ( stud; stud ; stud = stud->studentPtr )

    Again Very sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Sorting Question
    By Rkukulski in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 01:37 PM
  3. Question sorting character arrays: C programming
    By NeoSigma in forum C Programming
    Replies: 3
    Last Post: 05-23-2003, 09:28 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. an actual question (sorting arrays)
    By master5001 in forum C Programming
    Replies: 4
    Last Post: 08-13-2001, 10:21 PM