#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{ 
  //int a[10]={2,5,1,7,0,10,145,14,15,9};
  int a[10]={1,2,3,4,5,6,7,8,9,10};
  int tem,i=0,j,isTRUE=0;
  for(j=0; j<10; j++)
  {
     if(a[j]<a[j+1])  
     {
       isTRUE=1;
       break;
     }
     isTRUE=0;
  }
  
 if(isTRUE)
 printf("inorder\n");
 else
 printf("ASEorder\n");
  
    getch();
    return 0;
}

Here's my source code about checking an array is ordered or not
But it didn't work properly, but i cannot find any mistake
Help me!
Thank you!

Recommended Answers

All 3 Replies

You need two loops to do that. You have to check each number against all the previous values.

int a[10] = {5,6,7,8,9,10.11,12,13,1};
int i,j,isTrue;
isTrue = 1; // assume true
for(i = 0; i < 9 && isTrue == 1; i++)
{
    for( j = i+1; j < 10; j++)
   {
       if( a[i] > a[j] )
       {
             isTrue = 0;
             break;
      }
}

I think this will do..

int main()
{
  int a[10]={12,15,19,27,20,31,45,54,59,90};
//int a[10]={1,2,3,3,5,6,7,8,9,10};
 int flag;
 flag=find_order(a,sizeof(a)/sizeof(a[0]));
 if(flag)
 printf("In order\n");
 else
 printf("Not In order\n");
 return 0;
}

int find_order(int a[],int high)
{ 
  int j;
  for(j=0; j<high; j++)
  {
     if(a[j]<=a[j+1])  
           continue;
      else 
         break;
  }
if(j==high)
         return 1;
else
    return 0;
}

You may try this code,

void isSorted(int a[], int n) {
  int i;
  /* descending order */
  for (i=1;i<n;++i) {
    if (a[i]<a[i-1]) {
      break;
    }
  }
  if ( i < n ) { /* failed descending order */
    for (i=1;i<n;++i) {  /* ascending order? */
       if (a[i]>a[i-1]) {
          break;
       }
    }
  if ( i < n ) {
      printf("Array is not sorted\n");
  }
  else {
    printf("Array is sorted (descending)\n");
   }
  }
 else {
   printf("Array is sorted (ascending)\n");
 }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.