Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无论如何,使这个排序算法更有效/更短的C++_C++_Sorting_If Statement_For Loop_While Loop - Fatal编程技术网

无论如何,使这个排序算法更有效/更短的C++

无论如何,使这个排序算法更有效/更短的C++,c++,sorting,if-statement,for-loop,while-loop,C++,Sorting,If Statement,For Loop,While Loop,有人知道是否有一种方法可以使用for或while循环将数组中的数字从左到右按升序重新排列,而不仅仅是使用3个if语句,比如: // Number Sorting Algorithm - Trey Taylor 2014 #include <iostream> #include <cstdlib> #include <string> using namespace std; int main() { int numbersort []

有人知道是否有一种方法可以使用for或while循环将数组中的数字从左到右按升序重新排列,而不仅仅是使用3个if语句,比如:

// Number Sorting Algorithm - Trey Taylor 2014
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
    int main() {
        int numbersort [] = {200, 85, 63, 4}
        cout << "The numbers scrambled are: ";
        cout << numbersort[0] << ", ";
        cout << numbersort[1] << ", ";
        cout << numbersort[2] << ", ";
        cout << numbersort[3] << ", " << endl;
        firstlast:
        if (numbersort[0] > numbersort[1]) {
            int temp = 0;
            temp = numbersort[0];
            numbersort[0] = numbersort[1];
            numbersort[1] = temp;
        }
            if (numbersort[1] > numbersort[2]) {
                int temp = 0;
                temp = numbersort[1];
                numbersort[1] = numbersort[2];
                numbersort[2] = temp;
            }
                if (numbersort[2] > numbersort[3]) {
                    int temp = 0;
                    temp = numbersort[2];
                    numbersort[2] = numbersort [3];
                    numbersort[3] = temp;
                }
                        while (numbersort[0] > numbersort[1]) {
                        goto firstlast;
                }
        cout << "The numbers unscrambled are: ";
        cout << numbersort[0] << ", ";
        cout << numbersort[1] << ", ";
        cout << numbersort[2] << ", ";
        cout << numbersort[3] << ", ";
}

您可以使用std::sort或冒泡排序算法。算法检查循环中的当前数字是否比下一个大。如果是,请更改数组中的数字。最后,在数组的末尾得到最大的数,在数组的末尾得到最小的数

i = 0;
while (i < (sizeof(numbersort)/sizeof(numbersort[0])) - 1)
{
  if (numbersort[i] >= numbersort[i + 1])
  {
    std::swap(numbersort[i], numbersort[i + 1]);
    i = 0;
  }
  else
  {
    ++i;
  }
}

这似乎属于最短的解决办法就是打电话。请参阅链接以获取示例。如果您希望有人查看您的代码,您应该询问Use std::swap。将未使用的变量从循环中剔除。以及使用while和goto的方式。。。嗯,有一个特殊的结构叫做do…而对于这个
int main()
{
    int numbersort[] = { 200, 85, 63, 4 };
    int temp = 0;
    // This loop sorts all numbers in the array
    for (int z = 0; z < 4; ++z)
    {
        // This loop sorts only one number to the end
        for (int i = 0; i < 3; ++i)
        {
            if (numbersort[i] > numbersort[i + 1])
            {
                temp = numbersort[i];
                numbersort[i] = numbersort[i + 1];
                numbersort[i + 1] = temp;
            }
        }
    }

    cout << "Sorted numbers" << endl;
    for (int i = 0; i < 4; ++i)
    {
        cout << numbersort[i] << endl;
    }

    system("pause");
    return 0;
}