Thread: quick bubble sort question

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    quick bubble sort question

    hi all

    can someone please explain whey this buble sort doesnt work. Many thanks

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
        int a,b;
        int array[4];
        int i;
        int swapped;
        printf("Enter 5 integers\n");
        for (i=0;i<4; i++){
            scanf("%d", &a);
            array[i]=a;
            printf("%d", array[i]);
        }
        swapped=1;
        while (swapped){
            swapped=0;
            for (i=0;(i<4)&&(swapped=0); i++){
            if (array[i]>array[i+1]){
            b=array[i];
            array[i]=array[i+1];
            array[i+1]=b;
            swapped=1;
            }
    
            printf("%d\n", array[i]);
        }
    
    
        return 0;
    }}

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    change (swapped=0) to (swapped==0)

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Probably
    Code:
     while (swapped){
            swapped=0;
    //        for (i=0;(i<4)&&(swapped=0); i++){ wrong
            for (i=0;(i<4)&&(swapped==0); i++){
            if (array[i]>array[i+1]){
            b=array[i];
            array[i]=array[i+1];
            array[i+1]=b;
            swapped=1;
            }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    hi all thx for the quick replies

    i changed the code but it does not work
    i also want to know how t fix the same number issue if let say u put 15 12 15 12 15
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
        int a,b;
        int array[5];
        int i;
        int swapped;
        printf("Enter 5 integers\n");
        for (i=0;i<5; i++){
            scanf("%d", &a);
            array[i]=a;
          //  printf("%d", array[i]);
        }
    
    
        swapped=1;
        while (swapped){
            swapped=0;
            for (i=0;(i<4)&&(swapped==0); i++){
            if (array[i]>array[i+1]){
            b=array[i];
            array[i]=array[i+1];
            array[i+1]=b;
            swapped=1;
            }
    
    
            printf("%d\n", array[i]);
        }
    
    
    
    
    
        return 0;
    
    }}

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    This must work, check your test-output AFTER the while-loop like:
    Code:
        for (i=0; i<5; i++)
            printf("\n%d", array[i]);

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    can someone please explain whey this buble sort doesnt work. Many thanks
    Because there's no such thing as a buble sort.

    Hey Geoff, Welcome to the forum!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java bubble sort
    By rodrigorules in forum Tech Board
    Replies: 2
    Last Post: 01-15-2010, 11:40 AM
  2. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  3. Bubble Sort Query
    By coolboarderguy in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 12:50 AM
  4. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  5. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM