简体   繁体   English

递归地反转数组

[英]Recursively reverse an array

I am trying to recursively reverse the elements of an array. 我试图递归地反转数组的元素。 The only parameters I can have in the function is the array and the size. 我只能在函数中使用的参数是数组和大小。 This is what I have done so far, but having trouble in swapping. 到目前为止,这是我所做的,但是在交换时遇到了麻烦。 How can i fix my output? 如何解决我的输出? For example when i enter 1 2 3 , the elements get reversed to 2 3 1 例如,当我输入1 2 3时,元素将反转为2 3 1

//Recursive function for Reversing array
void reverse_arr(int a[],int size){
    if(size ==0){
        return ;
    }
    else{
        int temp;
        int i= 0;
        temp = a[i];
        a[i]= a[size-1];
        a[size -1] = temp;
        reverse_arr(a, size-1);
    }      
}

int main() {
    int a[100];
    int size ;

    cout<<"Enter the size of the array: "<<endl;
    cin>>size;
    cout<<"Enter the elements of the array: "<<endl;
    for(int i = 0; i<size; i++){
        cin>>a[i]; }
    for(int i = 0; i<size; i++){

        cout<<a[i]<<"  ";}
    cout<<endl;
    reverse_arr(a, 3);
    for(int i = 0; i<size; i++){

        cout<<a[i]<<"  ";}
}
void Reverse(int a[], int size)
{
  if (size > 1)
  {
    // Swap the extreme elements
    int swap= a[0]; a[0]= a[size-1]; a[size-1]= swap;

    // Recurse on the subarray obtained by dropping the extreme elements
    Reverse(a+1, size-2);
  }
}

Always swapping the first and last elements doesn't do what you want. 始终交换第一个和最后一个元素并不能满足您的需求。 If your array is initially 1 2 3 4 5 , here's the sequence of swaps at each recursion step: 如果您的数组最初是1 2 3 4 5 ,则这是每个递归步骤的交换顺序:

  • swap 0 and 4 : 5 2 3 4 1 交换045 2 3 4 1
  • swap 0 and 3 : 4 2 3 5 1 交换034 2 3 5 1
  • swap 0 and 2 : 3 2 4 5 1 交换023 2 4 5 1
  • swap 0 and 1 : 2 3 4 5 1 交换012 3 4 5 1

In a recursive algorithm, each recursive step is supposed to solve the same problem on a smaller set of data, and combine that with what we've done so far to get closer to the desired result. 在递归算法中,每个递归步骤都应在较小的数据集上解决相同的问题,并将其与到目前为止所做的工作结合起来,以更接近所需的结果。 But swapping 1st and last is only correct if they're in the same order as the original, which isn't true after each of these steps. 但是,只有在第一个和最后一个的顺序与原始顺序相同的情况下,才可以交换第一个和最后一个,这在每个步骤之后都是不正确的。

After you swap the first and last elements of the array, you need to recurse on the middle of the array, not just the same array with size - 1 . 交换数组的第一个和最后一个元素后,需要递归在数组的中间 ,而不仅仅是size - 1的同一数组。 So you need to call the function recursively on the array starting with the 2nd element, and subtract 2 from size . 因此,您需要从第二个元素开始在数组上递归调用函数,并从size减去2。

The base case where you do nothing should be when size <= 1 , since a 1-element array is the same when you reverse it. 不执行任何操作的基本情况应该是size <= 1 ,因为反转1个元素的数组是相同的。

//Recursive function for Reversing array
void reverse_arr(int a[],int size){
    if(size <= 1 ){
        return ;
    }
    else{
        int temp;
        int i= 0;
        temp = a[i];
        a[i]= a[size-1];
        a[size -1] = temp;
        reverse_arr(&a[1], size-2);
    }
}

I will pass both the start and end index and use: 我将同时传递start索引和end索引并使用:

void reverseArray(int arr[], int start, int end) 
{
    if (start >= end)
    return;

    int temp = arr[start]; 
    arr[start] = arr[end];
    arr[end] = temp;

    // Recursive Function calling
    reverseArray(arr, start + 1, end - 1);  
}

The logic is simple, swap arr[start] with arr[end] then recursively call reverse for rest of the array. 逻辑很简单,将arr [start]与arr [end]交换,然后递归调用反向数组的其余部分。

If you want to do it with just passing the size: 如果要通过传递大小来做到这一点:

void reverseArray(int a[],int size)
{
  if (size > 1)
  {    
    //swapping
    int temp = a[0]; 
    a[0]= a[size-1]; 
    a[size-1]= temp;

    reverseArray(a+1, size-2);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM