• C

Insertion Sort by Top-down recursive function

I am tryin to write a program where a recursive function (which calls itself each time) would sort the values contained in an array.

I had a go at it, but widout success. The code (not workin) is as follows


#include <stdio.h>
void sort(int m, int a[]);
void main (void)
{
   int m;
   int a[] = {3, 1, 9, 7, 8, 11, 7};
      printf("Type the number of values requied to be sorted ");
      scanf("%i", &m);

//      sort(int m, int a[]);
}

void sort(int m, int a[])

 {
//int answer=0;
//       if(m>0){
      //       answer = sort(m, a[answer]);
             
//       {
      
printf("%i",  a[m]);

       }

Thanks
khawaibAsked:
Who is Participating?
 
PaulCaswellCommented:
Hi khawaib,

To sort, say, the last three elements of the array, you need to point to the start of that section of the array and pass the correct length of the remaining items:

sort(&a[m-3],3);

Sorry I have to be sort of cryptic. We help with homework questions gladly but we have to be careful about doing it for you.

Paul
0
 
PaulCaswellCommented:
Hi khawaib,

What are you actually trying to do?

One method is to use the fact that a one-entry array is by definition sorted and a two-entry array can be sorted by comparing the two entries and swapping them. Any other array can be sorted as two arrays of smaller size.

Try implementing that! Sorry but your code isnt sufficient for us to suggest changes as there isnt really anything there.

Paul
0
 
PaulCaswellCommented:
Hi khawaib,

Hint:

to sort a part of an array starting at n of length m, use

sort(m, &a[n]);

Paul
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

 
khawaibAuthor Commented:
The recursive function should sort the numbers in array "a". The function would take each integer and will see whether its smaller than the previous and place it in front of an integer which is bigger than itself i.e. 3 before 1 (1, 3) etc.

However I,m having difficulty to know how the function would call itself, so it repeates the process with all the integers in the array till it reaches m value (number of integers which need to be sorted).  Any thoughts?


#include <stdio.h>
void sort(int a[], int m);
void main (void)
{
   int m;
   int a[] = {3, 1, 9, 7, 8, 11, 7, 5};
      printf("Type the number of values requied to be sorted ");
      scanf("%i", &m);

      printf("The sorted numbers are ");
   sort(a[m], m);

//      sort(int m, int a[]);
}

void sort(int a[], int m)

{
  int i,j,tmp;
  for ( i=m-2; i>=0; i-- )
  {
    tmp = a[i];
    for ( j=i+1; j<m && tmp>a[j]; j++ )
      a[j-1] = a[j];
    a[j-1] = tmp;
   
      sort(&a[m],m);
  }
 return;
      
}
0
 
khawaibAuthor Commented:
Hi Paul,

Thanks for your help

Now It doesn't like 2nd parameter because its int and function void i guess

error C2664: 'printf' : cannot convert parameter 2 from 'void' to '...'


void sort(int a[], int m);

printf("The sorted numbers are ");
      printf("%i", sort(&a[m],m));
 sort(&a[m], m);

Thanks
0
 
PaulCaswellCommented:
Hi khawaib,

You will have to print out the array in a loop after you sort it.

Paul
0
 
Razor2k5Commented:
You can use QuickSort, it's faster.

.....

void QuickSort(int l,int r)
{
   int i,j,x,w;
   i=l;
   j=r;
   x=a[round((l+r)/2)];
   do
   {      while(a[i]<x)i++;
      while(a[j]>x)j--;
      if(i<=j)
      {
         w=a[i];
         a[i]=a[j];
         a[j]=w;
         i++;
         j--;
      }
   }
   while(i<=j)
   if(l<j)QuickSort(l,j);
   if(r>i)QuickSort(i,r);
}

......

void main(){
   int a[];
   ......
   //a will be filled with values
   ......
   QuickSort(0,<length of a>);
   ......
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.