Solved

Big O notation for an array

Posted on 2009-07-05
7
678 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
  • 3
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 48
JavaFX TableView not displaying correctly 3 109
sql import cannot be resolved jsp 3 78
Bot application - advice 3 64
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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