Printing after every pass through a bubble sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mosullivan
    New Member
    • Nov 2007
    • 12

    Printing after every pass through a bubble sort

    I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far.

    [CODE=c]
    #include <stdio.h>
    #define MAXWORD 101
    void swap(int *i, int *j);

    int main(void)
    {
    int sort[MAXWORD];
    int size;
    int i = 1;
    int j;
    int pass = 0;

    printf("Enter how many numbers you want sorted. (Max lentgth is 100)\n\n");

    scanf("%d", &size);

    printf("\nEnter the numbers.\n\n");

    for (i = 0; i < size; ++i) /*Fill the array with the numbers input by user*/
    scanf("%d", &sort[i]); /*Accept the numbers entered by user*/

    printf("\nUnord ered data:");

    for (i = 0; i < size; ++i) /*Print the array of numbers that were entered by user*/
    printf("%5d", sort[i]);
    printf("\n");

    for (i = 0; i < size; ++i)
    {

    for (j = i + 1; j < size; ++j)
    {
    if (sort[i] > sort[j])

    swap(&sort[i], &sort[j]);

    ++pass;

    printf(" After pass %d:", pass);
    for (i = 0; i < size; ++i)
    printf("%5d", sort[i]);
    printf("\n");
    }

    }
    return 0;
    }
    [/CODE]

    Please me know if anyone has any ideas on how to print after each pass through the loop.
    Last edited by Ganon11; Nov 16 '07, 02:16 PM. Reason: Fixing [CODE] tags.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    If you can write a function, say, printArray(), then you can call the function at the end (or beginning, but the end is probably closer to what you want) and have it print the array. If you'd rather not write a function, you can put in the code directly at the end of the loop. By the end, I mean the last line before the closing } in a loop.

    Comment

    • mosullivan
      New Member
      • Nov 2007
      • 12

      #3
      I changed a few things and am now able to count each time through the loop and print that back out the appropriate number of times but the array is the same every time. How do I display the contents of the arrary after each pass?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Maybe you should move this piece of code from the innermost for... loop to the outer for...loop?

        [CODE=c]++pass;

        printf(" After pass %d:", pass);
        for (i = 0; i < size; ++i)
        printf("%5d", sort[i]);
        printf("\n");[/CODE]

        Comment

        • mosullivan
          New Member
          • Nov 2007
          • 12

          #5
          Originally posted by Ganon11
          Maybe you should move this piece of code from the innermost for... loop to the outer for...loop?

          [CODE=c]++pass;

          printf(" After pass %d:", pass);
          for (i = 0; i < size; ++i)
          printf("%5d", sort[i]);
          printf("\n");[/CODE]

          Here is what I have now. It will print the "After pass %d:" line the correct number of times but everytime the array is the same.

          [CODE=c]
          #include <stdio.h>
          #define MAXWORD 101
          int swap(int *i, int *j);

          int main(void)
          {
          int sort[MAXWORD];

          int size;
          int i;
          int j;
          int pass = 0;


          printf("Enter how many numbers you want sorted. (Max lentgth is 100)\n\n");

          scanf("%d", &size);

          printf("\nEnter the numbers.\n\n");

          for (i = 0; i < size; ++i) /*Fill the array with the numbers input by user*/
          scanf("%d", &sort[i]); /*Accept the numbers entered by user*/

          printf("\nUnord ered data:");

          for (i = 0; i < size; ++i) /*Print the array of numbers that were entered by user*/
          printf("%5d", sort[i]);
          printf("\n");



          for (i = 0; i < size; ++i)

          {
          for (j = i + 1; j < size; ++j)
          {
          if (sort[i] > sort[j])
          swap(&sort[i], &sort[j]);


          ++pass;

          }
          for (pass = 1; pass < size; ++pass)
          {
          printf(" After pass %d:", pass);
          for (i = 0; i < size; ++i)
          printf("%5d", sort[i]);
          printf("\n");

          }

          }

          }
          [/CODE]
          Last edited by Ganon11; Nov 16 '07, 03:43 PM. Reason: Fixing [CODE] tags.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Have you tried printing the array just before the return statement in main()? You may not even be sorting it correctly, which would explain why the array never changes. I suspect it's something to do with the swap function, which you have not shown us yet.

            Comment

            • mosullivan
              New Member
              • Nov 2007
              • 12

              #7
              I've moved my printf statements down to between the end brackets (lines 53 - 55) and it prints the final sort. Below is my swap function. Sorry I forgot it - I'm getting pretty stressed over this.


              Code:
               (C) #include <stdio.h>
              
              int swap(int *i, int *j)
              {
              
              
              int tmp;
              
              				tmp = *i;
              				*i = *j;
              				*j = tmp;
              			
              return 0;		
              
              }

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                So, just to clear up the situation:

                1) Your bubble sort is actually sorting correctly, as indicated by the final printf statement.

                2) Your printf statements in the middle of the sort are not printing the array's progress correctly - they merely print the original array.

                I'm not sure what's going on. The only thing I can suggest is making a separate print_array function that accepts an int* to the array and an int with its size, and that prints the first size elements of the array, then a newline. This will at least make your code a little easier to read. I'll take a look back over your code and see what I can do.

                EDIT: Why is the printing section inside a for...loop? You want it to execute once per execution of the first for...loop (thus printing after every time your inner loop has 'bubbled' a number to the right/left), so you should just have the statements to print the array. Additionally, this for...loop is resetting the value of pass, so that variable is now useless.

                Comment

                • mosullivan
                  New Member
                  • Nov 2007
                  • 12

                  #9
                  Thanks so much for your help. I started completely over and used a print array function and it worked. (Big sigh!)

                  Comment

                  • royal52
                    New Member
                    • Aug 2014
                    • 1

                    #10
                    nicely written implementation of bubble sort algorithm but it is missing explanation, if anyone wants explanations then they could find it there:

                    Theory of bubble sort in C++. How to perform bubble sort on the Array, definition and Explanations of bubble sort with complete working and proper examples.

                    Comment

                    Working...