{"ScriptPreparationCode":"window.arr = [19, 12, 14, 37, 26, 23, 0, 1, 17, 22, 29, 2, 7, 20, 33, 9, 10, 21, 6, 32, 24, 15, 11, 8, 36, 28, 31, 38, 34, 16, 18, 13, 25, 27, 35, 4, 3, 30, 5, 39]\r\n\r\nwindow.bubbleSort = (arr) =\u003E {\r\n for(let i = 0; i \u003C arr.length; i\u002B\u002B){\r\n\r\n //Inner pass\r\n for(let j = 0; j \u003C arr.length - i - 1; j\u002B\u002B){\r\n\r\n //Value comparison using ascending order\r\n\r\n if(arr[j \u002B 1] \u003C arr[j]){\r\n\r\n //Swapping\r\n [arr[j \u002B 1],arr[j]] = [arr[j],arr[j \u002B 1]]\r\n }\r\n }\r\n };\r\n return arr;\r\n}\r\n\r\nwindow.insertionSort = (arr) =\u003E {\r\n for(let i = 1; i \u003C arr.length;i\u002B\u002B){\r\n\r\n //Go through the elements behind it.\r\n for(let j = i - 1; j \u003E -1; j--){\r\n \r\n //value comparison using ascending order.\r\n if(arr[j \u002B 1] \u003C arr[j]){\r\n\r\n //swap\r\n [arr[j\u002B1],arr[j]] = [arr[j],arr[j \u002B 1]];\r\n\r\n }\r\n }\r\n };\r\n\r\n return arr;\r\n}\r\n\r\nwindow.selectionSort = (arr) =\u003E {\r\n let min;\r\n\r\n //start passes.\r\n for (let i = 0; i \u003C arr.length; i\u002B\u002B) {\r\n //index of the smallest element to be the ith element.\r\n min = i;\r\n\r\n //Check through the rest of the array for a lesser element\r\n for (let j = i \u002B 1; j \u003C arr.length; j\u002B\u002B) {\r\n if (arr[j] \u003C arr[min]) {\r\n min = j;\r\n }\r\n }\r\n\r\n //compare the indexes\r\n if (min !== i) {\r\n //swap\r\n [arr[i], arr[min]] = [arr[min], arr[i]];\r\n }\r\n }\r\n\r\n return arr;\r\n}\r\n\r\n\r\nwindow.merge = (arr1, arr2) =\u003E {\r\n //make a new array and have two value pointers\r\n let res = [],\r\n i = 0,\r\n j = 0;\r\n\r\n //sorting the first array.\r\n if (arr1.length \u003E 1) {\r\n let min = 0;\r\n for (let i = 0; i \u003C arr1.length; i\u002B\u002B) {\r\n if (i !== min) {\r\n if (arr1[i] \u003C arr1[min]) {\r\n //also swap the elements\r\n [arr1[i], arr1[min]] = [arr1[min], arr1[i]];\r\n //change the minimum\r\n min = i;\r\n }\r\n }\r\n }\r\n }\r\n\r\n //sorting the second array.\r\n if (arr2.length \u003E 1) {\r\n let min = 0;\r\n for (let i = 0; i \u003C arr2.length; i\u002B\u002B) {\r\n if (i !== min) {\r\n if (arr2[i] \u003C arr2[min]) {\r\n //also swap the elements\r\n [arr2[i], arr2[min]] = [arr2[min], arr2[i]];\r\n //change the minimum\r\n min = i;\r\n }\r\n }\r\n }\r\n }\r\n\r\n //Value comparison.\r\n while (i \u003C arr1.length \u0026\u0026 j \u003C arr2.length) {\r\n if (arr1[i] \u003C arr2[j]) {\r\n res.push(arr1[i]);\r\n i\u002B\u002B;\r\n } else {\r\n res.push(arr2[j]);\r\n j\u002B\u002B;\r\n }\r\n }\r\n\r\n //pushing the rest of arr1.\r\n while (i \u003C arr1.length) {\r\n res.push(arr1[i]);\r\n i\u002B\u002B;\r\n }\r\n\r\n //pushing the rest of arr2.\r\n while (j \u003C arr2.length) {\r\n res.push(arr2[j]);\r\n j\u002B\u002B;\r\n }\r\n\r\n return res;\r\n}\r\n\r\n//merge sort\r\nwindow.mergeSort = (arr) =\u003E {\r\n //Best case\r\n if (arr.length \u003C= 1) return arr;\r\n\r\n //splitting into halves\r\n let mid = Math.ceil(arr.length / 2);\r\n\r\n let arr1 = arr.slice(0, mid);\r\n\r\n let arr2 = arr.slice(mid);\r\n\r\n let arr1_subarrays = [],\r\n sorted_arr1_subarrays = [];\r\n\r\n let arr2_subarrays = [],\r\n sorted_arr2_subarrays = [];\r\n\r\n //loop through array 1 making subarrays of two elements\r\n for (let i = 0; i \u003C arr1.length; i \u002B= 2) {\r\n arr1_subarrays.push(arr1.slice(i, i \u002B 2));\r\n }\r\n\r\n //loop through array 2 making subarrays of two elements.\r\n for (let i = 0; i \u003C arr2.length; i \u002B= 2) {\r\n arr2_subarrays.push(arr2.slice(i, i \u002B 2));\r\n }\r\n\r\n // sorting each subarray of arr1.\r\n for (let i = 0; i \u003C arr1_subarrays.length; i \u002B= 2) {\r\n let result = merge(arr1_subarrays[i], arr1_subarrays[i \u002B 1]);\r\n result.forEach((value) =\u003E sorted_arr1_subarrays.push(value));\r\n }\r\n\r\n // sorting each subarray of arr2.\r\n for (let i = 0; i \u003C arr2_subarrays.length; i \u002B= 2) {\r\n let result = merge(arr2_subarrays[i], arr2_subarrays[i \u002B 1]);\r\n result.forEach((value) =\u003E sorted_arr2_subarrays.push(value));\r\n }\r\n\r\n let result = merge(sorted_arr1_subarrays, sorted_arr2_subarrays);\r\n\r\n return result;\r\n}\r\n\r\nwindow.partition = (items, left, right) =\u003E {\r\n //rem that left and right are pointers.\r\n\r\n let pivot = items[Math.floor((right \u002B left) / 2)],\r\n i = left, //left pointer\r\n j = right; //right pointer\r\n\r\n while (i \u003C= j) {\r\n //increment left pointer if the value is less than the pivot\r\n while (items[i] \u003C pivot) {\r\n i\u002B\u002B;\r\n }\r\n\r\n //decrement right pointer if the value is more than the pivot\r\n while (items[j] \u003E pivot) {\r\n j--;\r\n }\r\n\r\n //else we swap.\r\n if (i \u003C= j) {\r\n [items[i], items[j]] = [items[j], items[i]];\r\n i\u002B\u002B;\r\n j--;\r\n }\r\n }\r\n\r\n //return the left pointer\r\n return i;\r\n}\r\n\r\nwindow.quickSort = (items, left, right) =\u003E {\r\n let index;\r\n\r\n if (items.length \u003E 1) {\r\n index = partition(items, left, right); //get the left pointer returned\r\n\r\n if (left \u003C index - 1) {\r\n //more elements on the left side\r\n quickSort(items, left, index - 1);\r\n }\r\n\r\n if (index \u003C right) {\r\n //more elements on the right side\r\n quickSort(items, index, right);\r\n }\r\n }\r\n\r\n return items;\r\n}\r\n\r\n","TestCases":[{"Name":"Bubble Sort","Code":"window.bubbleSort(window.arr);","IsDeferred":false},{"Name":"Insertion Sort","Code":"window.insertionSort(window.arr)","IsDeferred":false},{"Name":"Selection Sort","Code":"window.selectionSort(window.arr)","IsDeferred":false},{"Name":"Merge Sort","Code":"window.mergeSort(window.arr)","IsDeferred":false},{"Name":"Quick Sort","Code":"window.quickSort(window.arr, 0, window.arr.length - 1)","IsDeferred":false}]}