functionbubbleSort(array){// run time --> O(n^2)letlength=array.length;for(leti=0;i<length-1;i++){for(letj=0;j<length-1;j++){if(array[j]>array[j+1]){lettmp=array[j+1];array[j+1]=array[j];array[j]=tmp;}}}returnarray;}
Insertion Sort
functioninsertionSort(array){// run time --> O(n^2)letlength=array.length;for(leti=1;i<length;i++){lettmp=array[i];letcurrentIndex=i;while(currentIndex>0&&array[currentIndex-1]>tmp){array[currentIndex]=array[currentIndex-1];currentIndex--;}array[currentIndex]=tmp;}returnarray;}
Top comments (0)