www.webdeveloper.com
Results 1 to 4 of 4

Thread: Check to see if an array is sorted

  1. #1
    Join Date
    Nov 2005
    Posts
    3

    Exclamation Check to see if an array is sorted

    Hey guys,

    Ok, basically, I had to copy an array of integers that another class sent to mine, and then do various things to it. The final thing I have to do is have the method sorted() check the array (localArray) to see whether or not it is sorted. (Ascending and Descending both count as sorted, no order what-so-ever doesnt.) The method then returns true if it is sorted, and false if it is not sorted. I sounds so simple for me, but I'm not sure why this isnt working. Here is the code I wrote:

    Code:
    public boolean sorted( ){
    for(int i=0;i<50;i++){
    if(localArray[i]<localArray[i+1]){
    return true;}
    else if(localArray[i]>localArray[i+1]){
    return true;}
    else{
    return false;} //by this point, the return statement should be set.
    }
    //compiler wants a return statement here(apparently the set statment above does not count.
    }
    Any ideas? Thanks.

  2. #2
    Join Date
    Sep 2005
    Location
    Chicago, Il
    Posts
    144
    It is hard to tell with the formatting here - I copied this to a different editor and got the following structure:

    Code:
    public boolean sorted( )
    {
    	for(int i=0;i<50;i++)
    	{
    		if(localArray[i]<localArray[i+1])
    		{
    			return true;
    		}
    		else if(localArray[i]>localArray[i+1])
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		} //by this point, the return statement should be set.
    	}
    //compiler wants a return statement here(apparently the set statment above does not count.
    }
    If this is right, this will never check the whole array. The first time through the for loop you will be return either true of false. You should not ever get a second loop.

    You are probably getting asked for that reutrn statement becuase the compiler is being dumb. Since a for loop has a possiblity of being false the first time through, the compiler probably requires a return statement after it. Although in your case this should not happen, I don't think the compiler actually checks the conditional, so it is just being dumb and wantign the return statement.


    Also, although I have not run this, if you actually did get all the way through the array, on the last element of the array (I assume it is 49), you would be checking against position 50. I am also assuming that the array is 50 spaces in size. This will give you an array out of bounds exception.

    To get rid of the first problem you can try the following:

    Code:
    public boolean sorted( )
    {
    	boolean isSorted = false;
    	
    	for(int i=0;i<50;i++)
    	{
    		if(localArray[i]<localArray[i+1])
    		{
    			isSorted = true;
    		}
    		else if(localArray[i]>localArray[i+1])
    		{
    			isSorted = true;
    		}
    		else
    			isSorted = false;
    			
    		if(isSorted != true)
    			return isSorted;
    	}
    	return isSorted;
    
    }
    This will loop throgh the entire array. On every element that is checked, if the elements are in order, it will assign true to isSorted. If they are not, then the dangling else set it to false. We then check to see if it was set to false at any time, if it was, then we break out of the loop and return flase right away - no use goign any farther. Otherwise it will go all the way to the end and eventually return true.



    For the array out of bounds issue, you can try (int i=0;i<49;i++). SInce you are always checking the current element againstthe NEXT element, when you get to the end of the array you actually don't have a next element to check against. therefor you want to stop one BEFORE the end of the array and check that against the very last item. I haven't run this, but I think these are the issues.

  3. #3
    Join Date
    Sep 2005
    Location
    Chicago, Il
    Posts
    144
    One other thing, i'm not sure your sort checking will work. What if you had the following values.



    50, 49, 53

    the first time it will see if 50 is smaller thean 49 - false, then look to see if 50 is larger than 49 - ture.

    second loop it will chekc to see if 49 is smaller than 53 - true


    So far you have always gotten through with a true. But you can see that
    50, 49, 53 is not sorted. You need to check ascending and descending seperately. There are ways to do this in a single loop. This isn't that trivial of an issue - it is why they give it as homework. Try reading up on somethign called a bubble sort - that should give you a clue on how to write the code to check for sorting.

    I can see how you would have thought this if you returned true or false out of every check - but you can't do that. The first time you would find something true, you would stop checking the array. You need to go all the way through an array before returning true to see if it is sorted. false you can return at any time.
    Last edited by BigDog; 02-02-2006 at 06:04 PM.

  4. #4
    Join Date
    Nov 2005
    Posts
    3
    thanks for the fast replies guys. i got it working through your examples and suggestions just fine. i can definitely see now that my way would have never worked. thanks again!

    good forums.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles