close
close

first Drop

Com TW NOw News 2024

Principles of Algorithms in TypeScript: Implementations and Applications
news

Principles of Algorithms in TypeScript: Implementations and Applications

Image description

In this article, we will explore the most important algorithms and how to implement TypeScript. These algorithms are increasingly used to solve problems in areas such as ordering, busca, graphos, and programs. Implementing them will allow you to use more features and applications in different contexts. Discover more of these algorithms:

1. Order algorithms

Bubble Sorting

O Bubble Sorting There is a simple algorithm that functions repeated trocando elements next to each other, which is an error in the order. If you find it easy to undertake, it is not so efficient to make large connections.

function bubbleSort(arr: number()): number() {
    let n = arr.length;
    for (let i = 0; i  n - 1; i++) {
        for (let j = 0; j  n - i - 1; j++) {
            if (arr(j) > arr(j + 1)) {
                (arr(j), arr(j + 1)) = (arr(j + 1), arr(j)); // Troca de elementos
            }
        }
    }
    return arr;
}
Go to full screen mode

Exit full screen

Merge Sort

O Merge Sort There is an efficient algorithm to continue the division and capture prince. You can divide the sequence in multiple ways, like this, and then combine them to form the final sequence.

function mergeSort(arr: number()): number() {
    if (arr.length  1) return arr;
    const mid = Math.floor(arr.length / 2);
    const left = mergeSort(arr.slice(0, mid));
    const right = mergeSort(arr.slice(mid));
    return merge(left, right);
}

function merge(left: number(), right: number()): number() {
    let result = ();
    while (left.length && right.length) {
        result.push(left(0)  right(0) ? left.shift()! : right.shift()!);
    }
    return result.concat(left, right);
}
Go to full screen mode

Exit full screen

Quick sort

O Quick sort it is based on a division and conquest, but it is an element that can be used and participates in the array of subarrays that are rearranged over and over again.

function quickSort(arr: number()): number() {
    if (arr.length  1) return arr;
    const pivot = arr(arr.length - 1);
    const left = arr.filter(el => el  pivot);
    const right = arr.filter(el => el > pivot);
    return (...quickSort(left), pivot, ...quickSort(right));
}
Go to full screen mode

Exit full screen

Insert Sorting

O Insert Sorting By inserting the elementary elements into the array, you can place certain elements in the correct position, or order the array by a certain length.

function insertionSort(arr: number()): number() {
    for (let i = 1; i  arr.length; i++) {
        let key = arr(i);
        let j = i - 1;
        while (j >= 0 && arr(j) > key) {
            arr(j + 1) = arr(j);
            j--;
        }
        arr(j + 1) = key;
    }
    return arr;
}
Go to full screen mode

Exit full screen

2. Search algorithms

Linear search

A Linear search It’s one of the simplest ways to work, but all the elements of a list can hold a person’s courage back.

function linearSearch(arr: number(), target: number): number {
    for (let i = 0; i  arr.length; i++) {
        if (arr(i) === target) return i;
    }
    return -1;
}
Go to full screen mode

Exit full screen

Binary Search

A Binary Search The algorithm is more efficient when it operates on the ordered list, so that a list with increasingly more repetitions encounters one of the following elements.

function binarySearch(arr: number(), target: number): number {
    let left = 0, right = arr.length - 1;
    while (left  right) {
        const mid = Math.floor((left + right) / 2);
        if (arr(mid) === target) return mid;
        if (arr(mid)  target) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}
Go to full screen mode

Exit full screen

3. Graph Algorithms

Dijkstra

O Dijkstra’s algorithm If you want to know more about any of the many other aspects of a graph, use an investigation of one of the smallest distances.

Search in Largura (BFS)

A Search in Largura (BFS) If you see all kinds of a grave photo through a camada, you can use it to not think about your caminho mais curto in grafos.

Search for Depth (DFS)

A Search for Depth (DFS) Exploring the maximum possibilities for a longer period of the consequences for the retrocedar, can be used for exploring graphos and detection of cyclos.

4. Dynamic program

Fibonacci

An order of Fibonacci You can calculate efficiently using programs that produce intermediate results to remove the recálculos.

function fibonacci(n: number): number {
    let fib = (0, 1);
    for (let i = 2; i  n; i++) {
        fib(i) = fib(i - 1) + fib(i - 2);
    }
    return fib(n);
}
Go to full screen mode

Exit full screen

Problema da Mochila (Knapsack Problem)

O Mochila’s Problem There is a classic problem with solving problems that can solve using programs to achieve the maximum value possible, given that there is a combination of things and values.

5. Other common algorithms

Kadane algorithm

O Kadane algorithm It is used to find a subarray as large as a die, thus solving the problem of maximum subarray at a linear rate.

Prim algorithm

O Prim algorithm If you have a small restriction in a grave, you can connect all possible connections together.

Kruskal algorithm

Semelhante and Prim’s algorithm, o Kruskal algorithm if you have a minimum age, you can lay a foundation on conjunctions and ordering your arestas.

Conclusion

These algorithms are fundamental to solve a varied problem with computers. Implementing TypeScript is an excellent way to work with your programs, including the pace that you understand as an international function. If you want to organize, both graphos and optimizations, it is so that algorithms play a crucial role in solving complex forma-efficient problems.

To facilitate the use of algorithms, a librarian is needed

(!(NPM version)(https://img.shields.io/npm/v/algoritims.svg?style=flat-square))(https://www.npmjs.com/package/algoritims) (!(Build Status)(https://img.shields.io/github/actions/workflow/status/usuario/algoritims/teste.yml?branch=main&style=flat-square))(h. Latest version: 1.0. 5, last published: 3 days ago. Start using algorithms in your project by running `npm i algorithms` . There are no other projects in the npm registry that use algorithms.

favicon
npmjs.com