Inductive method of algorithm series

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.