'An array of 5 string is given where each string contains 2 characters, Now you have to sort these strings using insertion sort, like in a dictionary

An array of 5 string is given where each string contains 2 characters, Now you have to sort these strings using insertion sort, like in a dictionary.

Input Input contains 5 strings of length 2 separated by spaces. String contains only uppercase English letters.

Output Print the sorted array.



Solution 1:[1]

function easySorting(arr)
  {
    let n = arr.length;
        for (let i = 1; i < n; i++) {
            // Choosing the first element in our unsorted subarray
            let current = arr[i];
            // The last element of our sorted subarray
            let j = i-1; 
            while ((j > -1) && (current < arr[j])) {
                arr[j+1] = arr[j];
                j--;
            }
            arr[j+1] = current;
        }
    return arr;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anoop tiwari