1, 冒泡排序算法 (递归)
public class 冒泡排序 {
- public static void main(String args[]){
- int[] array = {4,3,56,73,25,6,37,8,1,46};
- int n = array.length;
- BubbleSort(array, n, 0);
- for(int i=0;i<n;i++){
- System.out.println(array[i]);
- }
- }
- private static void BubbleSort(int array[],int n,int i){
- if(i == n-1) return;
- else{
- boolean exchange = false;
- for(int j=n-1;j>i;j--){
- if(array[j] < array[j-1]){
- int temp = array[j];
- array[j] = array[j-1];
- array[j-1] = temp;
- exchange = true;
- }
- }
- if(exchange==false) return;
- else {
- BubbleSort(array, n, i+1);
- }
- }
- }
- }
冒泡排序
2, 斐波那契数
public class 斐波那契数 {
- public static void main(String args[]){
- int n = 9;
- System.out.println(Fib(n));
- }
- private static int Fib(int n){
- if(n==1 || n==2){
- return 1;
- }else{
- return Fib(n-1)+Fib(n-2);
- }
- }
- }
斐波那契数
3, 求数组中最大值
public class 求数组中最大值 {
- public static void main(String args[]){
- int a[]={1,2,3,4,5};
- System.out.println(fmax(a, 5));
- }
- private static int fmax(int a[],int i){
- if(i==1){
- return a[0];
- }else{
- return Math.max(fmax(a, i-1),a[i-1]);
- }
- }
- }
求数组中最大值
4, 整数逆序输出
public class 整数逆序输出 {
- public static void main(String args[]){
- int a = 12345;
- digits(a);
- }
- private static void digits(int n){
- if(n!=0){
- System.out.print(n%10);
- digits(n/10);
- }
- }
- }
整数逆序输出
来源: http://www.bubuko.com/infodetail-3481116.html