Thread: Selection Sort confusion

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    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);
    }
    Last edited by Crankit211; 12-01-2001 at 07:45 AM.

  2. #2
    Unregistered
    Guest
    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

  3. #3
    Unregistered
    Guest
    > 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

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    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?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    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 */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion and selection sort
    By Dashing Boy in forum C Programming
    Replies: 4
    Last Post: 08-29-2006, 04:42 PM
  2. Selection Sort problem #2
    By Twigstar in forum C++ Programming
    Replies: 7
    Last Post: 07-11-2005, 07:27 PM
  3. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 08:39 PM
  4. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 08:33 PM
  5. selection sort records of chars
    By hew in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 03:49 PM