views:

138

answers:

4

I'm trying to sort an array least to greatest and i am really lost....

Here is what i have so far:

 int temp, temp2;
    for (int x = 0; x < array_size; x++)
    {
            temp=a[x];

            for (int i = 0; i < array_size; i++)
            {
                if (a[i] < temp)
                {
                    temp2=a[i];
                    a[i]=temp;
                    a[x]=temp2;
                }
            }
    }

updated: still not working and i have to use code.

int temp, temp2, x=-1;
for (int x = 0; x < array_size; x++)
{
        temp=a[x];

        for (int i = x+1; i < array_size; i++)
        {
            if (a[i] < temp)
            {
                temp2=a[i];
                a[i]=temp;
                a[x]=temp2;
            }
        }
}
+5  A: 

Unless this is homework and you're limited in which functions you can use:

#include <algorithm>
...
std::sort(a,a+array_size);
PigBen
+1  A: 

Have you looked at C's 'qsort' method? That'll sort your array.

C++ has its own built-in sort function too, as part of its standard library.

Can you not use either of those?

In your code, the inner loop ought to start with int i = x+1; rather than with i starting at 0.

Graham Perks
+1  A: 

You can use the STL sort algorithm.

In case you really want to hand code it, you may want to make some changes:

In the inner for loop, change

int i = 0

to

int i = x + 1

Also, reassign temp to a[i] inside the if.


Full code below:

// Arun Saha, 2010-Oct-20
// http://stackoverflow.com/questions/3983541/trying-to-sort-an-array

#include <iostream>
using namespace std;

void
mysort( int * a, size_t array_size ) {

    for( size_t i = 0; i < array_size; ++i ) {

        int minSoFar = a[i];

        for (size_t j = i+1; j < array_size; ++j ) {

            if( a[j] < minSoFar ) {

                minSoFar = a[j];

                int tmp = a[i];
                a[i]    = a[j];
                a[j]    = tmp;
            }
        }
    }
}

int
main() {

   int x[] = {40, 60, 10, 30, 20, 50};
   const size_t N = sizeof( x ) / sizeof( int );

   for( size_t i = 0; i < N; ++i ) {
       cout << x[ i ] << " ";
   }
   cout << endl;

   mysort( x, N );

   for( size_t i = 0; i < N; ++i ) {
       cout << x[ i ] << " ";
   }
   cout << endl;
}
ArunSaha
Why would i want to set a[i]=a[i]?
Alec
I said, `temp = a[i]` inside the `if() { ..}`
ArunSaha
@Alec: I took your code and made few tweaks to make it work. Check out the link to codepad, which has the full code and output of an example.
ArunSaha
Thank you this helped me a lot! Exactly what i needed to see to understand.
Alec
@Alec: You are welcome.
ArunSaha
@Arun, it's usually a better idea to post the code _here_ so as to keep SO as self-contained as possible. I always assume that every other site on the net will disappear (a reason I don't like nothing-but-a-link or LMGTFY answers).
paxdiablo
@paxdiablo: I cent-percent agree with you. However, whenever the code snippet is long and a scrollbar appears on the side, I hesitate to post it :(. Thank you for making the edit. I would keep your advice in mind.
ArunSaha
@Arun, sometimes I insert a ` ` line in the code (needs a blank line after it to get back into code mode) to ensure no scroll bar appears (generally only for relatively short code segments, say two pages in a scroll area). Long code, I'll leave alone so it doesn't pollute the answer.
paxdiablo
+3  A: 

You should use one of the library-provided sort routines but, for what it's worth, the canonical bubble sort can be done as follows:

def bubblesort (array, count):
    limit = count - 2
    didSwap = true
    while (didSwap) {
        didSwap = false
        for pos = 0 to limit:
            if array[pos] > array[pos+1]:
                temp = array[pos]
                array[pos] = array[pos+1]
                array[pos+1] = temp
                didSwap = true
            endif
        endfor
        limit = limit - 1
    endwhile
enddef

I would only use this where library-provided routines are not usable for some reason and, even then, only for small data sets.

It's relatively efficient (as far as bubble sort goes) since it doesn't re-check elements that have already been placed in the correct position (each iteration moves one more element to its correct position at the top of the list, hence the use of limit), and will exit after an iteration in which no swaps are done (i.e., the list is sorted).

paxdiablo