Very Easy 100% Easiest Logic Ever (Fully Explained Step by Step)(C++, JavaScript, Java)
Intuition\nIf you wanna to solve this problem then you have a good grip on Recusrion because you have to solve this problem in O(nlogn) which is only possible b
# Intuition\nIf you wanna to solve this problem then you have a good grip on Recusrion because you have to solve this problem in O(nlogn) which is only possible by using Merge sort or Quick Sort. but here in my solution I am using Merge Sort Algorithm\n\n# Approach\nHere\'s how the algorithm works step by step:\n\n**Divide the array in half**. The first step in this code is to divide the input array into two halves. This is done by finding the middle index of the array and creating two new arrays: one containing the elements from the start of the array up to the middle index, and one containing the elements from the middle index to the end of the array.\n\n**Recursively sort each half.** Once we have divided the input array into two halves, we recursively sort each half using the same algorithm. We repeat this process until each half contains only one element, at which point we consider them to be sorted.\n\n**Merge the sorted halves.** After both halves are sorted, we merge them back together into a single, sorted array. We do this by comparing the first element of each half and adding the smaller element to a new array. We then move the index of the array from which we took the smaller element by one and repeat the process until we have added all elements from both halves to the new array.\n\nReturn the sorted array. Finally, we return the merged array.\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code by using Merge Sort Algorithm\n```\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar sortArray = function(nums) {\n    if(nums.length < 2) return nums\n    let mid = Math.floor(nums.length/2);\n    let left = nums.slice(0, mid);\n    let right = nums.slice(mid);\n    return merge(sortArray(left), sortArray(right));\n};\n\nvar merge = function(left, right) {\n    let sortedArray = [];\n    while(left.length && right.length){\n        if(left[0] <= right[0]){\n            sortedArray.push(left.shift())\n        }else {\n            sortedArray.push(right.shift())\n        }\n    }\n\n    return [...sortedArray, ...left, ...right]\n}\n```\n# Code by using Binary Sort Algorithm\n```\n// Time Complexity Big-O = O(n^2)\n\nvar sortArray = function (nums) {\n  let sorted;\n  do {\n    sorted = false;\n    for (let i = 0; i <= nums.length; i++) {\n      if (nums[i] > nums[i + 1]) {\n        let temp = nums[i + 1];\n        nums[i + 1] = nums[i];\n        nums[i] = temp;\n        sorted = true;\n      }\n    }\n  } while (sorted);\n  return nums;\n};\n```\n\nI am working hard for you guys...\nPlease upvote if you found any help with this code...
['C++', 'Java', 'JavaScript']