- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <stdio.h>
#define ARRAY_SIZE 10
void lomuto (int A[], int l, int r, int smallerAtLeft)
{
if (smallerAtLeft == 1) //move elements smaller than pivot to the left and the greater ones to the right
{
int tmp, tmp2,pivot,i,j;
pivot = A[r];
i = l-1;
for (j =0; j<r-1; j++)
{
if (A[j] <= pivot)
{
i++;
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
tmp2 = A[i+1];
A[i+1] = A[r];
A[r] = tmp2;
}
if (smallerAtLeft == 0) //move elements smaller than pivot to the right and the greater ones to the left
{
int tmp3, tmp4,pivot,i,j;
pivot = A[r];
i = l-1;
for (j=0; j<r-1; j++)
{
if (A[j]>= pivot)
{
i++;
tmp3 = A[i];
A[i] = A[j];
A[j] = tmp3;
}
}
tmp4 = A[i+1];
A[i+1] = A[r];
A[r] = tmp4;
}
}
void quicksort (int A[], int l, int r, int ascending)
{
lomuto (A,l,r,ascending);
}
int main()
{
int testarray;
int testArray[ARRAY_SIZE] = {4, 2, 5, 3, 6, 7, 8, 1, 0};
quicksort (testarray,0,8,1);
return testarray;
}
晚上好。通常,我会搜索几乎每个论坛和最深层的线程,以查找代码中的可疑之处。但这一次我没有找到可以帮助我的答案。如果有人能告诉我为什么 code-exe 停止工作但在编译过程中屏幕上没有显示错误,我将非常感激。我们必须使用 lomuto 分区来实现快速排序算法。如果变量“smallerAtLeft”等于 1,则数组应按递增属性排序,如果变量“smallerAtLeft”等于 0,则应按递减属性排序。
此外,我们必须实现 void 函数,就像您在代码中看到的那样。 “lomuto-fct”和包含 lomuto 的“quicksort-fct”。
也许这个 Reverse-Lomuto-Thread 将来也会帮助其他人..
最佳答案
我认为您不明白 main
的返回值是什么是什么以及它的用途。它通常是成功和失败的指标,典型值0
成功时为小正值,失败时为小正值。甚至在 <stdlib.h>
中为此目的定义了宏。头文件: EXIT_SUCCESS
and EXIT_FAILURE
.
如果您想查看排序后的数组,您需要打印它:
printf("Sorted array = {");
for (unsigned i = 0; i < ARRAY_SIZE; ++i)
{
printf(" %d", testArray[i]);
}
printf(" }\n");
这当然需要您将实际的数组传递给排序函数。
关于C-Lomuto Quicksort Exe 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43147644/
我已经使用多线程实现了快速排序,它正确地对整数数组进行了排序,但是它比普通的快速排序需要更长的时间来执行。例如,对 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
我是一名优秀的程序员,十分优秀!