bubling code question

This is a discussion on bubling code question within the C Programming forums, part of the General Programming Boards category; Hello guys I m new in c me just started 1 week ago found a bubling sort code on google ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    bubling code question

    Hello guys I m new in c me just started 1 week ago found a bubling sort code on google but there is some stuff inside that i dont understand
    Code:
    
    /* bubble.c by detour@metalshell.com
     *
     * Use the very simple and slow bubble sort method to 
     * sort an array of integers.
     * 
     * http://www.metalshell.com/
     *
     */
     
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
     
    #define ARRAY_SIZE 20
     
    void print_array(int *array) {
      int x;
      for(x = 0; x < ARRAY_SIZE; x++) {
        if(x != ARRAY_SIZE-1)
          fprintf(stdout, "%d, ", array[x]);
        else
          fprintf(stdout, "%d\n", array[x]);
      }
    }
     
    int main() {
      int iarray[ARRAY_SIZE];
      int x, y, holder;
     
      // Seed rand()
      srand((unsigned int)time(NULL));
     
      for(x = 0; x < ARRAY_SIZE; x++)
        iarray[x] = (int)(rand() % 100);
     
      fprintf(stdout, "Before Sort\n---------------\n");
      print_array(iarray);  
     
     
      // Bubble sort method.
      for(x = 0; x < ARRAY_SIZE; x++)
        for(y = 0; y < ARRAY_SIZE-1; y++)
          if(iarray[y] > iarray[y+1]) {
            holder = iarray[y+1];
            iarray[y+1] = iarray[y];
            iarray[y] = holder;
          }
     
      fprintf(stdout, "\nAfter Sort\n---------------\n");
      print_array(iarray);  
     
    }
    in the buble sort that if statement I totally don't get it help me please to understand it more thanks...

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's set up for an ascending sort, so greater values found lower in the array, need to be swapped with lower values, found higher in the array.

    The normal is to print the array from low array subscripts (0,1,2 etc.) to the high subscripts.

    Inside the if statement, is the swap mechanism. Array[i] must put it's higher value into a temporary holder, then it can be assigned the value of the lower Array[i+1], then the Array[i + 1] is ready to receive the temporarily stored value, itself, completing the swap.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by Adak View Post
    It's set up for an ascending sort, so greater values found lower in the array, need to be swapped with lower values, found higher in the array.

    The normal is to print the array from low array subscripts (0,1,2 etc.) to the high subscripts.

    Inside the if statement, is the swap mechanism. Array[i] must put it's higher value into a temporary holder, then it can be assigned the value of the lower Array[i+1], then the Array[i + 1] is ready to receive the temporarily stored value, itself, completing the swap.
    thanks dude I understand it now....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi all i have a question about a code
    By iweapons in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2007, 11:05 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. My First If Code, Question? :P
    By dimirpaw in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 07:49 PM
  4. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  5. EXE to code? Decompile question.
    By RoD in forum Game Programming
    Replies: 4
    Last Post: 10-01-2002, 05:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21