# Thread: How to detect if a array is sorted.

1. ## How to detect if a array is sorted.

Hi,all!
Is there a way to detect if a array is sorted or not? 2. Loop through the array with the same comparison(s) as a sort. For example, integers stored in ascending order means that the smaller integers will be encountered first. If you find an integer somewhere that is out of order then you know it isn't sorted. 3. Originally Posted by just_rookie
Is there a way to detect if a array is sorted or not?
whiteflags' suggestion is good for a novice to practice, but once you have that practice, it would be simpler to #include <algorithm> then use the is_sorted generic algorithm. 4. Great suggestion laserlight.

just_rookie if you would like to 'roll your own', just iterate over the array and check if the current element is 'less than' the next element. If you receive true over the entire array, it is sorted. 5. I thought that the definition of "sorted" implied an obvious simple algorithm for checking it.

You might benefit from learning how to write that. 6. Originally Posted by whiteflags Loop through the array with the same comparison(s) as a sort. For example, integers stored in ascending order means that the smaller integers will be encountered first. If you find an integer somewhere that is out of order then you know it isn't sorted.
my code as follows.I used your method,but it cannot solve this problem:
Code:
```:
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void displayarr( int arr[],int size )
{
for ( int i = 0;i < size;i++ )
{
cout << arr[i] << "  ";
}
}

int main()
{
int arr;
srand(time(NULL));
for ( int i = 0;i < 10;i++ )
{
arr[i] = rand() % 100;
}
for ( int i = 0 ;i < 9;i++ )
{
if ( arr[ i ] < arr[ i+1 ] )
{
cout << "Sorted!" << endl;
displayarr(arr,10);
}
}
}``` 7. Originally Posted by laserlight whiteflags' suggestion is good for a novice to practice, but once you have that practice, it would be simpler to #include <algorithm> then use the is_sorted generic algorithm.
whiteflags' suggestion is good for me,because I didn't learn generic algorithm.Thank you for your reply! 8. The biggest mistake you made was writing "Sorted!" and displaying the array each time around the loop. You won't know until you've compared each element in turn once. Your if statement is fine, but you don't do anything if the if condition is false, which is a mistake. 9. ##  Originally Posted by whiteflags The biggest mistake you made was writing "Sorted!" and displaying the array each time around the loop. You won't know until you've compared each element in turn once. Your if statement is fine, but you don't do anything if the if condition is false, which is a mistake.
hi,someone send me a piece of code as follows,but i can not understand this code well,so please do me a favor!Thank you!
Code:
```#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
int i;
int p;
cout << "The original array is:" << endl;
srand(time(NULL));
for(i=0;i<4;i++)
{
p[i] = rand()%10;
cout << p[i] << "  ";
}
for(i=0;i<3;i++)
{
if(p[i]>p[i+1])
{
break;
}
}
if(i==3)
{
printf("Ascending array！\n");
}
else
{
for(i=0;i<3;i++)
{
if(p[i]<p[i+1])
{
break;
}
}
if(i==3)
{
printf("Descending array！\n");
}
else
{
printf("Unsorted array！\n");
}
}
free(p);
return 0;
}``` 10. If I were you, I would write a function like this:
Code:
```bool array_is_sorted(int arr[], int size)
{
// ...
}```
Then call it from the main function. Originally Posted by just_rookie
hi,someone send me a piece of code as follows,but i can not understand this code well,so please do me a favor!Thank you!
Concentrate on your own code. You are not that far away from a working solution. 11. just_rookie, it looks like the logic you have in your previous post's code is correct.

What is the problem? 12. Originally Posted by rodrigorules just_rookie, it looks like the logic you have in your previous post's code is correct.

What is the problem?
My problem is why we need the "if(i==3)" statement? 13. Originally Posted by just_rookie
My problem is why we need the "if(i==3)" statement?
That is one way to check if all elements of the array have been checked. The array has 4 elements, so you only need to check for 3 elements (since you are checking the current and next). However, if you write the function that I suggested, you don't need to do this since you would just return true after the loop ends. 14. Originally Posted by laserlight That is one way to check if all elements of the array have been checked. The array has 4 elements, so you only need to check for 3 elements (since you are checking the current and next). However, if you write the function that I suggested, you don't need to do this since you would just return true after the loop ends.
But when i =2,we can get "if(p>p)…" or "if(p<p)…",so i mean that we already checked "p",why we need to do it again? 15. You actually never check p because p is outside the valid range of array elements. The only reason the if (i==3) is necessary is because the implementation you were gifted is so ad hoc that there is no other way to determine the results. You were doing better on your own. You were also given fairly blunt hints in the thread, so I earnestly believe you have this. Popular pages Recent additions 