# Selection Sort confusion

• 11-30-2001
Crankit211
Selection Sort confusion...still
I've checked the FAQ's, text and several web sites and can't seem to find much on selection sorting. I've gotten my program to compile, but my output is not correct on one of the functions. Any help would be greatly appreciated.
Here's the code...

/* This program should display the
* maximum and minimum values
*of an array, as well as display the
*array, sorted in ascending
* order.
*/
#include <stdio.h>

/* Function Prototypes */

int find_max(int[], int);
int find_min(int[], int);
int get_min_range(double list[], int first, int last);
int sel_sort(double list[], int n);

int main(void)
{
int nums[] = {5, 3, 0, 9, 8, 1, 7, 4, 10, 2};

printf("\n\nThe maximum value is %d. \n", find_max(nums, 10));
printf("\nThe minimum value is %d. \n", find_min(nums, 10));

/* The problem may be here
* or in the sel_sort function,
* as far as I can figure.
*/
printf("\nThe ascending order of the array is %d \n", (nums, 10));
return(0);

}

/* Function that finds largest number */

int find_max(int last[], int num_ele)
{
int i, max = last[0];
for(i = 1; i < num_ele; ++i)

if(max < last[i]) max = last[i];
return(max);
}

/* Function that finds smallest number */

int find_min(int first[], int num_ele)
{
int i, min = first[0];
for(i = 1; i > num_ele; ++i)

if(min > first[i]) min = first[i];
return(min);
}

/* Function that establishes minimum range */

int get_min_range( double list[], int first, int last)
{
int i, small_sub;
small_sub = first;

for(i = first + 1; i <= last; ++i)
small_sub = i;

return(small_sub);
}

/* Function that performs the selection sort */

int sel_sort(double list[], int n)
{
int ele_one, /* First element in unsorted array */
temp, /*Temporary storage for address swapping*/
min_index; /* Subscript of next smaller element */

for(ele_one = 0; ele_one < n-1; ++ele_one)
{
min_index = get_min_range(list, ele_one, n-1);

return(0);
}

/* Exchange elements */

if(ele_one != min_index)
{
temp = list[min_index];
list[min_index] = list[ele_one];
list[ele_one] = temp;
}

return(ele_one);
}
• 11-30-2001
Unregistered
Code:

```int sel_sort(double list[], int n) { int ele_one, /* First element in unsorted array */ temp, /*Temporary storage for address swapping*/ min_index; /* Subscript of next smaller element */ for(ele_one = 0; ele_one < n-1; ++ele_one) { min_index = get_min_range(list, ele_one, n-1); return(0); }```
should be
Code:

```void sel_sort(double *list, int n){         int temp;         for(int i = 0; i < n - 1; i++){                 int min = i;                 for( int j = i + 1; j < n; j++){                         if(list[j] < list[min]) { min = j; }                 }                 temp = list[min];                 list[min] = list[i];                 list[i] = temp;         } }```
-Prelude
• 11-30-2001
Unregistered
> printf("\nThe ascending order of the array is %d \n", (nums, 10));

If you're printing out the array with this, then a while loop would be more effective.
Code:

```int i = 0; while( i < SIZE ){         printf("%d", array[i]); }```
-Prelude
• 12-01-2001
Crankit211
Thanks anyway.
I tried the new code and a couple of variations but all I output is a screen full of 5's. There appears to be an endless loop occuring. Any ideas? Anybody?
• 12-01-2001
Prelude
Try this and see if it works for you
Code:

```#include <stdio.h> int find_max(int[], int); int find_min(int[], int); int get_min_range(double list[], int first, int last); void sel_sort(int list[], int n); int main(void){         int nums[] = {5, 3, 0, 9, 8, 1, 7, 4, 10, 2};         int i = 0;         printf("\n\nThe maximum value is %d. \n", find_max(nums, 10));         printf("\nThe minimum value is %d. \n", find_min(nums, 10));         sel_sort(nums, 10);         while( i < 10 ){                 printf("\nThe ascending order of the array is %d \n", nums[i]);                 i++;         }         return(0); } int find_max(int last[], int num_ele){         int i, max = last[0];         for(i = 0; i < num_ele; i++)                 if(max > last[i]) max = last[i];         return max; } int find_min(int first[], int num_ele){         int i, min = first[0];         for(i = 0; i < num_ele; i++)                 if(min < first[i]) min = first[i];         return min; } int get_min_range( double list[], int first, int last){         int i, small_sub;         small_sub = first;         for(i = first + 1; i <= last; ++i)                 small_sub = i;         return(small_sub); } void sel_sort(int *list, int n){         int temp;         for(int i = 0; i < n - 1; i++){                 int min = i;                 for( int j = i + 1; j < n; j++){                         if(list[j] < list[min]) { min = j; }                 }                 temp = list[min];                 list[min] = list[i];                 list[i] = temp;         } }```
-Prelude
• 12-01-2001
Crankit211
You were on the right track...
Thanks Prelude, your suggestions guided me to the answer. You found the sorting problem and after a little rearrangement of the printf placement, the program works without a hitch. Here's the tweak.

printf("\n\nThe maximum value is %d. \n", find_max(nums, 10));
printf("\nThe minimum value is %d. \n\n", find_min(nums, 10));
sel_sort(nums, 10);

/****Splitting the printf command into two parts(before and after the while statement) was the finishing touch. ****/

printf("The ascending order of the array is:");
while( i < 10){
printf(" %d", nums[i]);
i++;

}
printf("\n");
return(0);
}

/* Thanks again */