# Still Needing Help : selection sorting

• 10-14-2001
Unregistered
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 f, int 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 f, int l)  {          int i=f;          int j=f;           do {                      i++;                      if (a[i] > a[j]) {                                j=i;                      }           } while (i != 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=0; i < NUMELEM ; i++)           {                 a[i] = rand()%MAXELEM+1;                     printf("%3d ",a[i]);                    if ((i+1)%10==0)                    printf("\n");           }  }  ```
• 10-14-2001
Troll_King
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.
• 10-14-2001
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;
}
• 10-14-2001
pinko_liberal
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 .
Quote:

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

• 10-14-2001
loobian
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
• 10-14-2001
Unregistered
Quote:

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.
• 10-14-2001
Unregistered
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 !!