Results 1 to 4 of 4

Thread: sorting algorithm not working correctly

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    sorting algorithm not working correctly

    I can't seem to get this sorting alogorithm to work:
    Code:
    #include <iostream>
    
    
    void sortAssending(int nums[], int size);
    
    int main()
    {
        int nums[] = {175,167,160,164,183,187,188,179,176,175,
                      169,175,176,178,165,160,173,165,187,178};
        sortAssending(nums, 20);
        for (int i=0; i<20; i++)
        {
            std::cout << nums[i] << std::endl;
        }
        
        system("PAUSE");
    }
    
    void sortAssending(int nums[], int size)
    {
         for (int start=0; start<size; start++)
         {
             for (int end=size-1; end>=0; end--)
             {
                 if (nums[start] > nums[end])
                 {
                     int temp = nums[start];
                     nums[start] = nums[end];
                     nums[end] = temp;
                 }
             }
         }
    }


    It seems to do everything right except the very end. Can anyone spot the problem?

  2. #2
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: sorting algorithm not working correctly

    PHP Code:
    template <class T>
    void bubble(*xint n)
    {
        
    int ij;
        for (
    0n-1; ++i)
               for (
    i+10; --j)
                if (
    x[j] < x[j-1]) swap(x[j], x[j-1]);

    See if that works for you. I must warn you. Bubble sorting is so slow that it's practically useless.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: sorting algorithm not working correctly

    Minor point: it's "ascending".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Re: sorting algorithm not working correctly

    Quote Originally Posted by System_Error
    Code:
    void sortAssending(int nums[], int size){
         for (int start=0; start<size; start++){
             for (int end=size-1; end>=0; end--){
                 if (nums[start] > nums[end]){
                     int temp = nums[start];
                     nums[start] = nums[end];
                     nums[end] = temp;
                 }
             }
         }
    }
    Try changing the 'end >= 0' to 'end > start'
    Then, it becomes just a weird form of selection sort in disguise:
    For each start (0 -> size - 1), assuming the first #start objects are sorted:
    Put the next smallest into nums[start]. This sorts the first #start + 1 objects, and completes the inductive proof.
    sql_lall

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width