function sort(target, type = 'bubble') { if (!Array.isArray(target)) return target; let len = target.length; if (type === 'bubble') { for (let i = len; i > 0; i--) { for (let j = 0; j <= i; j++ ) { let v1 = target[j]; let v2 = target[j + 1]; if (v1 > v2) { target[j] = v2; target[j + 1] = v1; } } } } if (type === 'select') { for (let i = 0; i < len - 1; i++) { for (let j = i; j <= len; j++ ) { let v1 = target[i]; let v2 = target[j + 1]; if (v1 > v2) { target[i] = v2; target[j + 1] = v1; } } } } if (type === 'insert') { for (let i = 1; i < len; i++) { for (let j = i; j > 0; j-- ) { let v1 = target[j]; // 4 let v2 = target[j - 1]; // 5 if (v1 < v2) { target[j] = v2; target[j - 1] = v1; } else { break; } } } } return target;}sort([5,4,3,2,1], 'insert');