From 0d1d961237a4d0d126a44ccc842f75226c8d7895 Mon Sep 17 00:00:00 2001 From: MalavikaN1 Date: Sat, 8 Oct 2022 21:02:45 +0530 Subject: [PATCH 1/2] Commited Selection Sort algorithm in js --- .js/SelectionSort.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .js/SelectionSort.js diff --git a/.js/SelectionSort.js b/.js/SelectionSort.js new file mode 100644 index 0000000..2c1331e --- /dev/null +++ b/.js/SelectionSort.js @@ -0,0 +1,19 @@ +function SelectionSort(input) { + for (var i = 0; i < input.length; i++) { + var temp = input[i]; + for (var j = i + 1; j < input.length; j++) { + if (temp > input[j]) { + temp = input[j]; + } + } + var index = input.indexOf(temp); + var tempVal = input[i]; + input[i] = temp; + input[index] = tempVal; + } +} + +var A=[10,45,3,6,19,-1,55,0]; +console.log("Array before sorting is:",A); +SelectionSort(A); +console.log("Array after sorting using Selection Sort is:",A); \ No newline at end of file From 39a1fe36b56d0c3f28a269123e2316a42ad381c6 Mon Sep 17 00:00:00 2001 From: MalavikaN1 Date: Sat, 8 Oct 2022 21:11:57 +0530 Subject: [PATCH 2/2] Insertion Sort in JS --- .js/InsertionSort.js | 18 ++++++++++++++++++ .js/LinearSearch.js | 0 .js/QuickSort.js | 0 3 files changed, 18 insertions(+) create mode 100644 .js/InsertionSort.js create mode 100644 .js/LinearSearch.js create mode 100644 .js/QuickSort.js diff --git a/.js/InsertionSort.js b/.js/InsertionSort.js new file mode 100644 index 0000000..9b98dff --- /dev/null +++ b/.js/InsertionSort.js @@ -0,0 +1,18 @@ +function insertionSort(inputArr) { + let n = inputArr.length; + for (let i = 1; i < n; i++) { + let current = inputArr[i]; + let j = i-1; + while ((j > -1) && (current < inputArr[j])) { + inputArr[j+1] = inputArr[j]; + j--; + } + inputArr[j+1] = current; + } + return inputArr; +} + +var A=[10,45,3,6,19,-1,55,0]; +console.log("Array before sorting is:",A); +insertionSort(A); +console.log("Array after sorting using Insertion Sort is:",A); \ No newline at end of file diff --git a/.js/LinearSearch.js b/.js/LinearSearch.js new file mode 100644 index 0000000..e69de29 diff --git a/.js/QuickSort.js b/.js/QuickSort.js new file mode 100644 index 0000000..e69de29