Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,759 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,532 people online right now. Registration is fast and FREE... Join Now!




reursion

 
Reply to this topicStart new topic

reursion, plusminusorder

danahako
5 Feb, 2007 - 12:35 AM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2007
Posts: 2


My Contributions
i have to write recursive function that get integers and int length and sort it so the integers that small then zero ate in the right side and the integers bigger then zero including zero are in the left side.
i cant use loops.

int PlusMinusOrder(int a[],int length)
User is offlineProfile CardPM
+Quote Post

horacio
RE: Reursion
5 Feb, 2007 - 03:08 AM
Post #2

D.I.C Head
**

Joined: 29 Jan, 2007
Posts: 67


My Contributions
QUOTE(danahako @ 5 Feb, 2007 - 01:35 AM) *

i have to write recursive function that get integers and int length and sort it so the integers that small then zero ate in the right side and the integers bigger then zero including zero are in the left side.
i cant use loops.

int PlusMinusOrder(int a[],int length)


hi,

the rules are you show a little effort, post anything you have so far
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Reursion
5 Feb, 2007 - 03:16 AM
Post #3

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions


You should be able to modify an existing recursive sort algorithm like quicksort for this.

See Quicksort snippet here smile.gif

User is offlineProfile CardPM
+Quote Post

danahako
RE: Reursion
5 Feb, 2007 - 05:16 AM
Post #4

New D.I.C Head
*

Joined: 5 Feb, 2007
Posts: 2


My Contributions
QUOTE(Ellie @ 5 Feb, 2007 - 04:16 AM) *

You should be able to modify an existing recursive sort algorithm like quicksort for this.

See Quicksort snippet here smile.gif


#include<stdio.h>

int plusminusorder(int a[],int len)
{
int temp;
if(len==2)
return 0;

if( a[len-1]>a[len-2])
{
temp=0;
temp=a[len-1];
a[len-1]=a[len-2];
a[len-2]=temp;
}
plusminusorder(a,len-1);
printf(" %d",a);
}

int main()
{
int a[7]={1,2,3,-8,9,0,156};
plusminusorder(a,7);
return 0;
}

User is offlineProfile CardPM
+Quote Post

Ellie
RE: Reursion
5 Feb, 2007 - 11:28 AM
Post #5

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
Here's my version as concise as I could get it. Hope it's useful. I made a recursive printing function to print the final array too.

CODE


#include<stdio.h>

plusminusorder(int a[],int low, int len)
{
if (low == len)
return 0;

int mid = (low + len)/2;
mid = partition(a, low, len, mid);
if (low<mid)
plusminusorder(a, low, mid-1);
if (mid<len)
plusminusorder(a, mid+1, len);
}

int partition (int a[], int low, int high, int mid)
{
   if (mid != low)
      swap (a, low, mid);
   mid = low;
   low++;
   while (low <= high)
    {
      if (a[low] <= a[mid])
    {
     low++;
    }
      else if (a[high] > a[mid])
       {
     high--;
       }
      else
       {
     swap(a,low,high);
       }
     }
     if (high != mid)
            swap(a, mid, high);
         return high;
  }


swap (int v[], int p1, int p2)
{
   int t;

   t = v[p1];
   v[p1] = v[p2];
   v[p2] = t;
}

printArr(int a[], int p, int aLen)
{
     if (p>aLen)
     return;
     else
     {
     printf("%d: %d\n", p, a[p]);
     printArr(a, p+1, aLen);
     }

}

int main()
{
int a[7]={1,2,3,-8,9,0,156};
int low = 0;
plusminusorder(a, low, 7);
printArr(a, 0, 6);
return 0;
}



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:11AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month