{"ScriptPreparationCode":"var test = [1,10,5,4,8,3,2,11,9,7,6,0];\r\nfunction quickSortA(array){\r\nif(array.length \u003C 2){\r\n return array;\r\n}\r\nvar last = array.length-1;\r\nvar pivot = array[last];\r\nvar left = [], right = [], mid = [];\r\n\r\nfor(var i=0; i\u003Carray.length; i\u002B\u002B){\r\n if(array[i] \u003C pivot){\r\n left.push(array[i]);\r\n }else if(array[i] \u003E pivot){\r\n right.push(array[i]);\r\n }else{\r\n mid.push(array[i]);\r\n }\r\n}\r\nreturn partition(left,mid,right);\r\n\r\n}\r\n\r\nfunction partition(left,mid,right){\r\n left = quickSortA(left);\r\n right = quickSortA(right);\r\n return left.concat(mid).concat(right);\r\n}\r\n\r\nfunction quickSortB(array){\r\n if(array.length \u003C 2){\r\n return array;\r\n }\r\n var pivot = array[Math.floor(Math.random()*array.length)]\r\n var left = [], right = [], mid = [];\r\n \r\n for(var i=0; i\u003Carray.length; i\u002B\u002B){\r\n if(array[i] \u003C pivot){\r\n left.push(array[i]);\r\n }else if(array[i] \u003E pivot){\r\n right.push(array[i]);\r\n }else{\r\n mid.push(array[i]);\r\n }\r\n }\r\n return quickSortB(left).concat(mid).concat(quickSortB(right));\r\n}\r\n\r\nfunction stableSort(v, f)\r\n{\r\n if (f === undefined) {\r\n f = function(a, b) {\r\n a = \u0022\u0022\u002Ba; b = \u0022\u0022\u002Bb;\r\n return a \u003C b ? -1 : (a \u003E b ? 1 : 0);\r\n }\r\n }\r\n var dv = [];\r\n for (var i=0; i\u003Cv.length; i\u002B\u002B) {\r\n dv[i] = [v[i], i];\r\n }\r\n dv.sort(function(a, b){\r\n return f(a[0], b[0]) || (a[1] - b[1]);\r\n });\r\n for (var i=0; i\u003Cv.length; i\u002B\u002B) {\r\n v[i] = dv[i][0];\r\n }\r\n}\r\n\r\n\r\nfunction quickSortC(arr, left, right)\r\n{\r\n\tvar i = left;\r\n\tvar j = right;\r\n\tvar tmp;\r\n\tpivotidx = (left \u002B right) / 2; \r\n\tvar pivot = parseInt(arr[pivotidx.toFixed()]); \r\n\t/* partition */\r\n\twhile (i \u003C= j)\r\n\t{\r\n\t\twhile (parseInt(arr[i]) \u003C pivot)\r\n\t\ti\u002B\u002B;\r\n\t\twhile (parseInt(arr[j]) \u003E pivot)\r\n\t\t\tj--;\r\n\t\tif (i \u003C= j)\r\n\t\t{\r\n\t\t\ttmp = arr[i];\r\n\t\t\tarr[i] = arr[j];\r\n\t\t\tarr[j] = tmp;\r\n\t\t\ti\u002B\u002B;\r\n\t\t\tj--;\r\n\t\t}\r\n\t}\r\n\r\n\t/* recursion */\r\n\tif (left \u003C j)\r\n\t\tquickSort(arr, left, j);\r\n\tif (i \u003C right)\r\n\t\tquickSort(arr, i, right);\r\n\treturn arr;\r\n}\r\n\r\n\r\nfunction quickSortD(t){\r\n _quickSort(t,0,t.length-1,0,t.length-1);\r\n}\r\n\r\nfunction _quickSort(t, s, e, sp, ep){ \r\n if( s\u003E=e ) return;\r\n while( sp\u003Cep \u0026\u0026 t[sp]\u003Ct[e] ) sp\u002B\u002B; \r\n if( sp==e )\r\n _quickSort(t,s,e-1,s,e-1); \r\n else{\r\n while(t[ep]\u003E=t[e] \u0026\u0026 sp\u003Cep ) ep--; \r\n if( sp==ep ){\r\n var temp = t[sp];\r\n t[sp] = t[e];\r\n t[e] = temp;\r\n if( s!=sp ){\r\n _quickSort(t,s,sp-1,s,sp-1);\r\n }\r\n _quickSort(t,sp\u002B1,e,sp\u002B1,e); \r\n }else{\r\n var temp = t[sp];\r\n t[sp] = t[ep];\r\n t[ep] = temp;\r\n _quickSort(t,s,e,sp\u002B1,ep);\r\n }\r\n }\r\n}","TestCases":[{"Name":"Quick Sort A","Code":"quickSortA(test);","IsDeferred":false},{"Name":"Quick Sort B","Code":"quickSortB(test);","IsDeferred":false},{"Name":"Stable Sort","Code":"stableSort(test);","IsDeferred":false},{"Name":"Quick Sort C","Code":"quickSortC(test);","IsDeferred":false},{"Name":"Quick Sort D","Code":"quickSortD(test);","IsDeferred":false}]}