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 4 of 4

Thread: Linear Search

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Linear Search

    Hey Guys,

    I've been stuck on part on parts 5-6 for my lab. I have an array that holds random integers and have sorted them. I am using linear search to locate an integer.

    I would like to receive some help on parts 5 and 6 please.

    Now, I don't know how to calculate the average-running time in code. I am also confused on part 6 when they ask me to repeat for 500 times.
    Attached Images Attached Images

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Linear Search

    Please type in the text so it is readable. The image is not readable and it is not possible to copy any text from an image to include in a response.

    Also be sure to wrap any code you post in code tags.

    Also posted here: http://www.dreamincode.net/forums/to...linear-search/
    and here: https://coderanch.com/t/691466/java/...Search#3245817
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Linear Search

    Thank you for the information.

    Here is a direct link to the prompt referencing my question:

    https://i.imgur.com/2juMme4.png

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Random;
    import java.util.Scanner;
     
    public class LAB1 {
    	private int userNumber;
    	private ArrayList<Integer> userList = new ArrayList<Integer>();
    	private ArrayList<Integer> userCounter = new ArrayList<Integer>();
     
    	private static Random rand = new Random();
    	private boolean bool = false;
     
    	Scanner input = new Scanner(System.in);
     
    	public ArrayList<Integer> randomArray() {
     
    		while(!bool) {
    			System.out.println("Enter a positive number: ");
    			userNumber = input.nextInt();
    			if(userNumber > 0) {
    				bool = true;
    			}
    		}
    		for(int i = 0; i < userNumber; i++) {
    			int randomNumber = rand.nextInt(1000 + 1 + 1000) - 1000;
    			userList.add(randomNumber);
    			userCounter.add(i);
    			Collections.sort(userList);
    		}
     
    		//System.out.println(userCounter);
    		System.out.println(userList);
     
    		System.out.println("Pick a random number in the array: ");
    		Integer randomNumber = input.nextInt();
     
    		final long startTime = System.nanoTime();
     
    		//		for(int i = 0; i < 500; i++) {
     
    		linearSearch(userList, randomNumber);
    		//		}
    		final long duration = System.nanoTime() - startTime;
    		System.out.println(duration/1000000000 + " seconds ");
     
    		return userList;
    	}
     
     
    	public static int linearSearch(ArrayList<Integer> arrayList,Integer key) {
    		int counter = 0;
    		for( int i = 0; i < arrayList.size(); i++) {
    			if(key.equals(arrayList.get(i))) {
    				counter++;
    				if(counter <= 1)
    					System.out.println("number " + arrayList.get(i) + " was found " + counter + " time");
    				if(counter > 1) {
    					System.out.println("number " + arrayList.get(i) + " was found " + counter + " times");
    				}
    			}
    		}
    		return -1;
    	}
     
     
    	public static void main(String args[]) {
     
    		LAB1 obj = new LAB1();
     
     
    		obj.randomArray();
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Linear Search

    Can you type in the text for the questions you have so it can be copied into a response if needed?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Linear Search in an array
    By camaleonx13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2013, 01:42 PM
  2. How To Use Linear Search
    By Semion in forum Collections and Generics
    Replies: 17
    Last Post: May 13th, 2013, 12:40 AM
  3. Linear Search / Index Location of Strings
    By xJavaLover in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 07:11 AM
  4. [SOLVED] Linear array search(without Arraylist)
    By Usoda in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 18th, 2011, 01:12 AM
  5. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM