From eafe1e22fc2564c4927a4b88d0ef8cdd8a9df15d Mon Sep 17 00:00:00 2001 From: yigencong Date: Thu, 22 Feb 2024 23:04:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sort/insertion-sort.mjs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 sort/insertion-sort.mjs diff --git a/sort/insertion-sort.mjs b/sort/insertion-sort.mjs new file mode 100644 index 0000000..ab9f233 --- /dev/null +++ b/sort/insertion-sort.mjs @@ -0,0 +1,19 @@ +import { generateRandomArray, isSort } from "../util/index.mjs"; + +export function insertionSort(arr){ + let n = arr.length; + for (let i = 1; i=0 && arr[j] > currentElement) { + arr[j+1] = arr[j] + j-- + } + arr[j+1] = currentElement + } +} + +let arr = generateRandomArray(1000) + +insertionSort(arr) +console.log(isSort(arr)); \ No newline at end of file