Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Whats the big-o notation (running time) of the following in java?

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Whats the big-o notation (running time) of the following in java?

    Hi, I am doing some study for an upcoming exam, I was wondering if anyone could help me figure out the running time of these arrays/ methods in java? Also if anyone has any good resources for studying data structures and algorithms I would be very grateful.

    Can anyone tell me the running time of the following parts of the class definition?

    The array declaration AND initialization shown at ‘A’. b) The method isSet() shown at ‘B' c) The method contains() shown at ‘C’. d) The method sort() shown at ‘D’.

    Thanks in advance!!

    CODE:

    `````````````````````````````````````````````````` `````````````````````````

    package ie.gmit;

    import java.util.Arrays;

    public class Numbers {
    private int[] numbers = {87,32,29,13,6,45,32,17}; //<-----A

    public boolean isSet() { //<----B
    for (int i = 0; i < numbers.length; i++) {
    for (int j =0; j < numbers.length; j++) {
    if (i != j && numbers[i] == numbers[j])
    return false;

    }
    }
    return true;
    }

    public boolean contains (int num) { // <------ C
    return Arrays.binarySearch(numbers, num)> -1;

    }

    public void sort() { //<------D
    Arrays.sort(numbers);
    }

    }


    `````````````````````````````````````````````````` `````````````````````````

  2. #2
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Whats the big-o notation (running time) of the following in java?

    import java.util.Arrays;
     
    public class Numbers {
     
    	public static void main(String[] args) {
     
    		System.out.println(isSet());
    		System.out.println(contains(6));
    		sort();
     
    	}
     
    	private static int[] numbers = { 87, 32, 29, 13, 6, 45, 32, 17 }; // <-----A
     
    	public static boolean isSet() { // <----B
    		for (int i = 0; i < numbers.length; i++) {
    			for (int j = 0; j < numbers.length; j++) {
    				if (i != j && numbers[i] == numbers[j])
    					return false;
     
    			}
    		}
    		return true;
    	}
     
    	public static boolean contains(int num) { // <------ C
    		return Arrays.binarySearch(numbers, num) > -1;
     
    	}
     
    	public static void sort() { // <------D
    		Arrays.sort(numbers);
    		for (int k : numbers) {
    			System.out.print(k + " ");
    		}
    	}
     
    }


    Can't tell you the run time but i thought that i would just add somethings.
    "Tick, tack"

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    25
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Whats the big-o notation (running time) of the following in java?

    <<Easy Learning Data Structures & Algorithms Java Practice >> good for you

    en.verejava.com

    package com.tool.util;
     
    import java.util.Arrays;
     
    public class Numbers {
     
        private int[] numbers = { 87, 32, 29, 13, 6, 45, 32, 17 }; 
     
        public boolean isSet() { 
            for (int i = 0; i < numbers.length; i++) {
                for (int j = 0; j < numbers.length; j++) {
                    if (i != j && numbers[i] == numbers[j])
                        return false;
                }
            }
            return true;
        }
     
        public boolean contains(int num) { 
            return Arrays.binarySearch(numbers, num) > -1;
        }
     
        public void sort() { 
            Arrays.sort(numbers);
        }
     
        public void print()
        {
            for(int i=0;i<numbers.length;i++)
            {
                System.out.print(numbers[i]+", ");
            }
        }
     
        public static void main(String[] args) {
     
            Numbers numbers=new Numbers();
            numbers.sort();
            numbers.print();
     
        }
     
    }

Similar Threads

  1. current execution time of a class in java by running another class
    By priyap761 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2014, 06:12 AM
  2. Replies: 4
    Last Post: October 1st, 2013, 01:31 AM
  3. Big-O Notation question
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: November 28th, 2011, 09:52 PM
  4. set time limit on running of a thread
    By z.zojaji in forum Threads
    Replies: 3
    Last Post: July 10th, 2010, 10:57 AM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM

Tags for this Thread