Thread: bubble sort not sorting numbers in order.

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    hmm...
    Code:
    After sorting...
       1   5  13  24  35  37  44  72  96  98
    I mean 35 before sorting, not after sorting.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    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

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    What is your current code?
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    typedef int intArray[];
    void generate(intArray nums, int size, int low, int high);
    void bubSort(intArray nums, int size);
    void displayNums(intArray nums, int size);
    void display(int n);
    bool notInOrder(int a, int b);
    void swap(int& a, int& b);
    
    int main()
    {
      const int MAX_SIZE = 5000; // arbitrary
      int numbers[MAX_SIZE];
      int howMany;
    
      cout << "How many numbers to sort? ";
      cin >> howMany;
    
      if(howMany > MAX_SIZE)
        {
          howMany = MAX_SIZE;
        }
    
      generate(numbers, howMany, 0, 999);
      cout << "Numbers generated..." << endl;
      displayNums(numbers, howMany);
      cout << endl;
      bubSort(numbers, howMany);
      cout << "After sorting..." << endl;
      displayNums(numbers, howMany);
      cout << endl;
    }
    
    void generate(intArray nums, int size, int low, int high)
    {
      int n, i = 0;
      do
        {
          n = (rand() + time(0)) % 100;
          if(n >= low && n <= high)
    	{
    	  nums[i] = n;
    	  i++;
    	}
        }while (i <= size);
    }
    
    void bubSort(intArray nums, int size)
    {
      for(int k = size-1; k >= 0; k--)
        {
          for(int i = 0; i <= k; i++)
    	{
    	  if(notInOrder(nums[i],nums[i+1]))
    	    {
    	      swap(nums[i],nums[i+1]);
    	    }
    	}
        }
    }
    
    void displayNums(intArray nums, int size)
    {
      for(int s = 0; s < size; s++)
        {
          cout.width(4);
          cout << nums[s];
          if(s != 0 && s%15 == 0)
    	{
    	  cout << endl;
    	}
        }
    }
    
    bool notInOrder(int a, int b)
    {
      return (a > b);
    }
    
    void swap(int& a, int& b)
    {
      int temp;
      temp = a;
      a = b;
      b = temp;
    }

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have an array out of bounds access in bubSort(). On the first iteration of the outer loop, k = size-1. However, the inner loop loops until and including the iteration where i = k. In that iteration, nums[i+1] is nums[k+1]. Since k = size - 1, that means that nums[k+1] is nums[size-1+1] which is nums[size], hence the array is accessed out of bounds.
    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

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    You have an array out of bounds access in bubSort(). On the first iteration of the outer loop, k = size-1. However, the inner loop loops until and including the iteration where i = k. In that iteration, nums[i+1] is nums[k+1]. Since k = size - 1, that means that nums[k+1] is nums[size-1+1] which is nums[size], hence the array is accessed out of bounds.
    So how would I go about correcting it? Sorry to be of bother but it has been a long day trying to get this working, and now more problems seem to appear as I go on seems like I will never make the program work fully! So I hope you can help.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rushhour
    So how would I go about correcting it?
    Instead of having i <= k as the condition for the inner loop, use i < k.
    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

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    Instead of having i <= k as the condition for the inner loop, use i < k.
    Thank you very much for helping me. Seemed simple now that I have thought about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. testing a bubble sort algorithm
    By rushhour in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 01:00 AM
  3. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 PM
  4. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  5. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM