Sort an integer array, keeping first in place in JavaScript


In the given problem statement we have to sort the given integer array but we have to keep the first item in place and sort the remaining items with the help of Javascript functionalities. So we will use some predefined functions of Javascript to solve the problem.

Understanding the Problem

The problem at hand is to get the sorted array of integer numbers in Javascript and the main operation in this task is to keep the first item intact in its original position. Or we can say we have to rearrange the items in the array so that they are visible in ascending order but for the first item which should be at the first index of the array. For example: suppose we have an array as [4, 7, 8, 5, 6, 1, 3] so after sorting the array item while keeping the first items as it is the outcome will be: [4, 1, 3, 5, 6, 7, 8].

So our aim is to produce an algorithm to do the operation as above.

Logic for the given Problem

To sort an array of integers in Javascript but keeping the first item at the same place we can use a combination of array slicing and sorting. So to solve the above problem we will use a straightforward approach. At the first step we will save the value of the first item of the array in a separate variable to secure that element. And then we will sort the remaining items of the integer array with the help of standard sorting predefined methods of Javascript such as sort method.

And finally we will combine the first item and the remaining sorted items back into the array. So using this process we can achieve the desired outcome of sorting the integer array while keeping the first item as it is.

Algorithm

Step 1: As we have to provide a solution for sorting the array items by keeping the first item as it is. So we will declare a function called sortKeepingFirstInPlace and the function will accept a parameter of integer array as input.

Step 2: As we know we have to preserve the first item in the first place. So for doing this task we will store the first item in a separate variable. This variable will be used later to combine the remaining items.

Step 3: Our task in this step is to sort the remaining items or the integer array with the help of sort method and inside the sort method we will use comparison function to sort the items in ascending order.

Step 4: Now we have the first item and sorted items of the array. In this step we will insert the first item at the beginning of the sorted items of the array and return to show the result.

Example

function sortKeepingFirstInPlace(arr) {
   // Store the 1st element
   const firstElement = arr[0];

   // Sort the remaining elements
   const sortedArray = arr.slice(1).sort((a, b) => a - b);

   // Insert the 1st element at the beginning
   sortedArray.unshift(firstElement);

   return sortedArray;
}

const array = [30, 10, 40, 20, 50];
const sortedArr = sortKeepingFirstInPlace(array);
console.log(sortedArr);

Output

[ 30, 10, 20, 40, 50 ]

Complexity

We have used some predefined methods of Javascript in our code like slice, sort and unshift. So this method has time complexities of O(1), O(n log n) and O(n) respectively. So the approximated time complexity for the code is O(n log n), in which n is the size of the input array. While the space complexity of the code is O(n) as we are utilizing the memory to store the array items and the size of the array is n.

Conclusion

The code has successfully solved the given problem with the time complexity of O(n log n). The main task of this problem was sorting the items while keeping the first item intact. So we have also used some built in methods of Javascript to get the result.

Updated on: 16-Aug-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements