Whats wrong with my file streaming/bubble sorting combo?

This is a discussion on Whats wrong with my file streaming/bubble sorting combo? within the C++ Programming forums, part of the General Programming Boards category; The code is self-explanotory. I'm trying to make a program that creates a .txt which contains 10 random integers. Then ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Whats wrong with my file streaming/bubble sorting combo?

    The code is self-explanotory. I'm trying to make a program that creates a .txt which contains 10 random integers. Then read the integers and sort it from least to greatest and then print them out in another .txt file.

    I used:
    File Streaming
    Singleton
    Bubble Sorting
    Class



    For this program.
    Attached Files Attached Files

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    In your second for loop, you typed "i < 10" instead of "j < 10".
    Also, you used Array[i] instead of Temp in the loop's assignment.

    Anyway, I thought you were supposed to bubblesort from opposite ends?


    Code:
    for(int i= 0; i< 10; i++)
     {
      for(int j = 10-1; j >= 0; j--)
      {
       if(Array[j] > Array[i])
       {
        int Temp= Array[i];
        Array[i]= Array[j];
        Array[j]= Temp;
       }
      }

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I think there are like 4 or 5 different ways to bubble sort. The easiest one that I've found is:
    Code:
    // sorts from least to greatest
    for(int i=0; i<10; i++)
     {
      for(int j=i+1; j<10; j++)
      {
       if(Array[j] < Array[i])
       {
        int Temp= Array[i];
        Array[i]= Array[j];
        Array[j]= Temp;
       }
      }

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by PJYelton
    I think there are like 4 or 5 different ways to bubble sort. The easiest one that I've found is:
    Code:
    // sorts from least to greatest
    for(int i=0; i<10; i++)
     {
      for(int j=i+1; j<10; j++)
      {
       if(Array[j] < Array[i])
       {
        int Temp= Array[i];
        Array[i]= Array[j];
        Array[j]= Temp;
       }
      }
    What are the other ways?!
    none...

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Do you have to use a Bubble Sort? I realize you are only sorting 10 integers which is no big deal. However, you should note that Bubble Sort is just about the slowest sorting algorithm imaginable. You could look up Radix Sort or the ever popular Quick Sort if you are interested in others.
    Last edited by MrWizard; 01-02-2003 at 04:52 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by MrWizard
    Do you have to use a Bubble Sort? I realize you are only sorting 10 integers which is no big deal. However, you should note that Bubble Sort is just about the slowest sorting algorithm imaginable. You could look up Radix Sort or the ever popular Quick Sort if you are interested in others.
    I don't know what does this has to do with my post that you've quoted?!

    >>you are only sorting 10 integers which is no big deal.
    It's not me who is sorting it's Alphacentric666.

    My post was because PJYelton said that there are about 4 or 5 other ways of bouble sort, and I think there is only one way that is called bubble sort, and any other way of sorting is a different algorithm...

    So I think you didn't read the posts well...
    none...

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Other ways to do bubble sort that I can think of off the top of my head - although they are all pretty much the same thing:
    Code:
    for (int i=0; i<= n; i++)
      for(int j=0; j<= n-i-1; j++)
        if ( arr[j] > arr[j+1] )
       {	
          int tmp = arr[j+1];
          arr[j+1] = arr[j];
          arr[j] = tmp;
       }
    Code:
    bool noswap = false;
    
    int i = 0;
    while(!noswap) 
    {
       noswap = true;
       for (int j = i + 1; j < n; ++j) 
      {
           if (arr[i] > arr[j]) 
          {
    	int tmp = arr[i];
    	arr[i] = arr[j];
    	arr[j] = tmp;
    	noswap = false;
          }
      } 
       i++;
    }
    I know there are a couple of others. But like MrWizard said, bubble sort while easy to write is very slow and inefficient. Another favorite of mine beyond the ones MrWizard mentioned is merge sort. Even if it isn't technically the best, I love the idea of an array breaking itself apart and putting itself back together again!

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    [QUOTEI know there are a couple of others. But like MrWizard said, bubble sort while easy to write is very slow and inefficient. Another favorite of mine beyond the ones MrWizard mentioned is merge sort. Even if it isn't technically the best, I love the idea of an array breaking itself apart and putting itself back together again! [/B][/QUOTE]

    I see...
    Now I understood what you meant by, others ways...

    I personally like the QuickSort, it's the best sorting algorithm I've seen.
    none...

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yep, quicksort rules too! But avoid shell sort...

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by ammar
    I don't know what does this has to do with my post that you've quoted?!

    >>you are only sorting 10 integers which is no big deal.
    It's not me who is sorting it's Alphacentric666.

    My post was because PJYelton said that there are about 4 or 5 other ways of bouble sort, and I think there is only one way that is called bubble sort, and any other way of sorting is a different algorithm...

    So I think you didn't read the posts well...
    Oops, I think I hit quote out of habit. As for the other ways of doing a bubble sort they are all the same except for very MINOR changes. These changes might help a little bit but on the whole they don't help that much.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Here's a good Quicksort template I wrote:


    Code:
    template <class number>
    bool greater(number a, number b){
     return (a > b);
    }
    template <class number>
    bool less(number a, number b){
     return (a < b);
    }
    //...these are the "internal" templates used by the algorithm...
    template <class number>
    int partition(number array[], int start, int finish, bool (*compare)(number, number) = greater) {
     int pivot = start;
     number value = array[pivot];
       for( int index = start + 1; index < finish ; ++index )
         if( !compare( array[index], value ) )
           swap(array[index], array[++pivot]);
      swap(array[start], array[pivot]);
     return pivot;
    }
    template <class number>
    void quicksort(number array[], int start, int finish, bool (*compare)(number, number) = greater) {
      if(start < finish) {
      int pivot = partition(array, start, finish, compare);
      quicksort(array, start, pivot, compare);
      quicksort(array, pivot + 1, finish, compare);
      }
     return;
    }
    //...and here is the user template...
    template <class number>
    void quicksort(number array[], int length, bool (*compare)(number, number) = greater) {
     quicksort(array, 0, length, compare);
    }
    Last edited by Sebastiani; 01-04-2003 at 05:25 PM.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    Heres an update.
    Attached Files Attached Files

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Alphacentric666
    Heres an update.
    Are you still having problems? If so, post questions and only the relevant part of the source within the text of your post. Some people don't read attachments as they usually contain a complete program, which no-one can be bothered to look through.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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