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 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