Home > Software engineering >  Need to check if array is sorted using a function in C
Need to check if array is sorted using a function in C

Time:11-24

Basically, we need to check if the elements of an 1D array are sorted using a function: if they are sorted in an ascending order: return 1 if they are sorted in an descending order: return -1 if they are not sorted: return 0 This is the method im using, however it doesnt return 1 but isntead 0, Im unsure where is the problem, Any comments or new methods of solving the problem are welcome since im a beginner.

int Is_Sorted(int* A, int n){
    int tempcr, tempdcr;

for (int i=0; i<N-1; i  ){

    if (A[i]<=A[i 1]){
        tempcr  ;
    }else if (A[i]>=A[i 1]){
    tempdcr  ;
    }
}
   if(tempcr==N-1){
        return 1;
   }else if(tempdcr==N-1)
        return -1;
   else
    return 0;

}

CodePudding user response:

Wrong logic

OP's code fails due to

}else if (A[i]>=A[i 1]){
tempdcr  ;

should be

}
if (A[i]>=A[i 1]) {
  tempdcr  ;

Consider the case when A[i]==A[i 1], both counters should increment.

Junk Values

Missing initialization @kaylum.

// int tempcr, tempdcr;
int tempcr = 0;
int tempdcr = 0;

Alternative approach:

There are 4 possibilities

  • Array has the same value value every where - or is of length 0.

  • Array is ascending. A[i] >= A[i-1] for all i > 0 and length is more than 0.

  • Array is descending. A[i] <= A[i-1] for all i > 0 and length is more than 0.

  • None of the above.

Simply loop and adjust two flags. int tempcr, tempdcr; counters not needed.

int Is_Sorted(const int* A, int n) {
  bool isAscending = true;
  bool isDescending = true;
  for (int i = 1; i<n; i  ) { // start at 1
     if (A[i] < A[i-1]) isAscending = false;
     if (A[i] > A[i-1]) isDescending = false;
  }
  if (isAscending && isDescending) {
    return TBD; // Unsure what OP wants here
  }
  if (isAscending) {
    return 1;
  }
  if (isDescending) {
    return -1;
  }
  return 0;
}

Simplifications and some micro optimization possible, but something to clarify a clear approach.

CodePudding user response:

For starters the function should be declared like

int Is_Sorted( const int* A, size_t n );

that is at least the first parameter should have the qualifier const because the passed array is not being changed within the function.

The variables tempcr and tempdcr were not initialized and have indeterminate values. So the function has undefined behavior. You have to initialize them like

int tempcr = 0, tempdcr = 0;

There is no sense to continue iterations of the loop if it is already known that the array is unsorted because it is inefficient.

Moreover the function has a logical bug.

Consider the array { 0, 0, -1 }

In this case in the first iteration of the loop the variable tempcr will be increased due to the if statement

if (A[i]<=A[i 1]){
    tempcr  ;
}else if (A[i]>=A[i 1]){
tempdcr  ;
}

But in the second iteration of the loop the variable tempdcr will be increased.

So the function will report that the array is unsorted though it is sorted in the descending order.

I would define the function the following way

int is_sorted( const int a[], size_t n )
{
    size_t ascending = 0, descending = 0;

    for (size_t i = 1; ( ascending == 0 || descending == 0 ) && i < n; i  )
    {
        if ( a[i-1] < a[i] )   ascending;
        else if ( a[i] < a[i-1] )   descending;
    }

    return descending == 0 ? 1 : ( ascending == 0 ? -1 : 0 );
}

If the passed array has all elements equal each other then the function considers it as sorted in the ascending order.

As pointed @chux - Reinstate Monica in his answer instead of counting elements you can use the corresponding variables as boolean objects. In this case the function can look like

int is_sorted1( const int a[], size_t n )
{
    int ascending = 0, descending = 0;

    for (size_t i = 1; ( ascending == 0 || descending == 0 ) && i < n; i  )
    {
        if ( a[i-1] < a[i] ) ascending = 1;
        else if ( a[i] < a[i-1] ) descending = 1;
    }

    return descending == 0 ? 1 : ( ascending == 0 ? -1 : 0 );
}
  • Related