使用递归(无循环)c+对数组进行冒泡排序+; #包括 #包括 使用std::cin; 使用std::cout; 使用std::endl; 常数int N=10; void readarray(int数组[],int N); int bubble_排序(int数组[],int大小,int舍入, 国际广场); int main() { 整数数组[N]; readarray(数组,N); int-round,place; cout>数组[i]; readarray(数组+1,N-1); } } int bubble_排序(int数组[],int大小,int舍入, 内部位置) { 四舍五入=0; 地点=0; if(round数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } void bubble_排序(标准::向量和数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 向量数={1,4,3,6,2,3,7,8,3}; 气泡_排序(数字); 对于(size_t i=0;i!=numbers.size();+i) 标准::cout=N-1) 返回false; 布尔气泡=数字[at]>数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } 无效气泡_排序(整数*数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 整数[N]={1,4,3,6,2,3,7,8,3,5}; 气泡_排序(数字); 对于(大小i=0;i!=N;++i) 在c++11中,可以执行以下操作: #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); } } #包括 #包括 无效交换(std::vector=numbers.size()-1) 返回false; 布尔气泡=数字[at]>数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } void bubble_排序(标准::向量和数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 向量数={1,4,3,6,2,3,7,8,3}; 气泡_排序(数字); 对于(size_t i=0;i!=numbers.size();+i) 标准::cout=N-1) 返回false; 布尔气泡=数字[at]>数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } 无效气泡_排序(整数*数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 整数[N]={1,4,3,6,2,3,7,8,3,5}; 气泡_排序(数字); 对于(大小i=0;i!=N;++i) 标准::cout

使用递归(无循环)c+对数组进行冒泡排序+; #包括 #包括 使用std::cin; 使用std::cout; 使用std::endl; 常数int N=10; void readarray(int数组[],int N); int bubble_排序(int数组[],int大小,int舍入, 国际广场); int main() { 整数数组[N]; readarray(数组,N); int-round,place; cout>数组[i]; readarray(数组+1,N-1); } } int bubble_排序(int数组[],int大小,int舍入, 内部位置) { 四舍五入=0; 地点=0; if(round数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } void bubble_排序(标准::向量和数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 向量数={1,4,3,6,2,3,7,8,3}; 气泡_排序(数字); 对于(size_t i=0;i!=numbers.size();+i) 标准::cout=N-1) 返回false; 布尔气泡=数字[at]>数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } 无效气泡_排序(整数*数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 整数[N]={1,4,3,6,2,3,7,8,3,5}; 气泡_排序(数字); 对于(大小i=0;i!=N;++i) 在c++11中,可以执行以下操作: #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); } } #包括 #包括 无效交换(std::vector=numbers.size()-1) 返回false; 布尔气泡=数字[at]>数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } void bubble_排序(标准::向量和数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 向量数={1,4,3,6,2,3,7,8,3}; 气泡_排序(数字); 对于(size_t i=0;i!=numbers.size();+i) 标准::cout=N-1) 返回false; 布尔气泡=数字[at]>数字[at+1]; 如果(冒泡) 互换(数字、at、at+1); 返回气泡或气泡_一次(数字,在+1处); } 无效气泡_排序(整数*数字) { if(气泡_一次(数字,0)) 气泡_排序(数字); } int main(){ 整数[N]={1,4,3,6,2,3,7,8,3,5}; 气泡_排序(数字); 对于(大小i=0;i!=N;++i) 标准::cout,c++,arrays,recursion,C++,Arrays,Recursion,我知道如何使用两个for循环进行冒泡排序,我想使用递归进行排序。使用循环需要两个for循环,而我认为对于递归,可能还需要两个递归函数/调用。这就是我目前所拥有的。问题是它只输出一个数字,即1或0。我不确定我的报税表是否正确 在c++11中,可以执行以下操作: #include <iostream> #include <cstdlib> using std:: cin; using std:: cout; using std:: endl; const int N=10

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

在c++11中,可以执行以下操作:

