非递归实现快拍,c中如何利用递归和非递归实现公式Pnx2n1xPn1xn1Pn

1,c中如何利用递归和非递归实现公式Pnx2n1xPn1xn1Pnfloat OnPn(int x, int n) if (n == 1)return 1; if (n == 0)return 1; return ((2*n - 1)*x*OnPn(x, n-1) - (n-1)*OnPn(x, n-2))/n;}假设P1(x) = 1;P0(x) = 1 。搜一下:c++中如何利用递归和非递归实现公式Pn(x)=((2n-1)*x*Pn-1(x)-(n-1)*Pn-2(x))/n
2,一个非递归程序/stack>/n,直接for循环遍历即可 while(sta[0];/ #include <,6}}; using namespace std;每个数组元素 int a[n][m] = {{1;i> #include <,4;j< /stdafx;i<,肯定是m^n种组合;< int sta[n]=;j++) { sta[j] =m;i--) { if(sta[i] ==0) { sta[i-1] = sta[i-1]-1;count++<",5},3;0;数组个数 const int m =4,4};< int _tmain(int argc;endl;i++) { sta[i]=m;j到n都重置为m for(int j =i; } void adjuststa() { / bool isstahaszero() { for(int i =0; /,2,5,4;输出数据 for(int i =0#include "#include "stdafx.h"#include <stack>#include <iostream>using namespace std;const int N = 3;//数组个数const int M =4; //每个数组元素int a[N][M] = int Sta[N]=bool IsStaHasZero() for(int i =0;i<N;i++)if(Sta[i]==0)return true;} } return false;}void AdjustSta() //Sta中不应该有0,如果有,那么进行调整 while(IsStaHasZero())for(int i=N-1;i>0;i--)if(Sta[i] ==0)Sta[i-1] = Sta[i-1]-1;//j到N都重置为Mfor(int j =i;j<N;j++)Sta[j] =M;}}} }}int Count =1;int _tmain(int argc, _TCHAR* argv[]) //初始化 for(int i =0;i<N;i++)Sta[i]=M; } //第一个问题,肯定是M^N种组合,直接for循环遍历即可 while(Sta[0]!=0)//输出数据for(int i =0;i<N;i++)cout<<a[i][Sta[i]-1]<<" ";}Sta[N-1] = Sta[N-1]-1;AdjustSta();cout<<Count++<<endl; } getchar(); return 1;} 第二个问题在输出的地方改为和判断,如果相等就输出即可,代码我不写了
3,编写 快速排序的非递归算法用循环,和一个中间变量.终于编写出来了 , 我写了两种,你看看:下面是代码:/*非递归算法1递归算法的开销很大,所以在下编了一个非递归的,算法描述如下:A non-recursive version of quick sort using stack:  There are 2 stacks, namely one which stores the start of a subarray and the other which stores the end of the subarray. STEP 1: while the subarray contains more than one element ,i.e. from Do {  SUBSTEP 1. pivot=Partion(subarray);  SUBSTEP 2. keep track of the right half of the current subarray i.e. push (pivot+1) into stackFrom, push (to) into stackTo  SUBSTEP 3. go on to deal with the left half of the current subarray i.e. to=pivot-1  } STEP 2: if(neither of the stacks is empty)  Get a new subarray to deal with from the stacks.  i.e. start=pop(stackFrom); to=pop(stackTo); STEP 3: both stacks are empty, and array has been sorted. The program ends. */*/void UnrecQuicksort(int q[],int low,int high){stack s1;stacks2; s1.push(low); s2.push(high); int tl,th,p; while(!s1.empty() && !s2.empty()) {tl=s1.top();th=s2.top(); s1.pop();s2.pop(); if(tl>=th) continue; p=partition(q,tl,th); s1.push(tl);s1.push(p+1); s2.push(p-1);s2.push(th); }}/*非递归算法2要把递归算法改写成非递归算法 , 可引进一个栈,这个栈的大小取决于递归调用的深度 , 最多不会超过n,如果每次都选较大的部分进栈,处理较短的部分,递归深度最多不超过log2n,也就是说快速排序需要的附加存储开销为O(log2n) 。*/void UnrecQuicksort2(int q[],int low,int high){int *a; int top=0,i,j,p; a=new int[high-low+1]; if(a==NULL) return; a[top++]=low; a[top++]=high; while(top>0) {i=a[--top]; j=a[--top]; while(j {p=partition(q,j,i); if(p-j {//先分割前部,后部进栈a[top++]=p+1; a[top++]=i; i=p-1; } else {//先分割后部,前部进栈a[top++]=j; a[top++]=p-1; j=p+1; } } }}/*打印输出*/void display(int p[],int len){for(int i=0;i cout<}/*测试*/int _tmain(int argc, _TCHAR* argv[]){int a[]={49,65,97,12,23,41,56,14};quicksort(a,0,7);//UnrecQuicksort(a,0,7); //UnrecQuicksort2(a,0,7);display(a,8);return 0;}正好以前写过,现在找出来贴上#define Maxsize 50000void quicksort(int a[],int n){int i,j,low,high,temp,top=-1; struct node {int low,high; } st[Maxsize]; top++; st[top].low=0;st[top].high=n-1; while(top>-1) {low=st[top].low;high=st[top].high;top--;i=low;j=high;if(lowtemp)j--; if(iquicksort没有必要费递归的吧【非递归实现快拍,c中如何利用递归和非递归实现公式Pnx2n1xPn1xn1Pn】
4,求快速排序的非递归实现代码bool exchange(int array[], int begin, int end, int &pos)int pos_end = end;int pos_begin = begin;pos = begin;if (begin >= end)return false;while(pos_begin < pos_end);while (array[pos_end] < array[pos]) pos_end--;if (pos_end > pos)array[pos_end] = array[pos] + array[pos_end];array[pos] = array[pos_end] - array[pos];array[pos_end] = array[pos_end] - array[pos];pos = pos_end;} while (array[pos_begin] > array[pos]) pos_begin++;if (pos_begin < pos)array[pos_begin] = array[pos] + array[pos_begin];array[pos] = array[pos_begin] - array[pos];array[pos_begin] = array[pos_begin] - array[pos];pos = pos_begin;}}return true;}struct Queueint begin;int end;struct Queue *next;};void QSort(int array[], int length)struct Queue *head = (struct Queue *)malloc(sizeof(struct Queue));struct Queue *tail = head;int begin;int end;int pos;head->begin = begin;head->end = length - 1;while (head != NULL)struct Queue *tmp = head;begin = head->begin;end = head->end;head->next = NULL;if (exchange(array[], begin, end, pos))tmp = (struct Queue *)malloc(sizeof(struct Queue));tmp->begin = begin;tmp->end = pos - 1;tmp->next = NULL;tail->next = tmp;tail = tail->next;tmp = (struct Queue *)malloc(sizeof(struct Queue));tmp->begin = pos + 1;tmp->end = end;tmp->next = NULL;tail->next = tmp;tail = tail->next;tmp = head;head = tmp->next;free(tmp);}}}public sub quicksort(byref astrsort() as string, byval lngleft as long, byval lngright as long) dim i as long dim j as long dim temp as string i = lngleft j = lngright temp = astrsort(i)nextstep: do until i >= j while (astrsort(j) > temp and j > i) j = j - 1 end while if j > i then astrsort(i) = astrsort(j) astrsort(j) = temp i = i + 1 end if while (astrsort(i) < temp and j > i) i = i + 1 end while if j > i then astrsort(j) = astrsort(i) astrsort(i) = temp j = j - 1 end if loop if lngleft < i - 1 then quicksort(astrsort, lngleft, i - 1) if lngright > i + 1 then quicksort(astrsort, i + 1, lngright) end sub将数组的第10到20个元素用快速算法递归排序quicksort(a, 10, 20)

    推荐阅读