Sorting odd and even elements separately JavaScript


In our problem statement we have to sort odd and even elements separately with the help of Javascript functionalities. So for doing this task we will use for loops and bubble sort for sorting odd and even numbers separately.

Understanding the problem statement

The problem statement is to write a Javascript function that will help to sort odd and even numbers in a given array. For example if we have an array [2, 3, 5, 4] then we will first sort even indexed elements [3, 4] and then sort the odd indexed elements in the array [2, 5]. After sorting these two subarrays we will merge these two arrays to get the resultant sorted array.

Logic for the given problem

To sort the elements we will basically use bubble sort to sort even indexed and odd indexed elements separately. So in every iteration of the loop we will set swapped values to false. If we make a swap then set it to true. Then we will loop through the odd indexed array and do the same procedure and set swap to true if we have swapped the elements if the first element is greater than the second element. At the end we sorted the array.

Algorithm

Step 1 − Declare a function called sortOddEven which is using a parameter of array of length n.

Step 2 − Use bubble sort algorithm to sort the even indexed and odd indexed elements separately.

Step 3 − Loop through the elements of the array. Then set swapped to false.

Step 4 − Loop even indexed elements and compare every pair of even indexed elements and swap them if they are in the wrong order. If we have swapped the items then set swapped to true.

Step 5 − Then loop odd indexed elements and compare every pair of odd numbers starting index from 1 and swap them if they are in the incorrect order. If we have swapped the items then set the swapped value to true.

Step 6 − Repeat the step four and five until no swaps required in the iteration.

Step 7 − Return the final result array which contains all sorted elements of the input array.

Code for the algorithm

//function to get sorted elements of the array
function sortOddEven(arr) {
   const n = arr.length;
   let swapped;
   do {
      swapped = false;
      // sort even elements
      for (let i = 0; i < n - 1; i += 2) {
         if (arr[i] > arr[i + 2]) {
            [arr[i], arr[i + 2]] = [arr[i + 2], arr[i]];
            swapped = true;
         }
      }
      // sort odd elements
      for (let i = 1; i < n - 1; i += 2) {
         if (arr[i] > arr[i + 2]) {
            [arr[i], arr[i + 2]] = [arr[i + 2], arr[i]];
            swapped = true;
         }
      }
   } while (swapped);
   return arr;
}
const arr = [9, 2, 7, 4, 5, 6, 3, 8, 1];
console.log(sortOddEven(arr));

Complexity

The time complexity of the bubble sort algorithm is O(n^2) in which n is the length of the array. But we are sorting the even indexed and odd indexed items separately so the actual number of comparisons and swaps is half the size of it, so the actual time complexity of the algorithm is equal to O(n).

Conclusion

In the above code we have implemented a solution to get the sorted array by sorting the even and odd indexed elements separately in Javascript. It is simple and easy to understand. And this algorithm is well suited for small sized arrays and not efficient for large arrays.

Updated on: 18-May-2023

574 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements