- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有没有办法根据多个条件进行快速排序?例如,我有一组边。每条边都有源、目的地和长度。我想先把长度较小的边放在我的数组中。但如果长度相同,我想用较小的源顶点排序。如果这些源顶点相同,我想按两个目标顶点中较小的一个排序。
例如:
4(来源)2(目的地)3(长度)
1(来源)5(目的地)3(长度)
因为它们的长度相同,所以我们查看源顶点。由于第二条边小于第一条边,我们交换它们,因为我们按源顶点进行比较。
下面是我的快速排序,老实说,我不确定为什么排序不正确。如果有办法使快速排序效率更低但更稳定,我很乐意接受建议!
void quickSort(edge *e, int left, int right)
{
int i = left, j = right;
int temp, temp1, temp2;
int pivot = (left + right)/2;
while(i <= j)
{
while(e[i] < e[pivot])
i++;
while(e[pivot] < e[j])
j--;
if(i <= j)
{
temp = e[i].getLength();
temp1 = e[i].getEdgeSrc();
temp2 = e[i].getEdgeDes();
e[i].setLength(e[j].getLength());
e[i].setEdgeSrc(e[j].getEdgeSrc());
e[i].setEdgeDes(e[j].getEdgeDes());
e[j].setLength(temp);
e[j].setEdgeSrc(temp1);
e[j].setEdgeDes(temp2);
i++;
j--;
} //if statement
}///while loop
if(left < j)
quickSort(e, left, j);
if(i < right)
quickSort(e, i, right);
}
我的排序条件:
bool edge::operator<(const edge &other) const
{
if (length < other.length)
return true;
else if ((length == other.length) && (source < other.source))
return true;
else if((length == other.length) && (source == other.source) && (destination < other.destination))
return true;
return false;
}
同样,如果有人知道通过降低时间复杂度但使其稳定来正确进行此快速排序的方法,我很乐意接受任何建议!谢谢你!有帮助吗?
编辑:这就是我调用快速排序的方式。我根据读取的边数调用它。
quickSort(e, 0, edges-1); //-1 because if you put in edges, it'd go past the bounds of the array
编辑:当我尝试在我的算法中加入这样的东西时:
0 1 1
0 3 1
1 3 1
2 5 1
4 10 1
4 8 1
10 8 1
11 6 2
11 7 2
6 7 1
9 6 1
9 7 1
这是输出:
0 1 1
0 3 1
1 3 1
2 5 1
4 8 1
4 10 1
6 7 1
6 9 1
8 10 1 <- 应该低于 7 9 1
7 9 1 <- 应该大于 8 10 1
6 11 2
7 11 2
最佳答案
这样写比较干净
if (length != other.length)
return length<other.length;
if ( source != other.source)
return source < other.source;
return destination < other.destination;
您还应该能够执行 temp = e[i]
等操作,因为成员都是整数。
我认为这(以及您提交的代码)应该可以完成您想要的任务。
如果您遇到稳定性问题,那是因为 quicksort isnt stable .您可以通过添加更多条件来绕过它,这样 lhs==rhs
就不会发生。或者你可以尝试 Mergesort
坦率地说,我对快速排序没有太多经验,但您的 impl 看起来确实与 Wikipedias In Place Algorithm 明显不同。 .例如,您的支点根本没有移动。你能检查一下这是否是问题所在吗?
看了your link之后
看起来链接的算法也使用 pivot 作为值而不是索引(就像你所做的那样)。它在语法上看起来与你的相同,直到你认为你的枢轴值可能会移动,之后你的枢轴索引将指向其他东西
int pivot = arr[(left + right) / 2];
这有帮助吗?
关于c++ - 使 QuickSort 按多个条件排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446454/
我已经使用多线程实现了快速排序,它正确地对整数数组进行了排序,但是它比普通的快速排序需要更长的时间来执行。例如,对 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
我是一名优秀的程序员,十分优秀!