chapter_sorting/quick_sort/ #47
Replies: 47 comments 94 replies
-
js版本错的有点严重啊 |
Beta Was this translation helpful? Give feedback.
-
此部分的尾递归优化能否有图说明呢,看文字简短描述实在是想象不出来 |
Beta Was this translation helpful? Give feedback.
-
为什么里面的循环还需要i<j |
Beta Was this translation helpful? Give feedback.
-
图的文章啥时候出来~等了一个月啦 K神 |
Beta Was this translation helpful? Give feedback.
-
k佬,排序算法这块能不能加上链表的,犹记得第一次面试,被问道链表的逆序,之前从来没考虑过,懵掉了🤣🤣🤣🤣 |
Beta Was this translation helpful? Give feedback.
-
K神,插入排序定义了base基准数,快排里也使用了基准数,但是并没有用局部变量来保存基准数,而在是在哨兵划分中定义了i和j,下边儿这种写法会不会更容易让读者理解呢? int partition(int[] nums, int left, int right) {
// 以 nums[left] 作为基准数,并记录基准数索引
int base = nums[left];
int baseIndex = left;
while (left < right) {
while (left < right && nums[right] >= base)
right--; // 从右向左找首个小于基准数的元素
while (left < right && nums[left] <= base)
left++; // 从左向右找首个大于基准数的元素
swap(nums, left, right); // 交换这两个元素
}
swap(nums, baseIndex, left); // 将基准数交换到两子数组的分界线
return left; // 返回基准数索引
} |
Beta Was this translation helpful? Give feedback.
-
刚发现哨兵划分这里,先 从右往左 还是 从左往右 寻找,结果是不一样的。显然,先 从右往左 是正确的 |
Beta Was this translation helpful? Give feedback.
-
基准数优化,Java代码,' < '本身优先级高于' ^ ',可略去括号 if (nums[left] < nums[mid] ^ nums[left] < nums[right])
return left;
else if (nums[mid] < nums[left] ^ nums[mid] < nums[right])
return mid; |
Beta Was this translation helpful? Give feedback.
-
快排的C语言实现中swap函数是已经自己实现了吗?我并没有查到C语言有封装好的swap函数 |
Beta Was this translation helpful? Give feedback.
-
K神,关于尾递归优化,为什么选短的数组能保证递归深度不超过 |
Beta Was this translation helpful? Give feedback.
-
尾递归优化的那个 未排序的子数组再怎么去处理? |
Beta Was this translation helpful? Give feedback.
-
请问, 如果数据全是一样的, 是不是会退化到n^2, 这该咋办 |
Beta Was this translation helpful? Give feedback.
-
格式出现点问题,例如 (0),都多了“\”,我使用的是edge浏览器 |
Beta Was this translation helpful? Give feedback.
-
这尾递归也牛逼了吧,这是怎么想出来的,我拿着241035自个推了一遍,实在是妙不可言。while (left < right)这个循环太牛了。对内递归,对外跳出递归。 |
Beta Was this translation helpful? Give feedback.
-
为什么在查找元素的时候,需要先要找出比基准数大的值呢?如果先找比基准数小的值的话,排序的结果是不正确的(即交换j--和i++的执行顺序) |
Beta Was this translation helpful? Give feedback.
-
这个尾递归优化相当于每次缩减了排序区间的这个的长度,只进行哨兵划分,每次子数组小的递归都没有进去就结束了,空间相当与没有递归。这样理解吗K神 |
Beta Was this translation helpful? Give feedback.
-
对于倒序数组,这个尾递归优化所占的空间复杂度是2吧? |
Beta Was this translation helpful? Give feedback.
-
关于左右指针的的执行顺序,先后都一样么 |
Beta Was this translation helpful? Give feedback.
-
c++中自定义的swap函数与它内置的swap函数会有什么区别吗,为什么不直接使用内置的函数呢 |
Beta Was this translation helpful? Give feedback.
-
hi! 第一个条件如果返回left 说明left是最小的,那if条件判断里的语句就成立,但成立的话通过异或该条件结果又是false,不满足if的条件判断,就应该不执行返回left,这不是矛盾吗 |
Beta Was this translation helpful? Give feedback.
-
我对 15.4 小节中代码的一点小疑问: |
Beta Was this translation helpful? Give feedback.
-
while (i < j && nums[j] >= nums[left]) |
Beta Was this translation helpful? Give feedback.
-
我来总结一下基准数优化和尾递归优化的作用和区别: |
Beta Was this translation helpful? Give feedback.
-
[4,3,5]怎么进行哨兵划分呀,第一开始哨兵是[4],然后从右往左找到第一个小于4的数:3,从左往右走找到大于哨兵的数,没找到,只能跟3相等,这样下一次哨兵就是3,下一个递归中,左右都是长度为一,那么最后还是[4,3,5],我哪里错了呀 |
Beta Was this translation helpful? Give feedback.
-
发现个问题,就是在partition方法中的while循环中,里面的while循环必须要是j--这个循环放在上面,而i++这个while要放在下面才可以,一旦交换位置,排序就不对,这是为什么?在逻辑上想不通这个道理。想着两个while子循环应该是彼此独立的,都是各干各的才对,为什么不能交换位置? |
Beta Was this translation helpful? Give feedback.
-
K神是怎么做到代码写的这么优美又这么易懂的?我曾经以为二者不可兼得,直到我看到了你的代码。 |
Beta Was this translation helpful? Give feedback.
-
K神,尾递归优化没看懂,能不能举个例子说明一下🙏 |
Beta Was this translation helpful? Give feedback.
-
非递归快速排序 def partition(arr, left, right):
i, j = left, right
while i < j:
while i < j and arr[j] >= arr[left]:
j -= 1
while i < j and arr[i] <= arr[left]:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i], arr[left] = arr[left], arr[i]
return i
def quick_sort(arr):
s = [(0, len(arr) - 1)]
while not len(s) == 0:
left, right = s.pop()
pivot = partition(arr, left, right)
if left < right:
s.append((left, pivot - 1))
if pivot + 1 < right:
s.append((pivot + 1, right))
if __name__ == '__main__':
nums = [5, 2, 8, 3, 9, 1, 7]
quick_sort(nums)
print(nums) |
Beta Was this translation helpful? Give feedback.
-
void QuickSorting(std::vector<int> &v,
std::vector<int>::size_type L,
std::vector<int>::size_type R)
{
if (L >= R)
return; // 左位置索引大于右位置索引,此数组已排完,函数退出
int benchmark = v[L ]; // 设左索引值为基准
auto i = L+1;
auto j = R;
/****************形成左子数组<=基准数<=右子数组****************/
while (i < j)
{
while (v[i] <= benchmark && i < j) // 找到左数组大于基准的索引
i++;
while (v[j] >= benchmark && i < j) // 找到右数组小于基准的索引
j--;
if (i < j) {
std::swap(v[i], v[j]);
}
}
std::swap(v[L],v[i]);//将基准值交换到分界线
/****************************************************************/
QuickSorting(v,L,i-1);//左子数组递归为左子数组<=基准数<=右子数组
QuickSorting(v,i+1,R);//右子数组递归为左子数组<=基准数<=右子数组
} |
Beta Was this translation helpful? Give feedback.
-
想请问一下,尾递归优化时,输入完全有序的数组的话,真的有优化吗,没有想明白 |
Beta Was this translation helpful? Give feedback.
-
chapter_sorting/quick_sort/
Your first book to learn Data Structure And Algorithm.
https://www.hello-algo.com/chapter_sorting/quick_sort/
Beta Was this translation helpful? Give feedback.
All reactions