• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
PaulCaswellCommented:
Hi khawaib,

Hint:

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

sort(m, &a[n]);

Paul
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;
      
}
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
PaulCaswellCommented:
Hi khawaib,

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

Paul
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>);
   ......
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.