Select Sort:
#include <iostream>using namespace std; #define LENGTH 10int data[length];void selectsort (int i,int num) { if (i < num) { int k = i; for (int j = i+1;j <= num; j + +) if (Data[j]<data[k]) k = j; if (k!=i) { int temp = data[i]; Data[i] = data[k]; DATA[K] = temp; } Selectsort (I+1,num); }} int main () { int num; cin>>num; for (int i = 1;i <= num; i++) cin>>data[i]; Selectsort (1,num); for (int i = 1;i <= num; i++) cout<<data[i]<< ""; cout<<endl; return 0;}
Insert Sort:
#include <iostream>using namespace std; #define LENGTH 10int data[length];void insertsort (int i) { if (i > 1) c1/>{ int x = data[i]; Insertsort (i-1); int j = i-1; while (j>0&&data[j]>x) { data[j+1] = data[j]; j = j-1; } DATA[J+1] = x; }} int main () { int num; cin>>num; for (int i = 1;i <= num; i++) cin>>data[i]; Insertsort (num); for (int i = 1;i <= num; i++) cout<<data[i]<< ""; cout<<endl; return 0;}
Integer power
Usually for the n power of X, a straightforward method is to squared n times for X in an iterative way, which is very inefficient. An efficient method can be introduced in the following way, so that M = [N/2], assuming already know how to calculate the M power of X. Then there are two kinds of situations: if n is an even number, then the n power of x equals the square of the M power of X, otherwise the n power of x is equal to the square of x multiplied by the M power of X. This idea can produce a recursive algorithm as shown in Exprec.
#include <iostream>using namespace Std;int power (int x,int m) { int y; if (m==0) y = 1; else { y = power (X,M/2); y = y*y; if (m%2==1) y = x*y; } return y;} int main () { int num1,num2; cin>>num1>>num2; Cout<<power (num1,num2) <<endl; return 0;}
Inductive method of algorithm series