#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);
   }
}
#包括
#包括
无效交换(std::vector=numbers.size()-1)
返回false;
布尔气泡=数字[at]>数字[at+1];
如果(冒泡)
互换(数字、at、at+1);
返回气泡或气泡_一次(数字,在+1处);
}
void bubble_排序(标准::向量和数字)
{
if(气泡_一次(数字,0))
气泡_排序(数字);
}
int main(){
向量数={1,4,3,6,2,3,7,8,3};
气泡_排序(数字);
对于(size_t i=0;i!=numbers.size();+i)
标准::cout=N-1)
返回false;
布尔气泡=数字[at]>数字[at+1];
如果(冒泡)
互换(数字、at、at+1);
返回气泡或气泡_一次(数字,在+1处);
}
无效气泡_排序(整数*数字)
{
if(气泡_一次(数字,0))
气泡_排序(数字);
}
int main(){
整数[N]={1,4,3,6,2,3,7,8,3,5};
气泡_排序(数字);
对于(大小i=0;i!=N;++i)

在c++11中,可以执行以下操作:

#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);
   }
}
#包括
#包括
无效交换(std::vector=numbers.size()-1)
返回false;
布尔气泡=数字[at]>数字[at+1];
如果(冒泡)
互换(数字、at、at+1);
返回气泡或气泡_一次(数字,在+1处);
}
void bubble_排序(标准::向量和数字)
{
if(气泡_一次(数字,0))
气泡_排序(数字);
}
int main(){
向量数={1,4,3,6,2,3,7,8,3};
气泡_排序(数字);
对于(size_t i=0;i!=numbers.size();+i)
标准::cout=N-1)
返回false;
布尔气泡=数字[at]>数字[at+1];
如果(冒泡)
互换(数字、at、at+1);
返回气泡或气泡_一次(数字,在+1处);
}
无效气泡_排序(整数*数字)
{
if(气泡_一次(数字,0))
气泡_排序(数字);
}
int main(){
整数[N]={1,4,3,6,2,3,7,8,3,5};
气泡_排序(数字);
对于(大小i=0;i!=N;++i)
标准::cout请阅读

请阅读


您忘记了
return
之后的函数名,以使其递归调用自身。此外,如果您的函数确实返回非void值(但为什么返回?),则需要一个“base case”return语句(没有递归调用),可能在函数的最末尾.为什么?反正这是个糟糕的算法;为什么要让它更糟?如果你想要一个低效的O(N^2)排序、插入排序和选择排序至少简单易懂。如果您只想排序,请使用
std::sort
@AlanStokes。我想练习递归函数,冒泡排序似乎是一种很好的做法。因此,如果有任何帮助,我们将不胜感激。@leems“基本情况”是什么be?@AncientDragon因为你的函数有一个返回类型
int
,你需要返回一些整数值…但是我猜你在这里犯了一个错误,根本不想返回一个值。然后,你只需要
return;
,你根本不需要写它。你忘记了
return
后面的函数名,使它成为c所有这些都是递归的。此外,如果函数确实返回非空值(但为什么返回?),则可能在函数的最后需要一个“基本情况”返回语句(没有递归调用)。为什么?这是一个糟糕的算法;为什么要使它更糟?如果您想要一个低效的O(N^2)排序、插入排序和选择排序至少简单易懂。如果您只想排序,请使用
std::sort
@AlanStokes。我想练习递归函数,冒泡排序似乎是一种很好的做法。因此,如果有任何帮助,我们将不胜感激。@leems“基本情况”是什么be?@AncientDragon由于您的函数的返回类型为
int
,您需要返回一些整数值……但我猜您在这里犯了一个错误,根本不打算返回值。然后,您只需要
返回;
,您根本不需要编写它。谢谢,如果有改动,您对std::vectors并不太熟悉如果你愿意的话,你可以用int[N]替换std::vector,用N替换number.size()。只是为了确保,size\t代表向量的大小?,如果是的话,我可以用其他东西替换它。(使用向量还不是很舒服)。非常感谢!不,size\t是一种类似int的类型(特别是,它是一个无符号整数)。大小与大小类型相同。我发现
|
更具可读性,特别是因为你可以阅读
,而你不能真正阅读
|
)谢谢,我对std::vector不太熟悉,如果有其他方法的话,我将不胜感激。您可以用int[N]交换std::vector
#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] << ' ';
}
function pass(i,j,n,arr)
{
  if(arr[i]>arr(j))
    swap(arr[i],arr[j]);

  if(j==n)
  {
    j=0;
    i=i+1;
  }
  if(i==n+1)
    return arr;

  return pass(i,j+1,n,arr);
}