A sorting technique that builds the sorted array one element at a time,
inserting each new element into its proper position among already-sorted
elements.
Time complexity is Quadratic, i.e. O(n^2)
Space complexity is Constant, i.e. O(1)
exportfunctioninsertionSort(numbersArray: number[]):void{letcurrentIndex: number;letcurrentKey: number;letpreviousIndex: number;for(currentIndex=1;currentIndex<numbersArray.length;currentIndex++){currentKey=numbersArray[currentIndex];previousIndex=currentIndex-1;// Move elements that are greater than currentKey one position ahead.
while(previousIndex>=0&&numbersArray[previousIndex]>currentKey){numbersArray[previousIndex+1]=numbersArray[previousIndex];previousIndex--;}numbersArray[previousIndex+1]=currentKey;}}