912. Sort an Array (Sorting Algorithms)
Few Sorting Algorithms:\n1. Selection Sort \n1. Bubble Sort\n1. Insertion Sort\n1. QuickSort\n1. Merge Sort\n\nSelection Sort - O(n^2)\n\n\nvar selectionSort = 
**Few Sorting Algorithms**:\n1. Selection Sort \n1. Bubble Sort\n1. Insertion Sort\n1. QuickSort\n1. Merge Sort\n\n**Selection Sort - O(n^2)**\n\n```\nvar selectionSort = function(nums) {\n    var n = nums.length;\n    for (var i = 0; i <n-1; i++) {\n        var min = i;\n        for (var j = i+1; j < n; j++ ) {\n            if (nums[j] < nums[min]) {\n                min = j;    \n            }\n        }\n        var temp = nums[i];\n        nums[i] = nums[min];\n        nums[min] = temp;\n    }\n    return nums;\n}\n```\n**Bubble Sort** -  **Best Case : O (n)**, **Average Case : O (n^2)**,**Worst Case : O (n^ 2)**\n```\nvar bubbleSort = function(nums) {\n    for (var k = nums.length - 1; k >= 1; k--) {\n            for (var i = 0; i < k; i++) {\n                if (nums[i] > nums[i + 1]) {\n                    swap(i, i + 1);\n                } \n            }\n        }\n    function swap(i,j) {\n        var temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n    return nums;\n}\n```\n**Insertion Sort**-**Best Case : O (n)**, **Average Case : O (n^2)**,**Worst Case : O (n^ 2)**\n\n```\nvar insertionSort = function(nums) {\n    for (var i = 1; i < nums.length; i++) {\n        var value = nums[i];\n        var hole = i;\n        while(hole > 0 && nums[hole-1] > value) {\n            nums[hole] = nums[hole-1];\n            hole = hole-1\n        }\n         nums[hole] = value;\n    }\n    return nums;\n}\n```\n\n**Merge Sort** -  **Best Case : O(n log n)**, **Average Case :O(n log n)**, **Worst Case : O(n log n)**\n\n```\nvar mergeSort = function(nums) {\n    if (nums.length < 2) return nums;\n    var mid   = Math.floor(nums.length/2);\n    var left  = nums.slice(0,mid);\n    var right = nums.slice(mid);\n    \n    function merge(left,right) {\n        var result = [],lLen = left.length, rLen = right.length, l = 0, r = 0;\n        while(l < lLen && r < rLen){\n            if(left[l] < right[r]){\n                result.push(left[l++]);\n            }\n            else{\n                result.push(right[r++]);\n            }\n        }  \n        return result.concat(left.slice(l)).concat(right.slice(r));\n    }\n\n   return merge(mergeSort(left),mergeSort(right));\n}\n```\n\n**Quick Sort** -  **Best Case : O(n log n)**, **Average Case :O(n log n)**, **Worst Case : O(n^2)**\n```\nvar quickSort = function(nums) {\n    \n    function quickSortHelper(nums, start, end) {\n        if (start >= end) return nums\n    \n        var pivotValue = nums[start]\n        var smaller = start\n        for (var i = start + 1; i <= end; i++) {\n            var bigger = i\n            if (nums[bigger] < pivotValue) {\n                smaller++\n                var smallerValue = nums[bigger]\n                nums[bigger] = nums[smaller]\n                nums[smaller] = smallerValue\n            }\n        }\n        var smallerCache = nums[smaller]\n        nums[smaller] = nums[start]\n        nums[start] = smallerCache\n        \n        quickSortHelper(nums, start, smaller - 1)\n        quickSortHelper(nums, smaller + 1, end)\n        return nums\n    }\n    \n    return quickSortHelper(nums, 0, nums.length - 1)\n};\n```\n\nBut we use real time\n```\nvar sortArray = function(nums) {\n    return nums.sort((a,b)=>a-b);\n};\n```
['JavaScript']