Results 1 to 4 of 4

Thread: Check to see if an array is sorted

  1. #1
    Join Date
    Nov 2005

    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:

    public boolean sorted( ){
    for(int i=0;i<50;i++){
    return true;}
    else if(localArray[i]>localArray[i+1]){
    return true;}
    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
    Chicago, Il
    It is hard to tell with the formatting here - I copied this to a different editor and got the following structure:

    public boolean sorted( )
    	for(int i=0;i<50;i++)
    			return true;
    		else if(localArray[i]>localArray[i+1])
    			return true;
    			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:

    public boolean sorted( )
    	boolean isSorted = false;
    	for(int i=0;i<50;i++)
    			isSorted = true;
    		else if(localArray[i]>localArray[i+1])
    			isSorted = true;
    			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
    Chicago, Il
    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
    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10078 seconds
  • Memory Usage 2,858KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates