Hi,all!
Is there a way to detect if a array is sorted or not?
Printable View
Hi,all!
Is there a way to detect if a array is sorted or not?
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.
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.Quote:
Originally Posted by just_rookie
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.
I thought that the definition of "sorted" implied an obvious simple algorithm for checking it.
You might benefit from learning how to write that.
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[10];
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);
}
}
}
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[4];
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;
}
If I were you, I would write a function like this:
Then call it from the main function.Code:bool array_is_sorted(int arr[], int size)
{
// ...
}
Concentrate on your own code. You are not that far away from a working solution.Quote:
Originally Posted by just_rookie
just_rookie, it looks like the logic you have in your previous post's code is correct.
What is the problem?
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.Quote:
Originally Posted by just_rookie
You actually never check p[3] because p[3] 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.