- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用中值主元元素实现 QuickSort 算法...如果没有数字出现两次,我的代码就可以工作,但这无论如何都不是解决方案。代码运行完美,如果我选择分区的第一个元素作为主元,无论值是否出现两次或更多...
这是我的代码:
void quickSort(int a[ ], int from, int to)
{ // sort partition from ... to of array a
int i, pivot, new_val;
if (from < to) // at least 2 elements in partition
{
//pivot = from; --> old version, first element is pivot
pivot = median(a, from, to);
for (i = from; i <= to; i++)
{
new_val = a[i];
if (new_val < a[pivot])
{ // shift values to the right
a[i] = a[pivot+1];
a[pivot+1] = a[pivot];
a[pivot] = new_val;
pivot++;
} // else a[i] stays put
}
quickSort(a, from, pivot-1); // left partition
quickSort(a, pivot+1, to); // right partition
} // else (from >= to) only 0 or 1 elements
} // end quicksort
int median(int a[], int from, int to)
{
int med = (from + to) / 2;
if((a[from] < a[med] && a[med] <= a[to]) || (a[to] < a[med] && a[med] < a[from]))
return med;
else if((a[med] < a[from] && a[from] < a[to]) || (a[to] < a[from] && a[from] < a[med]))
return from;
else if((a[med] < a[to] && a[to] < a[from]) || (a[from] < a[to] && a[to] < a[med]))
return to;
}
我做错了什么??
提前致谢!
最佳答案
当某些元素彼此相等时,您的中值函数将失效。
如果输入是 3、3 和 3,则函数中的条件都不计算为真。
在这种情况下,函数找不到任何返回语句并且行为未定义。
尝试使用 <=
而不是 <
在条件中。
此外,您正在使用 pivot + 1
不确定 pivot
不是最后一个元素。
如果中位数是数组的最后一个元素,程序就会崩溃。
你确定它适用于正常输入吗,因为我试过了,但这个函数没有正确地对数组进行排序。
您可以在分区例程中找到很好的解释 here .
关于c - QuickSort C 中位数枢轴元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23932321/
我有一个与这个非常相似的问题: How to pivot a MySQL entity-attribute-value schema 或者这个 Enumerating combinations via
我在 postgresql-11 中使用 crosstab() 时遇到问题。 这是我的 table , CREATE TABLE monitor(tz timestamptz, level int,
我目前正在使用 ViewPropertyAnimators 来缩放 ImageView。然而,问题是图像总是从中心向外缩放,当我希望它从最左边缩放并向右缩放时。似乎没有枢轴方法。我还有其他方法可以做到
问题 1:我有一个具有以下结构和数据的表: app_id transaction_id mobile_no node_id customer_attribute entered_value
import numpy as np import pandas as pd data = {'experiment_name': ['exp1', 'exp1', 'exp1', 'exp1', '
我正在构建一个包含多个跟踪器的系统,这些跟踪器将使用许多相同的列,因此当用户插入跟踪器时,有一个用于跟踪器的表、跟踪器列,然后是哪些列与哪个跟踪器搭配的交叉引用row 不同的列值存储在共享相同记录 I
我有一个数据帧,其中有几个变量(此处为 Var1 和 Var2),这些变量在不同位置(此处为 Station)是不同的。然后我有一定数量的案例,对于每个案例,我在每个位置都会得到不同的值。 例如: n
我们正在开发 C# 应用程序,我们一直在使用 Linq to SQL 或标准 ADO(当需要性能时)与 SQL Server 一起工作。 我们有一个这样布置的表: 客户 ID、年/月、产品名称、数量
如何在 UWP 上创建一个枢轴,如果选中标题选项卡上的按钮,它将导航到其他页面?以及如何在标题选项卡上使用图像?例如在下图中,如果选择选项卡标题“Store”,它将导航到“Store”页面。 我试过搜
开始了解它的所谓的枢轴,但无法在没有聚合的情况下获得 sqlite 的示例。 架构: Readonly Table foos(_id, foo) (10 max records) Readonly T
我在 postgres 中有一个表(这是查询的结果),它有一组行(复杂的数据求和的结果),如下所示:(列名是每一天的名称, 每列的值都是 double 。) 周日周一周二周三周四周五 1.24 1.1
我已经学习和练习 sql 大约 6 个月了。我已经准备好投入其中,只是承认我对此很愚蠢,而且我的大脑无法理解它的大部分模式。我正在尝试创建一个数据透视表。关于这个主题的简单文献并不多,我找到的一个来源
我有一些模仿以下结构的数据: rdd = sc.parallelize( [ (0,1), (0,5), (0,3), (1,2
我有一个如下所示的数据框: id Revenue Cost qty time 0 A 400 50 2 1 1 A
我正在使用 Laravel 4 构建应用程序,但偶然发现了数据透视表的问题。 我有一个用户模型、一个建立模型和一个 StudyLevel 模型。 目前,为了找到用户所在的位置,我在用户模型中使用了以下
https://databricks.com/blog/2016/02/09/reshaping-data-with-pivot-in-apache-spark.html很好地解释了一个枢轴如何为 S
我正在尝试使用 Python 为一些时间序列建模准备数据Pandas (第一个计时器)。我的 DataFrame看起来像这样: df = pd.DataFrame({ 'time': [0,
我想转换以下 Pandas 数据框 a b 0 1 2 1 1 5 2 2 4 3 1 3 4 3 7 5 2 1 到 0 1
我要获取相对于 DisplayObject 的 anchor 例子: // HOW GET THIS VALUES IN CODE Object height = 90px Objec
所以我有这个 DF: In [130]: dfAbr Out[130]: ip ospfArea router_name 0 1.1.1.1 0.0.0.2 Rou
我是一名优秀的程序员,十分优秀!