- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
我正在尝试使用 Lomuto's Partitiong 实现迭代QuickSort方法,出于这个原因,我正在尝试实现一个堆栈,它包含一对定义要分区的子数组的索引,使用一个 struct
数组和两个 fields
:iBeg
、iEnd
和仅存储/访问 end
元素。
代码如下:
function [sorted] = iterativeQuickSort(A)
% Accepts 1xN unsorted integer array.
% Returns a sorted copy.
% See also Partition.
% Starting and ending indexes of unsorted array.
iBeg = 1;
iEnd = numel(A);
% Struct holding A's subarrays star/end indexes resulting from partitioning.
stack_elem = struct('iBeg', iBeg, 'iEnd', iEnd);
stack(end + 1) = stack_elem; % push on stack
while numel(stack) != 0
% Extract last pair of indexes.
iBeg = stack(end).iBeg;
iEnd = stack(end).iEnd;
stack(end) = []; % pop from stack
% Get pivot index and array after rearranging elements around the pivot.
[B, pivotIndex] = Partition(A, iBeg, iEnd);
A = B;
% Store indexes of the next two subarrays defined by the pivot index,
% if their sizes are > 0.
if pivotIndex - 1 > iBeg
stack_elem = struct('iBeg', iBeg, 'iEnd', pivotIndex - 1);
stack(end + 1) = stack_elem;
end
if pivotIndex + 1 < iEnd
stack_elem = struct('iBeg', pivotIndex + 1, 'iEnd', iEnd);
stack(end + 1) = stack_elem;
end
end
sorted = A;
end
function [A, pivotIndex] = Partition (A, iBeg, iEnd)
% Accepts 1xN integer array.
% Two integers - start and end indexes current subarray of A.
% Returns index of pivot element of current subarray partition
% and A after swaps.
pivotValue = A(iEnd); % Choose last element to be pivot.
pivotIndex = iBeg; % Initialize pivot index to start of subarray.
for i = iBeg : iEnd % Iterate over current subarray
if A(i) <= pivotValue % Push elements <= pivot in front of pivot index.
% Place element at i-th position before element with pivot index.
[A(i), A(pivotIndex)] = swapElements(A(pivotIndex), A(i));
% Account for the swap, go to next element.
pivotIndex = pivotIndex + 1;
end
end
% Bring the element used as pivot to its place
[A(iEnd), A(pivotIndex)] = swapElements(A(pivotIndex), A(iEnd));
end
function [elem2, elem1] = swapElements(elem1, elem2)
[elem2, elem1] = deal(elem1, elem2);
end
明显愚蠢的数组赋值 A = B
是为了指示由于 swap
引起的元素变化在函数 Partition(A, iBeg, iEnd)
.
目前的状态似乎是一个无限循环,其原因我无法确定,因此任何建议和建议将不胜感激!
输入:
A = [5, 4, 6, 2, 9, 1, 7, 3];
S = iterativeQuickSort(A)
预期输出:
[1, 2, 3, 4, 5, 6, 7, 9]
当前输出:永远不会从功能中返回,仅通过强制制动停止:ctrl + c。
注意:分区函数的实现和应用与指出可能重复的不同。
我已经使用多线程实现了快速排序,它正确地对整数数组进行了排序,但是它比普通的快速排序需要更长的时间来执行。例如,对 10000 个整数进行排序多线程:6856 毫秒正常:1毫秒 我不知道我的代码有什么
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我正在比较 Julia 和 C++ 之间的性能。然后我发现 Julia 中的快速排序要快得多(甚至比 C++ 还要快),尤其是当数组的大小非常大时。 任何人都可以解释原因吗? quickSort.jl
据说特定的分区算法可以导致稳定的快速排序,我想知道要做到这一点需要什么细节? 如果有人有创建此类算法的示例或提示,将会有所帮助 最佳答案 分区算法被用作快速排序算法的“核心”。 “明智地”使用分区算法
我试图了解 APL 中的经典快速排序: Q←{1≥≢⍵:⍵ ⋄ S←{⍺⌿⍨⍺ ⍺⍺ ⍵} ⋄ ⍵((∇S))⍵⌷⍨?≢⍵} 有些东西我不明白,有些风格选择让我很困扰,所以我将把它们全部列出来。我希望
我对快速排序的理解是 选择一个枢轴元素(在本例中我选择中间元素作为枢轴) 在极值处初始化左指针和右指针。 查找枢轴左侧第一个大于枢轴的元素。 同样找到枢轴右侧第一个小于枢轴的元素 交换 3 和 4 中
我在此指出,很难进行稳定的快速排序。但是,我的快速排序似乎很稳定。 quicksortBy _ []=[] quicksortBy key (pivot:rest)= (quicksortBy
我在此指出,很难进行稳定的快速排序。但是,我的快速排序似乎很稳定。 quicksortBy _ []=[] quicksortBy key (pivot:rest)= (quicksortBy
我读到可以让快速排序以 O(nlogn) 运行 该算法表示在每一步中选择中位数作为主元 但是,假设我们有这个数组: 10 8 39 2 9 20 哪个值将是中位数? 在数学中,如果我没记错的话,中位数
我有这样一个算法问题:我需要让 Quicksort 像这样工作: 1) 数组的下标为奇数时,应从小到大排序 2) 即使是索引也应该从大到小排序。 所以如果我们有数组:2 5 1 3 4 0 6 2 5
我是 Java 新手,我正在尝试实现 QuickSort。下面是我的脚本。 public class QuickSort { public static void main(String[]
你能解释一下这个用java实现的快速排序算法有什么问题吗? static ArrayList quickSort(ArrayList array){ if (array.size() a =
我正在尝试使用就地排序在 python 中编写快速排序代码。我的代码在子数组中运行完美,但是它似乎无法将子数组粘在一起以形成最终的排序数组。 def quickSort (ar): if le
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有一个程序来接收一个结构,存储它然后对其进行排序。我尝试过使用希尔排序,但后来我选择了快速排序算法。但是,当我尝试在排序后打印数组时,它仍然返回未排序的数组。请记住,我正在尝试按“num_aluno
当我尝试运行此 QuickSort 时,出现段错误:11,但它编译正常。我使用驱动程序运行它,这就是我使用 fastsort() 和 fastsortR() 的原因。是什么导致了分段? /* ---
我现在正在学习不同类型的排序,我发现,从某个点开始,我的快速排序算法根本无法快速工作。 这是我的代码: class QuickSort { // partitioning arr
我正在编写一个网络应用程序,它会向用户询问一系列问题,这些问题只是两个值的主观比较。他们选择更大的那个,然后它构成了排序所需的下一个比较。目标是对58个项目进行排序,并显示排序列表。 我想使用快速排序
我一直在用头撞 table 。 我需要创建一个 n 大小的数组,该数组针对快速排序分区进行了优化。它将用于演示 QuickSort 的最佳案例的增长。我知道在最好的情况下,QuickSort 必须为每
对于quickSort,我有以下4种分区方法的代码.现在,如果我运行代码,各种分区的性能如下 partition0 性能为 1877, 分区 2 是 781, 分区 3 674, partition4
我是一名优秀的程序员,十分优秀!