상세 컨텐츠

본문 제목

Arrays 101 Code from Java to Go(Search in an Array)

Go/Leet Code

by Gopythor 2022. 3. 2. 19:19

본문

728x90
반응형

Search in an Array

Linear Search

  • The time complexity for a linear search is O(N)O(N)

    Edge case refers to a problem that occurs when the value of data processed by the algorithm exceeds a certain range according to the characteristics of the algorithm. For example, a problem may occur when the value of the variable called fixnum exceeds the range of -128 to 127.
    https://kathak33.tistory.com/19

  • Let's see the linear search algorithm in action, with all the edge cases handled properly. When we say edge cases, we basically mean scenarios that you wouldn't expect to encounter. For example, the element you're searching for might not even exist in the Array. Or, an even rarer, but possible, scenario is that the input Array doesn't contain any elements at all, or perhaps it is null. It's important to handle all of these edge cases within the code.

Code

Java

public static boolean linearSearch(int[] array, int length, int element) {
    // Check for edge cases. Is the array null or empty?
    // If it is, then we return false because the element we're
    // searching for couldn't possibly be in it.
    if (array == null || length == 0) {
        return false;
    }

    // Carry out the linear search by checking each element,
    // starting from the first one.
    for (int i = 0; i < length; i++) {
        // We found the element at index i.
        // So we return true to say it exists.
        if (array[i] == element) {
            return true;
        }
    }

    // We didn't find the element in the array.
    return false;
}

Go

func linearSearch(array []int, length, element int) bool {
    // Check for edge cases. Is the array null or empty?
    // If it is, then we return false because the element we're
    // searching for couldn't possibly be in it.
    if array == nil || length == 0 {
        return false
    }

    // Carry out the linear search by checking each element,
    // starting from the first one.
    for i := 0; i < length; i++{
        // We found the element at index i.
        // So we return true to say it exists.
        if array[i] == element{
            return true
        }
    }

    // We didn't fine the element in the array
    return false
}

Code

Java

public class ArraySearch {
    public static void main(String args[]) {

        // Declare a new array of 6 elements
        int[] array = new int[6];

        // Variable to keep track of the length of the array
        int length = 0;

        // Iterate through the 6 indexes of the Array.
        for (int i = 0; i < 6; i++) {
            // Add a new element and increment the length as well
            array[length++] = i;
        }

        // Print out the results of searching for 4 and 30.
        System.out.println("Does the array contain the element 4? - " + ArraySearch.linearSearch(array, length, 4));
        System.out.println("Does the array contain the element 30? - " + ArraySearch.linearSearch(array, length, 30));

        // Does the array contain the element 4? - true
        // Does the array contain the element 30? - false
    }

    public static boolean linearSearch(int[] array, int length, int element) {
        // Check for edge cases
        if (array == null || length == 0) {
            return false;
        }

        // Check each element starting from the first one
        for (int i = 0; i < length; i++) {
            // We found the element at index i, so return true.
            if (array[i] == element) {
                return true;
            }
        }

        // We didn't find the element in the array.
        return false;
    }
}

Go

package main

import "fmt"

func main() {
    // Declare a new array of 6 elements
    array := make([]int, 6)

    // Variable to keep track of the length of the array
    var length int = 0

    // Iterate through the 6 indexes of the Array.
    for i := 0; i < 6; i++ {
        // Add a new element and increment the length as well
        array[length] = i
        length++
    }

    // Print out the results of searching for 4 and 30.
    fmt.Println("Does the array contain the element 4? - ", linearSearch(array, length, 4))
    fmt.Println("Does the array contain the element 30? - ", linearSearch(array, length, 30))

    // Does the array contain the element 4? - true
    // Does the array contain the element 30? - false
}

func linearSearch(array []int, length, element int) bool {
    // Check for edge cases. Is the array null or empty?
    // If it is, then we return false because the element we're
    // searching for couldn't possibly be in it.
    if array == nil || length == 0 {
        return false
    }

    // Carry out the linear search by checking each element,
    // starting from the first one.
    for i := 0; i < length; i++ {
        // We found the element at index i.
        // So we return true to say it exists.
        if array[i] == element {
            return true
        }
    }

    // We didn't fine the element in the array
    return false
}

https://go.dev/play/p/6UF0z9JxPXb

Different
Why?
If we declare like below, We need to change function with argument and if conditions.
Because argument array []int in linearSearch does not allow array.
Only Slice can be used.
If you want to use array as argument you should change like below.
Also array doesn't have nil value due to initializing with 0 also in If conditions with array argument.

var array [6]int    //[0,0,0,0,0,0]

func linearSearch(array [6]int, length, element int) bool{
if array == nil || length == 0 { // Cannot convert nil (untyped nil value) to [6]int
        return false
    }

Result

Does the array contain the element 4? -  true
Does the array contain the element 30? -  false
728x90
반응형

관련글 더보기

댓글 영역