GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Jan-2008, 20:33
SRTeasy SRTeasy is offline
New Member
 
Join Date: Dec 2007
Posts: 9
SRTeasy is on a distinguished road

Another bubble sort, can't get past the 1st pass


I am trying to complete this bubble sort in which all the elements of the array are printed after each pass..

CPP / C++ / C Code:
#include <stdio.h>                        
#include <stdlib.h>                        

#define n 8

void swap(int *, int *);

int main(void)

{
    int a[] = {7, 3, 66, 3, -5, 22, -77, 2};
    int i, j;
	
	printf("Unordered data:    ");
	
	for(i=0; i<n; ++i)
		printf("%d   ", a[i]);
		printf("\n");
		printf("  After Pass 1:  ");
		{
		for(i=0; i<n; ++i)	
			{	
			for(j=i+1; j<n; ++j)
				
				{
				if(a[i]>a[j])
				swap(&a[i],&a[j]);
				}
			for(i=0; i<n; ++i)
			printf("%d   ", a[i]);
			printf("\n");
			}
		}
}						
void swap(int *i, int *j)
{
    int tmp;

    tmp = *i;
    *i = *j;
    *j = tmp;
}   

It works fine now with no errors and here is what I get:

Unordered data: 7 3 66 3 -5 22 -77 2
After pass 1: -77 7 66 3 3 22 -5 2

Here is what I would like to see:

Unordered data: 7 3 66 3 -5 22 -77 2
After pass 1: -77 7 66 3 3 22 -5 2
After pass 2: -77 -5 66 7 3 22 3 2
After pass 3:

And so on...

So what do I need to change/impliment to get this to keep going and printing after every pass?

It seems simple, but I'm tired and need some fresh eyes to help me out.

Thanks! SRTeasy
  #2  
Old 08-Jan-2008, 21:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Another bubble sort, can't get past the 1st pass


Quote:
Originally Posted by SRTeasy
I am tryinge...

CPP / C++ / C Code:
        for (i = 0; i < n; ++i) { /* <--- outer loop variable is i */
            for (j = i + 1; j < n; ++j)
            {
                if (a[i] > a[j])
                    swap(&a[i], &a[j]);
            }
            for (i = 0; i < n; ++i)    /*<--- makes i go to n--- use some other variable */
                printf("%d   ", a[i]);
            printf("\n");
        }
Quote:
Originally Posted by SRTeasy
It works fine now with no errors
There is a difference between compiling with no errors and working with no errors.

Use some other variable for the print loop above, since it makes go to n, so the outer loop is finished after one time through.


Regards,

Dave
  #3  
Old 09-Jan-2008, 10:56
SRTeasy SRTeasy is offline
New Member
 
Join Date: Dec 2007
Posts: 9
SRTeasy is on a distinguished road

Re: Another bubble sort, can't get past the 1st pass


Use another variable besides 'i' or 'n'? and what should that variable be declared?

Thanks for the help thus far, I am working on it..
  #4  
Old 09-Jan-2008, 11:04
SRTeasy SRTeasy is offline
New Member
 
Join Date: Dec 2007
Posts: 9
SRTeasy is on a distinguished road

Re: Another bubble sort, can't get past the 1st pass


AHHHH! I got it.. thanks for your help!
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 14:43
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 07:46
Bubble Sorts with 2d Arrays y2l C Programming Language 5 14-Apr-2006 13:01
help with Bubble sort program that uses vectors instead of arrays sasuke101 C++ Forum 9 25-Oct-2005 11:26
need help with bubble sort for c++ sasuke101 C++ Forum 2 21-Sep-2005 20:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:06.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.