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 8473927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:29:21+00:00 2026-06-10T17:29:21+00:00

Ok,still learning Arrays. I wrote this code which fills the array named rand with

  • 0

Ok,still learning Arrays.
I wrote this code which fills the array named “rand” with random numbers between 0 and 1( exclusive).
I want to start learning Complexity.
the For loop executes n times (100 times) ,every time it takes O(1) time,so the worse case scenario is O(n),am I right?
Also,I used ArrayList to store the 100 elements and I imported “Collections” and used Collections.sort() method to sort the elements.

import java.util.Arrays;
    public class random
    {
        public static void main(String args[])
        {
            double[] rand=new double[10];

                    for(int i=0;i<rand.length;i++)
                    {
                        rand[i]=(double) Math.random();
                        System.out.println(rand[i]);
                    }   

                    Arrays.sort(rand);
                    System.out.println(Arrays.toString(rand));
        }
    }

ArrayList:

import java.util.ArrayList;
import java.util.Collections;

public class random
{
    public static void main(String args[])
    {
        ArrayList<Double> MyArrayList=new ArrayList<Double>();


        for(int i=0;i<100;i++)
        {               
            MyArrayList.add(Math.random());
        }

        Collections.sort(MyArrayList);


        for(int j=0;j<MyArrayList.size();j++)
        {
            System.out.println(MyArrayList.get(j));
        }

    }
}
  • 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-10T17:29:23+00:00Added an answer on June 10, 2026 at 5:29 pm

    Yes you are correct, the complexity of adding N items is O(N). Sorting will take an additional O(N * log(N)) in most cases according to the docs:

    The sorting algorithm is a tuned quicksort, adapted from Jon L.
    Bentley and M. Douglas McIlroy’s “Engineering a Sort Function”,
    Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
    1993). This algorithm offers n*log(n) performance on many data sets
    that cause other quicksorts to degrade to quadratic performance.

    ArrayList is still backed by an array, so the complexities will be the same, bar the occasional resizing which is amortized anyway.

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

Sidebar

Related Questions

I'm still learning to code in AS3, which seems pretty straight forward and similar
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
Still learning WPF....thanks for any help. Is there any way to Refactor this: <ListBox
Still learning CSS so pardon if this is obvious - I am building a
Still learning JSP Web Applications here. I have been doing this for a while
I'm still learning js and trying out the Webstorm IDE, which seems sweet (including
Hi sorry still learning here and slow to learning code arguments. Just wondering could
Newbie still learning, been trying to search and hack code together for hours now,
I'm still learning what hibernate can do and this time i'm trying something that
The following code displays random database images as well as one specific image (which

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.