0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonで書いた探索・ソートアルゴリズムをjavascriptで書いてみる

Last updated at Posted at 2024-11-28

概念で理解していることをほかのプログラミング言語に適用し、
習得度を確認してみるトライ。

python3 での 2分探索

def binary_search(arr: list, target: int) -> bool:
    first = 0
    last = len(arr)
    while last >= first:
        mid = (first + last) // 2
        if target == arr[mid]:
            return True
        else:
            if target > arr[mid]:
                first = mid + 1
            else:
                last = mid - 1
    return False

javascriptによる 2分探索

こちらはOK

function binarySearch(Iterable, Target){
    let first = 0;
    let last = Iterable.length;

    while (last >= first){
        let mid = Math.floor((first + last) / 2);
        if (Target == Iterable[mid]){
            return "true";
        }else{
            if(Target > Iterable[mid]){
                first = mid + 1;
            }else{
                last = mid - 1;
            }
        }
    }
    return "false";
}

python のマージソート

def merge_sort(arr: list) -> list:
    if len(arr) > 1:
        mid = len(arr) // 2
        left = arr[:mid]
        right = arr[mid:]

        merge_sort(left)
        merge_sort(right)

        left_i, right_i, arr_i = 0, 0, 0

        while left_i < len(left) and right_i < len(right):
            if left[left_i] < right[right_i]:
                arr[arr_i] = left[left_i]
                left_i += 1
            else:
                arr[arr_i] = right[right_i]
                right_i += 1
            arr_i += 1
        
        while left_i < len(left):
            arr[arr_i] = left[left_i]
            left_i += 1
            arr_i += 1
        
        while right_i < len(right):
            arr[arr_i] = right[right_i]
            right_i += 1
            arr_i += 1
    
    return arr

javascriptのマージソート

javascriptのマージソートは、正しく動かない.

const mergeSort = (array) =>{
    if (array.length > 1){
        let mid = Math.floor((0 + array.length) / 2);
        let left = array.slice(0, mid);
        let right = array.slice(mid, array.length);

        mergeSort(left);
        mergeSort(right);

        let [i, j, k] = [0, 0, 0];
        while( i < left.length & j < right.length ){
            if (left[i] < right[j]){
                array[k] = left[i];
                i += 1;
            }else{
                array[k] = right[j];
                j += 1;
            }
            k += 1;
        }

        while ( i < left.length){
            array[k] = left[i];
            i += 1;
            k += 1;
        }

        while (j < right.length){
            array[j] = right[j];
            j += 1;
            k += 1;
        }
    }
    return array;
}


プリントデバッグでもしつつ、どうして意図した挙動になっていないかを確認する。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?