Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:24:02+00:00 2026-06-09T16:24:02+00:00

I was messing around with NSArray functions earlier, and I think I may have

  • 0

I was messing around with NSArray functions earlier, and I think I may have happened across the easiest way to randomize a NSArray:

NSArray *randomize(NSArray *arr)
{
    return [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return arc4random_uniform(3) - 1; // one of -1, 0, or 1
    }];
}

Which, in theory, should randomize the NSArray thoroughly. However, after considerable thinking, I was wondering if this could possibly be unsafe, and theoretically turn into an infinite loop, depending on the sort algorithms used by NSArray.

I tested this on arrays on sizes of 10 – 100000, and I saw a linear performance difference (about N * (log10(N) + 2) comparisons per randomization), which isn’t bad.

However, is there ever a situation that could arise where the NSArray could never theoretically sort itself, and cause an application crash? It seems to me that it shouldn’t happen, but you never know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:24:04+00:00Added an answer on June 9, 2026 at 4:24 pm

    I think this depends on the underlying sorting algorithm.

    Consider what would happen if the underlying sort is a bubble sort. This means that any time you compare a pair of elements, there is a 1/3 chance that you will swap them (if the comparison makes them appear out of order). Consequently, if you were to sort an array of n elements with this comparison function, the probability that the algorithm terminates at each step is equal to the probability that none of the comparisons evaluate to “out of order.” Since each comparison says “out of order” with probability 1/3, this means that the probability that the algorithm terminates on each iteration is (2/3)n. This means that the expected number of iterations before the algorithm terminates is (3/2)n = 3n / 2n. If you try running this algorithm for a reasonably-sized array (say, n = 1000), then the expected number of iterations is going to be staggeringly huge; n = 1000 gives 1.233840597×10176 expected iterations! This algorithm will eventually terminate, but the expected runtime is so long that from a practical perspective it’s effectively infinite.

    On the other hand, if you try using a different algorithm, such as selection sort, you are not guaranteed to get a uniform distribution. For example, consider the very first pass of the algorithm, which will find the element to put at position 1. Every element in the array should (if the distribution really is uniform) have probability 1/n of being placed first. But this is not the case. Note that the first element will be kept in the first position unless it is swapped with something. This occurs only if the comparison comes up +1 (or -1, depending on the internals) at any point during the first scan. The probability that all the comparisons come back with a different value is (2/3)n-1, which is not the same as 1/n. In fact, it’s astronomically unlikely that the first element in the sequence will end up at the front once you’re done sorting. Consequently, even though the algorithm will terminate, there is no guarantee that you get a uniformly random distribution.

    If you try using something like quicksort, heapsort, or mergesort, then the algorithm will eventually terminate, but I’m not sure whether or not it’s guaranteed to be random. I’ll think about whether this is uniformly random for a bit and then update my answer.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been messing around with some coding and finally think I'm finally ready for
I am currently messing around with some code for an advertising network, i have
i have been messing around with a search engine and i am trying to
I was messing around with NIO earlier today and as numClients got larger (~
I have been messing around with iTunes COM from python. However, I haven't been
I am currently messing around on my linux system and now I have the
So I have been messing around creating and deleting (read destroying via TfsDeleteProject) in
I have been messing around with how it will be best for my new
I'm messing around with expression trees, but I'm little stuck. I have this expression:
I have been messing around with synchronization in Java and it has yet to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.