?
Solved

Big O notation for an array

Posted on 2009-07-05
7
Medium Priority
?
685 Views
Last Modified: 2013-11-23
Hi,

What is the complexity of an array for getting an element using index like a[10].
0
Comment
Question by:SunScreenCert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 2000 total points
ID: 24781766
The complexity is O(1) as it takes the exact smae amount of time to access an element regardless of the size of the list.
0
 
LVL 20

Expert Comment

by:a_b
ID: 24782553
Since you the index number that you want to address you donot have to traverse the array you can directly access it without looping through the array, so it is just one operation, therefore O(1).

It would be a different story if you had to access maybe the largest or the smallest element of the array.
0
 
LVL 2

Author Comment

by:SunScreenCert
ID: 24782738
I asked this question coz somebody was suggesting that this program be split into different arrays. Please tell me if this would give me any additional advantage. Also i am novice to programming, so all tips to make it efficient and coding standards etc are welcome.

class Say{
      static String[] print= {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty", "hundred"};
      public static void main(String... args){
            try{
                  int i = Integer.parseInt(args[0]);
                  if(i >= 0 && i < 21)
                  System.out.println(print[i]);
                  else{
                        int length = args[0].length();
                        char[] arr= args[0].toCharArray();
                        if(length == 2){
                              //int i = Integer.parseInt(args[0].charAt(0));
                              switch(arr[0]){
                                    case 2:
                                          System.out.print(print[20]);
                                          break;
                                    case 3:
                                          System.out.print(print[21]);
                                          break;
                                    case 4:
                                          System.out.print(print[22]);
                                          break;
                                    case 5:
                                          System.out.print(print[23]);
                                          break;
                                    case 6:

                                          System.out.print(print[24]);
                                          break;
                                    case 7:
                                          System.out.print(print[25]);
                                          break;
                                    case 8:
                                          System.out.print(print[26]);
                                          break;
                                    case 9:
                                          System.out.print(print[27]);
                                          break;
                                    }
                                 System.out.print(print[(int)arr[1]]);
                                                                           
                        }
}
                  
            }catch(NumberFormatException e){
                  System.out.println("Please print only digits");
            }catch(ArrayIndexOutOfBoundsException e){
                  System.out.println("Print only valid numbers");
            }
      }
}
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 20

Expert Comment

by:a_b
ID: 24782755
What is the purpose of the program?
0
 
LVL 2

Author Comment

by:SunScreenCert
ID: 24782969
It takes input as numbers ranging from 0- 100 and return them in words
0
 
LVL 20

Expert Comment

by:a_b
ID: 24783099
Try this out -

 
public class Test
{
    
 
    public static void main (String... args)
    {
        String[] print = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
                "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
                "eighty", "ninty", "hundred"};
 
        try
        {
            String str = "45";
            int i = Integer.parseInt(str);
            if (i >= 0 && i < 21)
                System.out.println(print[i]);
            else
            {
                int length = str.length();
                char[] arr = str.toCharArray();
                if (i < 21)
                    System.out.println(print[arr[0]]);
                else
                {
                    int a = arr[0]-48+18; 
                    int b = arr[1]-48; 
                    System.out.println(print[a]+" "+print[b]);
 
                }
           }
 
        }
        catch (NumberFormatException e)
        {
            System.out.println("Please print only digits");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Print only valid numbers");
            e.printStackTrace();
        }
    }
}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question