對數組遍歷一篇就可知曉數組是否有序

#include<iostream> 
#include<cstdlib> 
#include<ctime> 
#include<iterator> 
#include<algorithm> 
using namespace std; 
int main() 
{ 
    const int len=10; 
    int a[len],i; 
    srand((unsigned)time(NULL)); 
    for(i=0;i!=len;i++) 
    { 
        a[i] =rand(); 
    } 
    copy(a,a+len,ostream_iterator<int>(cout,"  ")); 
    int judge1=0,judge2=0; 
    for(i=0;i<len-1;i++) 
    { 
        if(a[i]>a[i+1]) 
            judge1 =1; 
        if(a[i]<a[i+1]) 
            judge2 =1; 
        if(judge1&&judge2) 
            break; 
    } 
    i==len-1 ? (cout << endl << "Sorted" << endl) : (cout << endl << "Not sorted." << endl) ; 
    return 0; 
}
相關文章
相關標籤/搜索