Thread: Whats wrong with my sorting algorithm?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Whats wrong with my sorting algorithm?

    I'm just trying to get a simple sorting by swapping algorithm to work on an existing unsorted array..

    Code:
                                    int a, b;
                                    for (a = 0;a<count;a++)
                                    {
                                            for (b=0;b< count;b++)
                                            {
                                                    if (FREQUENCY[count] > FREQUENCY[count-1])
                                                    {
                                                            int temp;
                                                            
    
                                                           
                                                           
                                                            temp = FREQUENCY[count];
    
    
                                                            FREQUENCY[count] = FREQUENCY[count-1];
    
                                                           
                                                            FREQUENCY[count - 1] = temp;
                                                    }
                                            }
                                    }
    It's supposed to sort with the largest element being the first element and the lowest being the last. As the title says, what's wrong with it?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You should be using b to index the array, not count. count never changes in the loop.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like instead of FREQUENCY[count] you should be using FREQUENCY[b]
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You should forget using count, inside the for loops - that is just the controller so the for loops know when to stop.

    You want to do something like this:

    Code:
    int a, b, temp;
    for(a=0;a<count-1;a++) {
       for(b=a+1;b<count;b++) {
          if(frequency[a] < frequency[b]) {  //out of order?
             temp=frequency[a];
             frequency[a]=frequency[b];
             frequency[b]=temp;
          }
       }
    }
    Although similar to a bubble sort, it's actually a Substitution sort.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by Adak View Post
    You should forget using count, inside the for loops - that is just the controller so the for loops know when to stop.

    You want to do something like this:

    Code:
    int a, b, temp;
    for(a=0;a<count-1;a++) {
       for(b=a+1;b<count;b++) {
          if(frequency[a] < frequency[b]) {  //out of order?
             temp=frequency[a];
             frequency[a]=frequency[b];
             frequency[b]=temp;
          }
       }
    }
    Although similar to a bubble sort, it's actually a Substitution sort.
    Does that have a better Time case than bubble sort or is it still O(n^2)?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's an optimization of bubble sort, not a different sort. You can read about it here, as what Adak wrote is logically equivalent to the second example.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whiteflags View Post
    It's an optimization of bubble sort, not a different sort. You can read about it here, as what Adak wrote is logically equivalent to the second example.
    You are mistaken. What he wrote is no longer any form of bubble sort through any stretch of the imagination.
    Bubble sort is characterised by the swapping of only adjacent pairs of items. a and b are not adjacent most of the time in what Adak wrote.
    It's still O(n*n), but is not bubble sort at all, and it is certainly no more efficient than a plain bubble sort.

    It's one of those algorithms that people often come up with when faced with the task of sorting and having done no research on the matter. Heck it's the first sorting algorithm I ever used too. I don't think it has any particular name, people tend to refer to it by various different names.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It has the same complexity as Bubble sort. In tests on random integers, it sorts faster than a naive Bubble sort, but is slower than an optimized Bubble sorter that includes the swapped flag to tell it when it can stop sorting. Especially if the data is not random, but already partially sorted.

    Aside from being short and easy to memorize and code correctly, it has no particular practical use - a lot like Bubble sort. It is difficult to find a name for it, because it's frequently called Bubble sort, etc. (even by me). I had to go back to an old, old file, to find the name. That's the big advantage Bubble sort has always enjoyed - a catchy name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats wrong with this?
    By stevedawg85 in forum C++ Programming
    Replies: 13
    Last Post: 03-05-2006, 08:21 PM
  2. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  3. Can anyone tell me whats wrong?
    By Dragoncaster131 in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 04:36 AM
  4. Whats wrong with my file streaming/bubble sorting combo?
    By Alphacentric666 in forum C++ Programming
    Replies: 12
    Last Post: 01-04-2003, 09:02 PM
  5. whats wrong here??
    By pancho in forum C Programming
    Replies: 3
    Last Post: 03-20-2002, 10:27 PM