开发者

Trying to sort an array

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;
            }
        }
}


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

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


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).


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