Thread: Check if array is sorted

  1. #16
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by rmmstn View Post
    Well my logic was that, if neither counter is (n-1) in the end, that would mean that the array is not increasing/decreasing, but I think I didn't understand the task right. I believe 1, 2, 2, 3, 4 is still a sorted array since the elements are in increasing order (right?). 1, 1 ,1 is unsorted (because they're all equal?) and 1, 1, 0 is sorted because the elements are decreasing. Is this logic right? Does it matter if the elements are repeated?
    It really just depends on the convention you want to go with. I think hamster_nz's approach is a good one, although I would recommend encoding those return values as enums/defines so that the usage is a little more intuitive.

  2. #17
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by hamster_nz View Post
    Using size_t is the right thing to do, but feel free to use plain old int.

    'size_t' is just an appropriately sized unsigned integer that matches the processor's address space.
    Not necessarily. size_t is a type that can hold the size of the largest supported object. For example, on ye olde x86 segmented memory system, size_t might be only 16 bits wide (no object can be larger than 64 KiB), even though a pointer might be 20 bits wide (for a whopping 1 MiB of addressable memory).

    size_t is an appropriate type to use when referring to object sizes (e.g., with malloc) or indexes into arrays, since size_t is guaranteed to be capable of indexing every possible location in an array.

  3. #18
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by christop View Post
    Not necessarily. size_t is a type that can hold the size of the largest supported object. For example, on ye olde x86 segmented memory system, size_t might be only 16 bits wide (no object can be larger than 64 KiB), even though a pointer might be 20 bits wide (for a whopping 1 MiB of addressable memory).

    size_t is an appropriate type to use when referring to object sizes (e.g., with malloc) or indexes into arrays, since size_t is guaranteed to be capable of indexing every possible location in an array.
    Interesting caveat. I wasn't aware there was a distinction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to check if an array of doubles is sorted?
    By Lina_inverse in forum C Programming
    Replies: 20
    Last Post: 10-06-2012, 03:12 PM
  2. How to detect if a array is sorted.
    By just_rookie in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2012, 05:35 AM
  3. Selecting pivot in sorted array.
    By infantheartlyje in forum General Discussions
    Replies: 4
    Last Post: 10-14-2011, 09:43 AM
  4. how delete 1th element of a sorted array
    By vicky_4040 in forum C Programming
    Replies: 4
    Last Post: 10-11-2009, 06:12 AM
  5. finding max/min in a sorted array
    By Crcullen3916 in forum C++ Programming
    Replies: 9
    Last Post: 09-23-2008, 02:18 AM

Tags for this Thread