js实现shell排序

//shell排序配插入排序
function
shell_insert_sort(arr){ var gap = arr.length; do{ gap = parseInt(gap/3) + 1; console.log(gap); for(var i = gap; i < arr.length; i+= gap){ var tmp = arr[i]; for( var j = i - gap; j >= 0 && tmp < arr[j]; j -= gap){ arr[j + gap] = arr[j]; } arr[j+gap] = tmp; } }while(gap > 1); } //shell排序配交换排序 function shell_bubble_sort(arr){ gap = arr.length; var flag = true; do{ gap = parseInt(gap/3) + 1; flag = true; for(var i = 0; i < arr.length && flag ; i += gap){ flag = false; console.log(arr) for(var j = 0; j < arr.length - i - gap; j += gap){ if(arr[j] > arr[j + gap]){ swap(arr, j, j + gap); flag = true; } } } }while(gap > 1); } function swap(arr, i, j){ var tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } var arr = [1, 99, 23, 88, 56, 77, 3]; shell_bubble_sort(arr); //shell_insert_sort(arr) console.log(arr);

 

你可能感兴趣的:(js实现shell排序)