472,805 Members | 809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define MAXWORD 101
  3. void swap(int *i, int *j);
  4.  
  5. int main(void)
  6.     int sort[MAXWORD];
  7.     int size;
  8.     int i = 1;
  9.     int j; 
  10.     int pass = 0;
  11.  
  12.     printf("Enter how many numbers you want sorted.  (Max lentgth is 100)\n\n");
  13.  
  14.     scanf("%d", &size);
  15.  
  16.     printf("\nEnter the numbers.\n\n");
  17.  
  18.     for (i = 0; i < size; ++i)                    /*Fill the array with the numbers input by user*/
  19.         scanf("%d", &sort[i]);                    /*Accept the numbers entered by user*/
  20.  
  21.     printf("\nUnordered data:");
  22.  
  23.     for (i = 0; i < size; ++i)                    /*Print the array of numbers that were entered by user*/
  24.         printf("%5d", sort[i]);
  25.         printf("\n");
  26.  
  27.     for (i = 0; i < size; ++i) 
  28.     {
  29.  
  30.         for (j = i + 1; j < size; ++j)
  31.         {
  32.             if (sort[i] > sort[j]) 
  33.  
  34.                  swap(&sort[i], &sort[j]);
  35.  
  36.                  ++pass;
  37.  
  38.                  printf("  After pass %d:", pass);
  39.                  for (i = 0; i < size; ++i)
  40.                  printf("%5d", sort[i]);
  41.                  printf("\n");
  42.         }
  43.  
  44.     }
  45.                 return 0;
  46. }
  47.  
Please me know if anyone has any ideas on how to print after each pass through the loop.
Nov 15 '07 #1
9 19280
Laharl
849 Expert 512MB
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.
Nov 15 '07 #2
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?
Nov 16 '07 #3
Ganon11
3,652 Expert 2GB
Maybe you should move this piece of code from the innermost for... loop to the outer for...loop?

Expand|Select|Wrap|Line Numbers
  1. ++pass;
  2.  
  3. printf("  After pass %d:", pass);
  4. for (i = 0; i < size; ++i)
  5.    printf("%5d", sort[i]);
  6. printf("\n");
Nov 16 '07 #4
Maybe you should move this piece of code from the innermost for... loop to the outer for...loop?

Expand|Select|Wrap|Line Numbers
  1. ++pass;
  2.  
  3. printf("  After pass %d:", pass);
  4. for (i = 0; i < size; ++i)
  5.    printf("%5d", sort[i]);
  6. printf("\n");

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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define MAXWORD 101
  3. int swap(int *i, int *j);
  4.  
  5. int main(void)
  6.     int sort[MAXWORD];
  7.  
  8.     int size;
  9.     int i;
  10.     int j;
  11.     int pass = 0;
  12.  
  13.  
  14.     printf("Enter how many numbers you want sorted.  (Max lentgth is 100)\n\n");
  15.  
  16.     scanf("%d", &size);
  17.  
  18.     printf("\nEnter the numbers.\n\n");
  19.  
  20.     for (i = 0; i < size; ++i)                    /*Fill the array with the numbers input by user*/
  21.         scanf("%d", &sort[i]);                    /*Accept the numbers entered by user*/
  22.  
  23.     printf("\nUnordered data:");
  24.  
  25.     for (i = 0; i < size; ++i)                    /*Print the array of numbers that were entered by user*/
  26.         printf("%5d", sort[i]);
  27.         printf("\n");
  28.  
  29.  
  30.  
  31.     for (i = 0; i < size; ++i) 
  32.  
  33.     {
  34.         for (j = i + 1; j < size; ++j)
  35.         {
  36.             if (sort[i] > sort[j]) 
  37.             swap(&sort[i], &sort[j]);
  38.  
  39.  
  40.         ++pass;
  41.  
  42.         }
  43.             for (pass = 1; pass < size; ++pass)
  44.             {
  45.                 printf("  After pass %d:", pass);
  46.                     for (i = 0; i < size; ++i)
  47.                         printf("%5d", sort[i]);
  48.                         printf("\n");
  49.  
  50.             }
  51.  
  52.     }
  53.  
  54. }
  55.  
Nov 16 '07 #5
Ganon11
3,652 Expert 2GB
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.
Nov 16 '07 #6
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.


Expand|Select|Wrap|Line Numbers
  1.  (C) #include <stdio.h>
  2.  
  3. int swap(int *i, int *j)
  4. {
  5.  
  6.  
  7. int tmp;
  8.  
  9.                 tmp = *i;
  10.                 *i = *j;
  11.                 *j = tmp;
  12.  
  13. return 0;        
  14.  
  15. }
Nov 16 '07 #7
Ganon11
3,652 Expert 2GB
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.
Nov 16 '07 #8
Thanks so much for your help. I started completely over and used a print array function and it worked. (Big sigh!)
Nov 17 '07 #9
nicely written implementation of bubble sort algorithm but it is missing explanation, if anyone wants explanations then they could find it there:

http://www.hellgeeks.com/bubble-sort/
Aug 28 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.