Bubble sorting an array using NO loops.

This is a discussion on Bubble sorting an array using NO loops. within the C++ Programming forums, part of the General Programming Boards category; Well does it work? Compile? Sort?...

  1. #16
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Well does it work? Compile? Sort?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    No, it does not.

  3. #18
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by Kristyy_mariee View Post
    No, it does not.
    Well, don't keep us in suspence, what's the problem?!
    Devoted my life to programming...

  4. #19
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    Something is seriously wrong with the loops. And its confusing me.. plus I need to know how to print the array once its sorted. right now im getting the unsorted numbers from the file and just bogus numbers from a cout i have after i call the function.
    Code:
     #include <iostream>
     #include <fstream>
     using namespace::std;
     
     
     int bubblesort( int arrayf[], int currentitem, int arraysize, bool isswapped);
     
     int bubblesort( int arrayf[], int currentitem, int arraysize, bool isswapped)
      {
      int temp;
      
      
    if (currentitem ==arraysize-1){ //checking for end of array
     if (isswapped==true){
                        
                        isswapped=false;
                        bubblesort(arrayf,0,arraysize,isswapped);
                        }
                        
                        
                           if (isswapped==false){
                           bubblesort(arrayf,currentitem,arraysize,isswapped);//sorted
                             }
                             
                             
                        
                        
                        
       if (arrayf[currentitem]>arrayf[currentitem+1])//swapping if smaller
       {
                                    
       temp = arrayf[currentitem];
       arrayf[currentitem] = arrayf[currentitem+1];
       arrayf[currentitem+1] = temp;
       isswapped = true;
       bubblesort(arrayf,currentitem+1,arraysize,isswapped);
    }
                 
                 
                 
                 else {
       if(arrayf[currentitem]<arrayf[currentitem+1])
       
           //isswapped=false;
       bubblesort(arrayf,currentitem+1,arraysize,isswapped);
       
                                                              }
                                                   
                                                  }
    }
    int main()
    {
    
         int j = 15; 
        int arr[j];
        const int numValues = sizeof(arr) / sizeof(arr[0]);
    //opens file
      ifstream myfile ("integers.txt");
      
      if (myfile.is_open())
      {
        while ( myfile.good() )
        {cout<<"unsorted";
              for(j=0;j<=14;j++){
            myfile>>arr[j];                     
          
          cout << arr[j] << endl;
         
          
    } 
     
          bool bSwapped = false;
        bubblesort(arr, 0, numValues, bSwapped);  // start from the first item
    
        
        cout<<arr[j];
         myfile.close();
         getchar();
         return 0;
         
         }
    }}

  5. #20
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    and also where the return statements would be , i was wondering if a return statement should be in the if where isswapped==false... but im not sure.

  6. #21
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    I am not going to do any more work on the sort for you, but Are you saying you are not allowed to use loops for the output either? If that is the case why are you allowed one to populate your array? Anyway, I you are not allowed to use loop to output result it is a simple matter of using my countdown example but count up instead and use the value as your index...starting from 0 instead of 1 as i mentioned earlier. So this would be a seperate call to 'RecursiveOutput()' with its own recall once you are done.

    I have to add you have some real fundamental errors in there, you need to practice some basic stuff and gain understanding in general, quite apart from the task in hand
    Last edited by rogster001; 03-28-2012 at 01:40 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble Sorting Problem
    By jappy512 in forum C Programming
    Replies: 3
    Last Post: 12-02-2009, 05:54 PM
  2. Bubble Sorting
    By yukapuka in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2008, 09:44 PM
  3. bubble sorting a 2-d array
    By stodd04 in forum C Programming
    Replies: 5
    Last Post: 03-17-2005, 12:40 PM
  4. Bubble Sorting HELP WANTED !
    By Shahram_z in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2003, 07:12 PM
  5. help with my bubble sorting of arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 03:43 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21