未进行/保留对阵列的更改
Changes to array are not being made/kept
家庭作业的一部分是用C ++以"函数式"编写快速排序。 我已经大部分代码下来了,但我遇到的唯一问题是对数组的更改没有保留。 这是我的代码,我知道它可能看起来不像函数式编程,但我的老师说这很好:
#include <stdio.h>
#include <iostream>
void swap(int* x, int* y);
int partition(int arr[], int low, int high, int cursor);
void quickSort(int A[], int start, int end);
int main(){
int list[20] = {20, 5, 12, 11, 1, 6, 18, 7, 2, 3, 14, 19, 10, 4, 13, 8, 15, 9, 17, 16};
quickSort(list,0,19);
for(int count =0; count<20; count++){
std::cout<< list[count] << " ";
}
};
void swap(int* x, int* y){
int temp = *x;
*x = *y;
*y = temp;
};
int partition(int arr[], int low, int high, int cursor){
if(low == high){
swap(&arr[cursor+1], &arr[high]);
return cursor+1;
}else if(arr[low] <= arr[high]){
swap(&arr[cursor+1], &arr[low]);
return partition(arr, low+1, high, cursor+1);
}else if(arr[low] > arr[high] && low != high){
return partition(arr, low+1, high, cursor);
}
};
void quickSort(int A[], int start, int end){
if(start < end){
int p = partition(A, start, end, start-1);
quickSort(A, start, p-1);
quickSort(A, p+1, end);
}
};
感谢 timrau 指出缺少的条件,我的问题得到了解决。 一个愚蠢的菜鸟错误,我稍后会踢自己。 谢谢。 如果其他人可能遇到这样的问题,我为解决问题而添加的代码是这样的:
else if(arr[low] > arr[high] && low != high){
return partition(arr, low+1, high, cursor);
}
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- OpenMP阵列性能较差
- 为多个会话保留XPtr
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- 保留对其他类的成员函数的引用
- 阵列必须使用大括号封闭的初始器进行初始化
- 指针保留字符串
- 是否有内置方法可以强制转换为不同的基础类型,但保留常量限定符?
- 没有从阵列<float>到阵列<int>的可行转换
- 如何让 GCC/Clang 在保留标识符上出错
- 必须为 C++20 协程帧保留多少内存?
- C++动态安全 2D 交错阵列
- 如何将一个窗口保留在另一个应用程序窗口的前面
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 即使阵列腐烂到指针,如何将函数的参数保留为数组,这很好
- 未进行/保留对阵列的更改