Still Needing Help : selection sorting

This is a discussion on Still Needing Help : selection sorting within the C Programming forums, part of the General Programming Boards category; I am still needing understanding of why my largest_element function is not working properly. What I gather is that 30 ...

  1. #1
    Unregistered
    Guest

    Question Still Needing Help : selection sorting

    I am still needing understanding of why my largest_element function is not working properly.

    What I gather is that 30 elements are randomly generated in an array. The values of which are between 1 and 200. (DONE)

    Then another function is called to find the largest element. Now I don't know if I need to return the index of the largest element of the array (that is how it seems) and use this in the selection sorting (ascend_sort and descend_sort) functions or not.

    Can someone explain???
    finding largest_element
    sorting functions


    PHP Code:
    #include <stdio.h> 
    #include <time.h> 
    #include <stdlib.h> 

    #define NUMELEM 30 
    #define MAXELEM 200 

    void ascend_sort(int a[],int n); 
    void descend_sort(int a[], int n); 
    void random_array(int a[], int n); 
    int largest_element (int a[], int fint l); 

    main() 

    int array[NUMELEM]; 
    int last=0

    /* create array */ 

    random_array(array,NUMELEM); 

    /* need to call function largest_element and assign value*/ 
    /* returned to a variable to be used in the selection_sort */
    /* functions named ascend_sort and descend_sort          */

    last=largest_element(array,array[0],array[NUMELEM-1]); 

    printf("%d",last); 

    /* find largest element */ 

    /* FUNCTION: largest_element : my resource text says to */ 
    /* make i and j point to the first element of array (but it */ 
    /* does not assume a[0] is the first element for some */ 
    /* reason; f and l are representing first and last elements */ 
    /* of array passed */ 

    int largest_element(int a[], int fint l

            
    int i=f
            
    int j=f

             do { 
                        
    i++; 
                        if (
    a[i] > a[j]) { 
                                  
    j=i
                        } 
             } while (
    != l); 

             return 
    j


    /* sort in ascending order using selection sorting */ 

    void ascend_sort(int a[],int n


    int largest
    int temp






    /* sort in descending order using selection sorting */ 

    void descend_sort(int a[], int n





    void random_array(int a[], int n

        
    int i

        for (
    i=0NUMELEM i++) 

             { 
                   
    a[i] = rand()%MAXELEM+1
                       
    printf("%3d ",a[i]); 

                      if ((
    i+1)%10==0
                      
    printf("\n"); 
             } 


  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    //call
    random_array(array,NUMELEM); 
    
    //definition
    void random_array(int a[], int n) 
    { 
        int i; 
    
        for (i=0; i < NUMELEM ; i++) 
    
             { 
                   a[i] = rand()%MAXELEM+1; 
                       printf("%3d ",a[i]); 
    
                      if ((i+1)%10==0) 
                      printf("\n"); 
             } 
    }
    Why would you have an arguement NUMELEM dictated by the parameter n and not use it in your function. n is NUMELEM. Either use it instead of NUMELEM or get rid of it. One way to eliminate your #define would be to pass the size like this: sizeof(array)/sizeof(array[0])

    The largest element is found by circualting throgh the array. Just pass the array arguement and size.

    Code:
    last=largest_element(array, sizeof(array)/sizeof(array[0]);
    
    int largest_element(char a[], int size)
    {
       int largest = 0;
       int i;
    
       for(i=0; i < size; i++)
       {
          if(array[i] > largest) largest = array[i];
       }
        return largest;
    }
    I'd have to look up selection sort. Don't feel like doing that right now. I only know bubble sort by heart.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    last=largest_element(array,array[0],array[NUMELEM-1]);
    I think you meant
    largest_element(array,0,NUMELEM-1);


    also another thing , the way largest_element is implemented may run into trouble

    int..largest_element(int..a[],..int..f,..int..l)
    {
    ................int..i=f;
    ................int..j=f;
    ..................do..{
    ........................................i++;
    ........................................if..(a[i]..>..a[j])..{
    ............................................................j=i;
    ........................................}
    ..................}..while..(i..!=..l);
    ..................return..j;
    }

    you seem to implicitly assume f < l
    suppose I want to find the maximum element of an array with 1 element ,
    suppose a is defined in main as
    int a[]={0,2,3};
    so , suppose I call it as
    last=largest_element(a,0,0);
    your function will return 1 , not 0 .
    of course you can just add a line after the declarations to solve this problem , if you dont want to change the do{...}while(); loop to a while loop or a for loop for some reason.

    int..largest_element(int..a[],..int..f,..int..l)
    {
    ................int..i=f;
    ................int..j=f;
    if(l==f) return l;
    ..................do..{
    ........................................i++;
    ........................................if..(a[i]..>..a[j])..{
    ............................................................j=i;
    ........................................}
    ..................}..while..(i..!=..l);
    ..................return..j;
    }

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I was wrong , in case of an array with one element , your function wont return wrong value , it will become an infinite loop as the condition (i==l) will never be true , but the change I suggested should work .
    Originally posted by pinko_liberal
    last=largest_element(array,array[0],array[NUMELEM-1]);
    I think you meant
    largest_element(array,0,NUMELEM-1);


    also another thing , the way largest_element is implemented may run into trouble

    int..largest_element(int..a[],..int..f,..int..l)
    {
    ................int..i=f;
    ................int..j=f;
    ..................do..{
    ........................................i++;
    ........................................if..(a[i]..>..a[j])..{
    ............................................................j=i;
    ........................................}
    ..................}..while..(i..!=..l);
    ..................return..j;
    }

    you seem to implicitly assume f < l
    suppose I want to find the maximum element of an array with 1 element ,
    suppose a is defined in main as
    int a[]={0,2,3};
    so , suppose I call it as
    last=largest_element(a,0,0);
    your function will return 1 , not 0 .
    of course you can just add a line after the declarations to solve this problem , if you dont want to change the do{...}while(); loop to a while loop or a for loop for some reason.

    int..largest_element(int..a[],..int..f,..int..l)
    {
    ................int..i=f;
    ................int..j=f;
    if(l==f) return l;
    ..................do..{
    ........................................i++;
    ........................................if..(a[i]..>..a[j])..{
    ............................................................j=i;
    ........................................}
    ..................}..while..(i..!=..l);
    ..................return..j;
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    hi there... in case you want to see...
    this is tutorial about selection sort:
    http://www.cpp-home.com/tutorial.php?113_1

    and this is a code that uses the selection sort:
    http://www.cpp-home.com/code.php?82_1
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  6. #6
    Unregistered
    Guest
    hi there... in case you want to see...
    this is tutorial about selection sort:
    http://www.cpp-home.com/tutorial.php?113_1
    This website is way cool, but I don't know how to convert
    C++ code into C. I am new to programming in any language. Took C as first language, having many troubles with pointers, structures and recursive functions.

    Can you explain how to convert to C source code???
    Just a few pointers would be a start, I suppose.

    I was trying to set a separate function to find largest element but this C++ code has that function in a loop in the main() to and just a separate function to do the selection sorting.

  7. #7
    Unregistered
    Guest
    I think you could put a loop in your main function to
    check to see if an element is larger than the next

    similar to bubble sorting

    review Troll King's code, it makes sense !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. selection problem
    By Ken JS in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 10:47 PM
  2. Selection sort, not sorting
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 04-23-2007, 12:17 PM
  3. Selection sorting algorithm
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2004, 05:37 PM
  4. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 09:05 PM

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