Thread: Problem with bubblesort.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    Fort Walton Beach, Florida, United States
    Posts
    6

    Problem with bubblesort.

    I am trying to get my program to sort in ascending order but it will only sort in descending order. I have read all the articles, watched all the youtube videos I can find about bubble sort. I would will really appreciate someone taking a look and tell me what I am doing wrong. I know to change from ascending to descending all you have to do is change the signs. If I change the signs from how I have them now the array won't sort for me.

    I am using Cygwin on Windows 7 64bit.

    Code:
    /* One Dimensional Array Source Code*/
    
    #include <stdio.h>
    
    main(void)
    {
    //Declare and Initialize Variables
    
    int iArray[100] = {0};
    int iX = 0;
    int iNum = 0;
    int iCount = 0;
    int iTemp = 0;
    int iJ = 0;
    
    
    //Input Data Module
    
    
                printf("\n Enter a number:");
                printf("\n Enter 0 when done:\n");
                scanf("%d", &iNum);
                        
                while(iNum != 0)
                {
                    iArray[iX] = iNum;
                    
                    printf("\n Enter a number:");
                    printf("\n Enter 0 when done:\n");
                    scanf("%d", &iNum);
                    iX++;                
                    iCount++;
                    
                                        
                } //while 
                    
    //Sorting Data Module
             
            for (iX = 0; iX < iCount; iX++)
            {    
                for (iJ= 0; iJ < (iCount - 1); iJ++)
                {
                
                    if (iArray[iX] > iArray[iJ])
                    {
                        iTemp = iArray[iX];
                        iArray[iX] = iArray[iJ];
                        iArray[iJ] = iTemp;                    
                                
                    }//end if
                    
                } //end for
                
            } //end for
        
            
                
    //Display Data Module
    
    
            printf("\n The sorted list:\n\n\t");
              
            for  (iX = 0; iX <iCount; iX++)
            {
                printf("%d\n\t", iArray[iX]);
                    
            } //end for
            
            return 0;
            
            
            
        
            
    }//end program
    Test Data

    AL@Alecia-PC ~
    $ ./a

    Enter a number:
    Enter 0 when done:
    99

    Enter a number:
    Enter 0 when done:
    34

    Enter a number:
    Enter 0 when done:
    16

    Enter a number:
    Enter 0 when done:
    1

    Enter a number:
    Enter 0 when done:
    42

    Enter a number:
    Enter 0 when done:
    0

    The sorted list:

    99
    42
    34
    16
    1

    Thanks so much.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by asallen08 View Post
    I know to change from ascending to descending all you have to do is change the signs. If I change the signs from how I have them now the array won't sort for me.
    You only have to change one sign, the one which compares the two values.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Dec 2012
    Location
    Fort Walton Beach, Florida, United States
    Posts
    6
    Quote Originally Posted by AndiPersti View Post
    You only have to change one sign, the one which compares the two values.

    Bye, Andreas
    Thank you so much. I have been trying to figure it out for a week now.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your sort is broken anyway. When you compare iArray[iX] > iArray[iJ], iX is sometimes less than iJ and sometimes greater.
    There are two ways to fix this, one of them will turn it into an actual bubble sort, one way will turn it into something else that also sorts correctly.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP]Bubblesort
    By ShiroAzure in forum C++ Programming
    Replies: 8
    Last Post: 11-07-2011, 06:34 PM
  2. Need help with Bubblesort
    By kburyanek in forum C Programming
    Replies: 9
    Last Post: 11-13-2009, 02:41 PM
  3. bubblesort problem
    By rainman39393 in forum C++ Programming
    Replies: 2
    Last Post: 02-08-2008, 08:23 PM
  4. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 09:20 PM
  5. bubblesort
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 10:45 PM

Tags for this Thread