Hi,all!
Is there a way to detect if a array is sorted or not?
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.Originally Posted by just_rookie
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Last edited by rodrigorules; 09-09-2012 at 12:13 AM. Reason: clarification
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 homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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; }
Last edited by just_rookie; 09-09-2012 at 09:11 PM.
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.Originally Posted by just_rookie
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
just_rookie, it looks like the logic you have in your previous post's code is correct.
What is the problem?
Check out my programming / algorithm blog @ http://www.swageroo.com
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.Originally Posted by just_rookie
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.