#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1

    Using recursive function to sort an array


    I am trying to use a recursive function to return the values of an array after sorting the integers stored, in ascending order. But I am not sure what to return in that function. Here is the code:
    Code:
    #include <stdio.h>
    
    int selection_sort(int a[], int n);
    
    int main(void)
    {
    	int n, i;
    	
    	printf("Enter the number of integers to enter: ");
    	scanf("%d", &n);
    	
    	int a[n];
    	
    	printf("\nEnter one integer per line: ");
    	for (i = 0; i < n; i++) {
    		scanf("%d", &a[i]);
    	}
    	
    	selection_sort(a, n);
    	
    	printf("\nIn sorted order: ");
    	
    	for (i = 0; i < n; i++) {
    		printf("%d ", a[i]);
    	}
    	
    	return 0;
    }
    
    int selection_sort(int a[], int n)
    {
    	int i, max;
    	
    	max = a[0];
    	
    	for (i = 0; i < n; i++) {
    		if (a[i] > max) 
    			max = a[i];
    		a[i] = a[n - 1];
    		a[n - 1] = max;		
    	}
    	
    	if (n > 0) {
    		selection_sort(a, n - 1);
    	}
    	/* What should I return here for the function to return the values of the array in the sorted order? */
    }
    Last edited by arman.khandaker; October 18th, 2013 at 06:53 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    7
    Originally Posted by arman.khandaker
    But I am not sure what to return in that function.
    That function doesn't require to return any value - simply change it's type to void.

    EDIT:
    Note that not all compilers allow to create variable-length arrays:
    Code:
    int a[n];
    Even if so, the value of n could be too large to allocate memory for the array on the stack causing the program to crash. Use dynamic memory allocation instead:
    Code:
    #include <stdlib.h>
    
    int *a = malloc(n * sizeof(int));
    if (a != NULL)
    {
        // ...
        free(a);
    }

    Comments on this post

    • arman.khandaker agrees
    Last edited by DRK82; October 18th, 2013 at 08:05 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by DRK82
    That function doesn't require to return any value - simply change it's type to void.

    EDIT:
    Note that not all compilers allow to create variable-length arrays:
    Code:
    int a[n];
    Even if so, the value of n could be too large to allocate memory for the array on the stack causing the program to crash. Use dynamic memory allocation instead:
    Code:
    #include <stdlib.h>
    
    int *a = malloc(n * sizeof(int));
    if (a != NULL)
    {
        // ...
        free(a);
    }
    Thanks, it worked. I had to modify the rest of my code a bit though, but it has nothing to do with return type of the function. Just to update:
    Code:
    #include <stdio.h>
    
    void selection_sort(int a[], int n);
    
    int main(void)
    {
    	int n, i, b;
    	
    	printf("Enter the number of integers to enter: ");
    	scanf("%d", &n);
    	
    	int a[n];
    	
    	printf("\nEnter one integer per line: ");
    	for (i = 0; i < n; i++) {
    		scanf("%d", &a[i]);
    	}
    	
    	selection_sort(a, n);
    	
    	printf("\nIn sorted order: ");
    	
    	if (a[0] > a[1]) {
    		b = a[0];
    		a[0] = a[1];
    		a[1] = b;
    	}
    	
    	for (i = 0; i < n; i++) {
    		printf("%d ", a[i]);
    	}
    	
    	return 0;
    }
    
    void selection_sort(int a[], int n)
    {
    	int i, max;
    	
    	max = a[0];
    	
    	for (i = 0; i < n; i++) {
    		if (a[i] > max) { 
    			max = a[i];
    			a[i] = a[n - 1];
    			a[n - 1] = max;		
    		}
    	}
    	
    	if (n > 0) {
    		selection_sort(a, n - 1);
    	}
    }

IMN logo majestic logo threadwatch logo seochat tools logo