Array sort

This is a discussion on Array sort within the C++ Programming forums, part of the General Programming Boards category; It sorts an array, max size of array is 1024 - the max size is given by our teacher. Code: ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    Array sort

    It sorts an array, max size of array is 1024 - the max size is given by our teacher.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a[1024], n, x, i, j;
    
    	cin >> n;
    
    	for ( i = 0; i < n; i++ )
    	{
    		cin >> x;
    		a[i] = x;
    	}
    	for ( i = 0; i < n; i++ )
    	{
    		for ( j = 0; i+j+1 != n; j++ )
    		{
    			if ( a[i] > a[i+j+1] )
    			{
    				x = a[i];
    				a[i] = a[i+j+1];
    				a[i+j+1] = x;
    			}
    		}
    	}
    	for ( i = 0; i < n; i++ )
    		cout << a[i] <<" ";
    }
    I tried it and I haven't got any problem yet. Is/Will there be a problem with my code.

    It's a bubble sort right?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There will be a problem if anyone chooses to input more than 1024 numbers. You might need a sanity check.

    I'm not sure if it is exactly bubble-sort (which as far as I know should always compare two consecutive items: e.g j and j - 1.

    But it looks like the index calculation in the inner loop could be simplified:
    Code:
    for ( j = i + 1; j != n; j++ )
    and now just use j instead of i + j + 1 (since i + 1 is a constant in the inner loop).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    I think its okay the max is given by our teacher, that's what hes asking for.

    I changed my code to this
    for ( j = i + 1; j != n; j++ )
    So I guess there's nothing wrong anymore, right?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, you might want to use better variable names. Even something like "temp", for example, would be better than "x".

    I see you're re-using the variable x. I would try to avoid doing that if I were you.

    Hopefully you also changed the code inside the inner for loop as well . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    As hinted, it's not Bubble Sort. Bubble Sort is stable because it compares adjacent items only. This doesn't do that.
    It will certainly sort though and it's what I call Simple Sort, but if you need to have written Bubble Sort, then you have not done so.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion Sort on Array of Structs
    By n0r3gr3tz in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 08:28 AM
  2. Using unix command line to sort random array
    By lostmyshadow in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:14 PM
  3. New Sort an Array of Classes
    By ajpeters in forum C++ Programming
    Replies: 11
    Last Post: 09-06-2005, 06:23 PM
  4. how to sort out an array?
    By Chobo_C++ in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2004, 04:33 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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