C++ 正在尝试对数组排序

C++ 正在尝试对数组排序,c++,arrays,sorting,C++,Arrays,Sorting,我正在尝试将数组从最小到最大排序,我真的迷路了 以下是我到目前为止的情况: 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) {

我正在尝试将数组从最小到最大排序,我真的迷路了

以下是我到目前为止的情况:

 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;
                }
            }
    }
int-temp,temp2;
对于(int x=0;x
更新:仍然不工作,我必须使用代码

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;
            }
        }
}
int-temp,temp2,x=-1;
对于(int x=0;x
除非这是一项家庭作业,而且您只能使用以下功能:

#include <algorithm>
...
std::sort(a,a+array_size);
#包括
...
std::sort(a,a+数组大小);

你看过C的“qsort”方法吗?这将对数组进行排序

C++也有自己的内置排序函数,作为其标准库的一部分

这两种你都不能用吗

在代码中,内部循环应该以
inti=x+1开始而不是从0开始的i。

您可以使用STL算法

如果您真的想手工编码,您可能需要进行一些更改:

在内部
for
循环中,更改

int i = 0

另外,如果
中有
temp
,则将
temp重新分配给
a[i]


完整代码如下:

// 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;
}
//阿伦萨哈,2010年10月20日
// http://stackoverflow.com/questions/3983541/trying-to-sort-an-array
#包括
使用名称空间std;
无效的
mysort(int*a,大小\u t数组大小){
对于(大小i=0;i<数组大小;++i){
int minSoFar=a[i];
对于(大小j=i+1;j<数组大小;++j){
if(a[j]cout您应该使用库中提供的排序例程之一,但是,无论如何,规范冒泡排序可以按如下方式进行:

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
我只会在库提供的例程由于某种原因不可用的情况下使用它,即使这样,也只适用于小数据集


它相对高效(就冒泡排序而言),因为它不会重新检查已经放置在正确位置的元素(每次迭代都会将一个以上的元素移动到列表顶部的正确位置,因此使用
限制
),并且会在没有进行交换的迭代后退出(即列表已排序)。

为什么我要设置a[i]=a[i]?我说,
temp=a[i]
if(){..}
@Alec:我接受了你的代码,并对其进行了一些调整。请查看codepad的链接,其中包含完整的代码和一个示例的输出。谢谢你,这对我帮助很大!这正是我需要了解的。@Arun,通常最好将代码发布在这里,以便尽可能保持独立。我总是认为当你说“我必须使用代码”时你的意思是?你不能使用任何库函数?那么你的问题是如何在C++中做一个冒泡排序?是的,我的意思是我必须自己编码它,我不能使用你下面展示的库函数。删除了<代码>可能的作业< /Cord>标签-从这点开始,元标记被明确地劝阻了。Alec,我可以看到历史。我不是在攻击你,只是告诉大家我为什么这么做。干杯。访问查看你最喜欢的算法的实现和图形表示。
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