遍历一次,测试数组是否排好了:html
// 只是迭代一次,判断一个数组是否按照小到大排序 #include <iostream> using namespace std; bool issort(int array[], int length) { int i; // 从后向前比较,若是发现后一个较小返回false for (i = length - 1; i > 0; i -= 1) { if (array[i] < array[i - 1]) { return false; } } // 没有发现返回true return true; } int main (int argc, char const* argv[]) { int array1[10] = {0, 1, 2, 3, 4, 0, 1, 2, 3, 5}; int array2[10] = {0, 1, 2, 3}; cout << issort(array1, 10) << endl; cout << issort(array2, 4) << endl; return 0; }