From 190b17b8d81eb29e95dcef8dfbf86d61f9f9c052 Mon Sep 17 00:00:00 2001 From: thakurutkarsh22 Date: Sat, 12 Oct 2019 17:08:12 +0530 Subject: [PATCH] AddingSelectionSortInJavaScript --- Javascript/selectionSort.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Javascript/selectionSort.js diff --git a/Javascript/selectionSort.js b/Javascript/selectionSort.js new file mode 100644 index 0000000..4397c43 --- /dev/null +++ b/Javascript/selectionSort.js @@ -0,0 +1,20 @@ +function selectionSort(A) { + var len = array_length(A); + for (var i = 0; i < len - 1; i = i + 1) { + var j_min = i; + for (var j = i + 1; j < len; j = j + 1) { + if (A[j] < A[j_min]) { + j_min = j; + } else {} + } + if (j_min !== i) { + swap(A, i, j_min); + } else {} + } +} + +function swap(A, x, y) { + var temp = A[x]; + A[x] = A[y]; + A[y] = temp; +} \ No newline at end of file