-5
// 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] << ", ";
}
有誰知道,如果有一種方法使用for或while循環由左到陣列中的號碼重新排列升序排列正確的,而不是僅僅使用3 if語句反正做這個排序算法更高效/更短的C++
這似乎屬於http://codereview.stackexchange.com/ –
最短的解決方法是調用['標準:: sort'(http://en.cppreference.com/w/cpp /算法/排序)。查看鏈接的例子。 – 5gon12eder
如果您想讓某人查看您的代碼,您應該詢問[codereview.se] –