简体   繁体   English

C++ 数组排序 - 将“bbba”和“0001”视为不正确排序的问题

[英]C++ Array Sort - problem accounting for "bbba" and "0001" as incorrect sorts

 //is the array ordered?
    //if there are one or zero elements, inherently sorted
    //can't compare to other elements if they don't exist
    if ( len == 0 || len ==1 )
    {
        return true;
        cout<<"isOrdered"<<endl;
    }
    //if more than 1 element <> can do a comparison
    for (int i=0; i<len; i++)
    {
          if ( sArr[i] > sArr[i+1])
          {
            return false;
            cout<<"notOrdered"<<endl;
          }
          else if (sArr[i]<=sArr[i+1])
            {
              return true; //here it must be ordered.
            }
    }

} }

sort logic taken from: https://www.geeksforgeeks.org/program-check-array-sorted-not-iterative-recursive/排序逻辑取自: https : //www.geeksforgeeks.org/program-check-array-sorted-not-iterative-recursive/

This code is in a larger function, which turns strings and unsigned ints into arrays, and checks if the order of elements is ASCII ordered from lowest to highest.这段代码在一个更大的函数中,它将字符串和无符号整数转换为数组,并检查元素的顺序是否为 ASCII 从低到高排序。

Problem is that it does not treat "abba" or "111110" as not ordered cases.问题是它不会将“abba”或“111110”视为非有序案例。 it continually returns true for cases like this and I don't understand why.对于这样的情况,它不断返回 true,我不明白为什么。 Doesn't the code compare element by element to the one right next to it?代码不是逐个元素与它旁边的元素进行比较吗?

You don't need to test for 0 or 1 at start (since the for loop already has the i<len condition and will be false for both 0 and 1) but you do need to move the return true out of the loop.您不需要在开始时测试 0 或 1(因为 for 循环已经具有i<len条件并且对于 0 和 1 都是假的)但您确实需要将return true移出循环。 You don't know that it's sorted until you've compared all elements:在比较所有元素之前,您不知道它已排序:

for(int i=1; i<len; ++i) {
    if(sArr[i-1] > sArr[i]) return false;
}
return true;

from your code, you need to return at least there is one out of order and then need to return true when all are okay.从您的代码中,您需要至少返回一个乱序,然后在一切正常时返回 true。

** also need to run only till (length -1) ** 也只需要运行直到(长度 -1)

for (int i=0; i<len-1; i++)
{
      if ( sArr[i] > sArr[i+1])
      {
        cout<<"notOrdered"<<endl;
        return false;
      }
}
cout<<"isOrdered"<<endl;
return true;

Testing for 0 and 1 is a good idea in the recursive case.在递归情况下,测试 0 和 1 是一个好主意。 This function could be called recursively, so you can leave that out.这个函数可以递归调用,所以你可以省略它。 You have left out the recursion and used a for loop instead.您已经省略了递归,而是使用了 for 循环。 Here is the corrected code, which waits until after the for loop to return true if the current pairs are sorted correctly.这是更正后的代码,如果当前对排序正确,它会等到 for 循环之后返回 true。

#include <iostream>

using namespace std;

bool IsArraySorted(int sArr[], int len)
{
  //is the array ordered?

  //if more than 1 element <> can do a comparison
  for (int i = 0; i < len - 1; i++) {
    if (sArr[i] > sArr[i + 1]) {
      cout << "notOrdered" << endl; // call before return :-)

      return false;
    }
  }
  cout << "Ordered" << endl;

  return true; //here it must be ordered.

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM