selection sorting

This is a discussion on selection sorting within the C Programming forums, part of the General Programming Boards category; program is to sort an array in ascending and descending order using selection sorting. Here are some of my questions ...

  1. #1
    Unregistered
    Guest

    selection sorting

    program is to sort an array in ascending and descending order using selection sorting.

    Here are some of my questions right now. I need a bit of guidance to continue on.

    Output of run of program thus far only helps to see the original array (see bottom of post)

    (1) do I need two separate functions to sort in ascending and descending order -- the original array is sorted twice
    (2) do I need 3 parameters if I define number of elements of original array -- textbook explains that one int variable is the first element and the other int variable is the last element in array
    (3) is the function find_largest_element to return a value or anything at all???


    Code:
      #include <stdio.h>
      #include <time.h>
      #include <stdlib.h>
    
      #define NUMELEM 30
      #define MAXELEM 200
    
      void ascend_sort(int a[],int b, int e);
      void descend_sort(int a[], int e, int b);
      void random_array(int a[], int b);
      void find_largest_element (int a[], int b, int e);
            
    
      main()
      {
        int array[NUMELEM];
        int first;
        int last;
    
        /*  create array                                     */
        /*  find largest element                             */
        /*  sort in ascending order using selection sorting  */
        /*  sort in descending order using selection sorting */
        /*  print new arrays                                 */
    
        random_array(array,NUMELEM);
    
      }
    
      void find_largest_element(int a[], int b, int e)
      {
            
     }
    
      void ascend_sort(int a[],int b, int e)
    {
      int largest;
      int temp;
    
    
    
    
    }
    
    void descend_sort(int a[], int e, int b)
    {
    
    
    
    }
    
    void random_array(int a[], int b)
    {
       int i;
    
       for (i=0; i < NUMELEM; i++)
    
         {
            a[i] = rand()%MAXELEM+1;
            printf("%3d  ",a[i]);
    
              if ((i+1)%10==0)
                printf("\n");
         }
    }
    gcc selecting.c
    a.out
    39 159 114 116 52 28 11 20 13 87
    150 168 85 61 26 144 90 184 138 167
    167 179 96 112 168 55 32 146 83 137

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Your best bet would be to use a linked list. That way it is easy to sort it and add at will. This seems to be the best way for your problem.

  3. #3
    Unregistered
    Guest

    can't use linked lists

    haven't learned linked lists yet

    know recursion
    know array/pointers (so-so)

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Oh. Let me think. I'll get back to you later.

  5. #5
    Unregistered
    Guest

    finding largest element of array

    I am working on the function largest_element that seeks to find largest element of array. Now I am not sure if I set it up correctly to find the largest element. And also, I am not sure if call it correctly, (or have understandable arguments).

    [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 f, int l);

    main()
    {
    int array[NUMELEM];
    int last=0;

    /* create array */
    /* find largest element */
    /* sort in ascending order using selection sorting */
    /* sort in descending order using selection sorting */
    /* print new arrays */

    random_array(array,NUMELEM);

    /* need to call function largest_element to use later */

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

    printf("%d",last);
    }

    /* 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 f, int l)
    {
    int i=f;
    int j=f;

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

    return j;
    }

    void ascend_sort(int a[],int n)

    {
    int largest;
    int temp;




    }

    void descend_sort(int a[], int n)
    {


    }

    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");
    }
    }

  6. #6
    Unregistered
    Guest

    last post messed up

    REPOST

    I am working on the function largest_element that seeks to find largest element of array. Now I am not sure if I set it up correctly to find the largest element. And also, I am not sure if I call it correctly, (or have understandable arguments). Something is not
    correct because when I run it, I am not getting the right value for last (which is variable returned from function largest_element).

    Please help.

    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 f, int l); 
    
    main() 
    { 
    int array[NUMELEM]; 
    int last=0; 
    
    /* create array */ 
    /* find largest element */ 
    /* sort in ascending order using selection sorting */ 
    /* sort in descending order using selection sorting */ 
    /* print new arrays */ 
    
    random_array(array,NUMELEM); 
    
    /* need to call function largest_element to use later */ 
    
    last=largest_element(array,array[0],array[NUMELEM-1]); 
    
    printf("%d",last); 
    } 
    
    /* 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 f, int l) 
    { 
            int i=f; 
            int j=f; 
    
             do { 
                        i++; 
                        if (a[i] > a[j]) { 
                                  j=i; 
                        } 
             } while (i != l); 
    
             return j; 
    } 
    
    void ascend_sort(int a[],int n) 
    
    { 
    int largest; 
    int temp; 
    
    
    
    
    } 
    
    void descend_sort(int a[], int n) 
    { 
    
    
    } 
    
    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"); 
             } 
    }

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, 09:47 PM
  2. Selection sort, not sorting
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 04-23-2007, 11:17 AM
  3. Selection sorting algorithm
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2004, 04: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, 01:29 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM

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