欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单的几种算法

程序员文章站 2022-05-13 17:43:57
...

1.冒泡算法

void bubble_sort(int a[], const int size)
{
        bool flag = true;
        int temp = 0; /* Temporary value for swapping two elements */
 
        for (int i = 0; i < size - 1; i ++)
        {
                flag = true;
                for (int j = 0; j < size - i - 1; j ++)
                {
                        if (a[j] > a[j + 1])
                        {
                                temp = a[j];
                                a[j] = a[j + 1];
                                a[j + 1] = temp;
                                flag = false;
                        } // end if
                } // end for j = ...
 
                if (flag == true)
                        break;
 
        } // end for i = ...
}

 

2.二叉树算法

3.递归算法

private static int sum(int num) {
if(num==1){
return 1;
}
else{
return num+sum(num-1);
}
}
}