diff --git a/4.shellSort.md b/4.shellSort.md index 0406030..3650268 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -1,3 +1,5 @@ + +https://www.cnblogs.com/onepixel/articles/7674659.html # 希尔排序 希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本。但希尔排序是非稳定排序算法。 diff --git a/6.quickSort.md b/6.quickSort.md index 1c81c59..3390b80 100644 --- a/6.quickSort.md +++ b/6.quickSort.md @@ -87,9 +87,30 @@ function quickSort2(arr, low, high) { return arr; } -``` +var quickSort = function(arr) { +  if (arr.length <= 1) {//如果数组长度小于等于1无需判断直接返回即可 + return arr; + } +  var pivotIndex = Math.floor(arr.length / 2);//取基准点 +  var pivot = arr.splice(pivotIndex, 1)[0];//取基准点的值,splice(index,1)函数可以返回数组中被删除的那个数 +  var left = [];//存放比基准点小的数组 +  var right = [];//存放比基准点大的数组 +  for (var i = 0; i < arr.length; i++){ //遍历数组,进行判断分配 +    if (arr[i] < pivot) { +      left.push(arr[i]);//比基准点小的放在左边数组 +    } else { +      right.push(arr[i]);//比基准点大的放在右边数组 +    } +  } + //递归执行以上操作,对左右两个数组进行操作,直到数组长度为<=1; +  return quickSort(left).concat([pivot], quickSort(right)); +}; +
使用的时候,直接调用quickSort()就行了。 + +``` + ## 4. Python 代码实现 ```python