Solved

Big O notation for an array

Posted on 2009-07-05
7
673 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 …
This video teaches viewers about errors in exception handling.
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 …

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now