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



LinkBack URL
About LinkBacks


