c++ - 使用递归(无循环)C++ 对数组进行冒泡排序

标签 c++ arrays recursion

#include <iostream>
#include <cstdlib>

using std:: cin;
using std:: cout;
using std:: endl;

const int N=10;

void readarray(int array[], int N);
int bubble_sort (int array[], int size, int round,
                 int place);

int main ()
{
    int array[N];
    readarray( array, N );

    int round, place;
    cout << bubble_sort(array, N, place, round);


    return EXIT_SUCCESS;
}


void readarray(int array[], int N)
{
    int i=0;
    if (i < N)
    {
        cin >> array[i];
        readarray(array+1, N-1);
    }
}


int bubble_sort (int array[], int size, int round,
                int place)
{
    round =0;
    place =0;

  if (round < N-1) // this goes over the array again making sure it has 
                   // sorted from lowest to highest
  {
     if (place < N - round -1) // this sorts the array only 2 cells at a 
                               // time
         if (array[0] > array[1])
         {
            int temp = array[1];
            array[1]=array[0];
            array[0]=temp;
            return (array+1, size-1, place+1, round);
         }
   return (array+1, size-1, place, round+1);
   }
}

我知道如何使用两个 for 循环进行冒泡排序,我想使用递归来进行。使用循环你需要两个 for 循环,我认为对于递归它可能还需要两个递归函数/调用。这是我到目前为止所拥有的。问题是它只输出一个数字,要么是 1,要么是 0。我不确定我的返回是否正确。

最佳答案

在c++11中,你可以这样做:

#include <iostream>
#include <vector>


void swap(std::vector<int &numbers, size_t i, size_t j)
{
    int t = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = t;
}

bool bubble_once(std::vector<int> &numbers, size_t at)
{
    if (at >= numbers.size() - 1)
        return false;
    bool bubbled = numbers[at] > numbers[at+1];
    if (bubbled)
        swap(numbers, at, at+1);
    return bubbled or bubble_once(numbers, at + 1);
}

void bubble_sort(std::vector<int> &numbers)
{
    if ( bubble_once(numbers, 0) )
        bubble_sort(numbers);
}

int main() {
    std::vector<int> numbers = {1,4,3,6,2,3,7,8,3};
    bubble_sort(numbers);

    for (size_t i=0; i != numbers.size(); ++i)
        std::cout << numbers[i] << ' ';
}

通常,您可以用递归函数替换每个循环:

  1. 检查守卫 -> 如果失败返回。
  2. 否则执行正文
  3. 递归调用函数,通常使用递增的计数器或其他东西。

但是,为了防止(n 实际)堆栈溢出,在循环同样足够的情况下避免递归是一种很好的做法。此外,循环具有非常规范的形式,因此对于许多程序员来说很容易阅读,而递归可以在许多程序员中完成,因此更难阅读、测试和验证。哦,递归通常比较慢,因为它需要创建一个新的堆栈框架(需要引用,不太确定)。

编辑

使用普通数组:

#include <iostream>
#include <vector>

#define N 10

void swap(int *numbers, size_t i, size_t j)
{
    int t = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = t;
}

bool bubble_once(int *numbers, size_t at)
{
    if (at >= N - 1)
        return false;
    bool bubbled = numbers[at] > numbers[at+1];
    if (bubbled)
        swap(numbers, at, at+1);
    return bubbled or bubble_once(numbers, at + 1);
}

void bubble_sort(int *numbers)
{
    if ( bubble_once(numbers, 0) )
        bubble_sort(numbers);
}

int main() {
    int numbers[N] = {1,4,3,6,2,3,7,8,3,5};
    bubble_sort(numbers);

    for (size_t i=0; i != N; ++i)
        std::cout << numbers[i] << ' ';
}

关于c++ - 使用递归(无循环)C++ 对数组进行冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34968275/

相关文章:

c++ - 将指针作为参数传递给 void* 参数时转换指针

arrays - Swift 字典到数组来填充 pickerView

python - 数组列表 : How can I see if an array in one list is in another list?

java - Java中的数独求解器,使用回溯和递归

c++ - 组合 std::strings 和 C-Strings 导致缓冲区溢出

c++ - Visual Studio std::complex 性能

javascript - 如何使用分割成数组的字符串搜索数组?

c - 理解静态 int 执行

python - 在Python中的递归coi函数中返回一个列表

c++ - 是否可以在其成员函数中使用类模板?