Saturday 19 March 2016

Sorted Order Check


Write a program to find whether the given array is sorted in ascending or descending order.

Input Format:
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. Assume that the maximum value of n is 15.

Output Format:

Print yes if the array is sorted in ascending or descending order. Print no if the array is not sorted in ascending or descending order .

Sample Input 1: 
 5
 2
 3
 6
 8
 10

Sample Output 1: 
 yes

Sample Input 2: 
 5
 20
 13
 6
 4
 1

Sample Output 2: 
yes

Sample Input 2: 
5
20
13
6
4
10

Sample Output 3: 
 no

Code:
  #include<stdio.h>
int main(){
  int a[15],n,i,aflag=0,bflag=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
    if(i>0){
      if((a[i-1]>a[i]))
        aflag++;
      else
        bflag++;
    }
  }
  if(aflag==(n-1)||(bflag==(n-1)))
    printf("yes");
  else
    printf("no");
  return 0;
}

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. If you want only asecending order check then you dont need bflag.So just remove it and change the condition..

    if(a[i-1]<a[j])
    {
    aflag++;
    }
    if(aflag==(n-1))
    printf("yes");
    else
    printf("no");

    ReplyDelete
  4. Very Interesting and good piece of information you have been shared and keep sharing the blogs...
    Check Ordering

    ReplyDelete
  5. Thanks for sharing ! This article will teach you straightforward ways in which to proceed concerning the method of ordering and designing checks.Click here to learn more

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. can you tell me why the "n" variable is used?

    ReplyDelete