DEV Community

Cover image for BubbleSort en Typescript
Ger Moren
Ger Moren

Posted on

4 5

BubbleSort en Typescript

BubbleSort in Javascript

NAMESPACES

Se crea el namespace MathUtils con los distintos types auxiliares para el método BubbleSort:

Num, Length, Push NTuple and Add types

En el caso de la resta importa el orden en que se reciben los paramentos, por este motivo el primer parámetro debe ser mayor o igual que el segundo:
Substract type

Si queremos comparar 2 números en Typescript, los símbolos "<" y
">" no son validos
type isMinor = 1 > 2 ? true : false;
por este motivo vamos a crear el type GreaterComparison
Type GreatComparison

BUBBLE SORT
Ayudándonos del namespace MathUtils definido anteriormente procedemos a definir el type BubbleSort el cual recibe un array como primer parámetro y una variable auxiliar "actual".
Si el array tiene un solo elemento se retorna el mismo array.

Caso contrario extendemos un array en el que se infieren 3 elementos, los números a comparar y lo que queda del resto.

Se llama recursivamente al type BubbleSort.

Bubble Sort Method

type example1 = BubbleSort<[234, 43, 55, 63, 5, 6, 235, 547]>;
// [ 5, 6, 43, 55, 63, 234, 235, 547]
type example2 = BubbleSort<[5, 2, 4, 6, 1, 3]>;
// [1, 2, 3, 4, 5, 6]
type example3 = BubbleSort<[1]>;
// [1]
type example4 = BubbleSort<[]>;
// []
Enter fullscreen mode Exit fullscreen mode

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay