Thread: Sorting using Recursion function

  1. #1
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Angry Sorting using Recursion function

    Hi , i wrote this code ,which sort an array of floats using a recursion function (available in the code to sort just some members of the array), i finished it just like you can see, and its working well, but sometimes it turns the largest no in the array into very small no like (2.15154e-41) !!!!!!!!!!!!!!!! , try it out and tell me what do u think !!!! [try numbers in thousands ]

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <conio.h>
    void quicksort(int a[],int i, int j)
    {
      int temp =0;
      for (int count=i;count<j+1;count++)
      {
        for (int count2=count+1;count2<j+1;count2++)
        {
          if (a[count]>a[count2])
          {
          temp=a[count];
          a[count]=a[count2];
          a[count2]=temp;
          }
        }
      }
    
    }
    int main(int argc, char *argv[])
    {
      int marks[10];
      int no;
    
      cout<<"How many marks you have ? (MAX 10)  ";
      cin>>no;
      for (int g=0;g<no;g++)
      {
        cout<<endl<<"enter mark no "<< (g+1)<<"  " ;
        cin>>marks[g];
      }
      int s,f;
      cout<<"\nEnter the initial index you want to begin the sort from ";
      cin>>s;
      cout<<"\nEnter the finish index you want to stop the sort at ";
      cin>>f;
      int *p=&marks[0];
      quicksort(p,s-1,f-1);
      for (int l=0;l<no;l++)
      {
        cout<<"   "<<marks[l]<<"   ";
      }
      getche();
    
    
    
    
      return 0;
    }
    I hope someone has the answer thanks
    Programming is a high logical enjoyable art for both programer and user !!

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I'm confused, how is this recursion? I don't see anywhere where the sort function calls itself again.

    Also, what values do you enter that mess up exactly?

  3. #3
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    im so sorry , i posted the another code i wrote for the same thing
    im a dummy

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <iostream>
    
    void quicksort(float a[],int i,int j)
    {
      bool ok=true;
      int temp=0;
      for (int count1=i;count1<j+1;count1++)
      {
        if (a[count1]>a[count1+1])
        {
          ok=false;
        }
      }
      if (ok) {return ;};
      for (int count2=i;count2<j+1;count2++)
      {
        if (a[count2]>a[count2+1])
        {
          temp=a[count2];
          a[count2]=a[count2+1];
          a[count2+1]=temp;
        }
      }
      float *pp=&a[0];
      quicksort(pp,i,j);
    }
    
    
    int main(int argc, char *argv[])
    {
      float marks[10];
      int no;
    
      cout<<"How many marks you have ? (MAX 10)  ";
      cin>>no;
      for (int g=0;g<no;g++)
      {
        cout<<endl<<"enter mark no "<< (g+1)<<"  " ;
        cin>>marks[g];
      }
      int s,f;
      cout<<"\nEnter the initial index you want to begin the sort from ";
      cin>>s;
      cout<<"\nEnter the finish index you want to stop the sort at ";
      cin>>f;
      float *p=&marks[0];
      quicksort(p,s-1,f-1);
      for (int l=0;l<no;l++)
      {
        cout<<"   "<<marks[l]<<"   ";
      }
      getche();
    
    
    
    
      return 0;
    }
    thats my code
    try to enter 5 no for examples (2,300,123,2000,4100)


    sorry for this mistake
    and thanks PJYelton for the reply
    Programming is a high logical enjoyable art for both programer and user !!

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I don't really see how it turns numbers into small numbers. What numbers are you plugging in that it does this?

    edit: I haven't tried numbers in the millions or higher, just too slow due to the fact that it is recursion.

  5. #5
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    these no causes me the problem10 no)
    65 32 125 1 266 484444 656 2 154 1

    after all, thanks for caring and for replying all
    Programming is a high logical enjoyable art for both programer and user !!

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I don't really see any probs here, tested with 5 numbers, 10 numbers, and 3. Index from 0 to (n - 1). Sorry.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    I dont really see the problem either.. sorry for not been any help
    Snoop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Recursion function won't exit
    By cheesehead in forum C++ Programming
    Replies: 20
    Last Post: 10-30-2001, 03:58 PM