冒泡排序
package com. bz. demo ;
import java. util. Arrays ;
public class BubbleSort {
public static void main ( String [ ] args) {
int [ ] array = { 1 , 2 , 6 , 3 , 7 , 10 , 5 , 8 , 50 } ;
BubbleSort bsort = new BubbleSort ( ) ;
bsort. bSort ( array) ;
System . out. println ( Arrays . toString ( array) ) ;
}
public int [ ] bSort ( int [ ] array) {
int temp = 0 ;
for ( int i= 0 ; i< array. length - 1 ; i++ ) {
for ( int j= 0 ; j< array. length - 1 - i; j++ ) {
if ( array[ j] > array[ j+ 1 ] ) {
temp = array[ j] ;
array[ j] = array[ j+ 1 ] ;
array[ j+ 1 ] = temp;
}
}
}
return array;
}
}
九九乘法表
package com. bz. demo ;
public class Test03 {
public static void main ( String [ ] args) {
for ( int i= 1 ; i<= 9 ; i++ ) {
for ( int j= 1 ; j<= i; j++ ) {
if ( j == i) {
System . out. print ( j + " * " + i + " = " + ( i * j) + "\n" ) ;
} else {
System . out. print ( j + " * " + i + " = " + ( i * j) + "\t" ) ;
}
}
}
}
}
打印三角形
package com. bz. demo ;
public class Test04 {
public static void main ( String [ ] args) {
int num = 5 ;
for ( int i= 1 ; i<= num; i++ ) {
for ( int j= num- i; j>= 1 ; j-- ) {
System . out. print ( " " ) ;
}
for ( int j= 1 ; j<= i; j++ ) {
System . out. print ( "*" ) ;
}
for ( int j= 1 ; j< i; j++ ) {
System . out. print ( "*" ) ;
}
System . out. println ( ) ;
}
}
}
递归算法
package com. bz. demo ;
public class Demo03 {
public static void main ( String [ ] args) {
Demo03 d = new Demo03 ( ) ;
int no = d. test ( 5 ) ;
System . out. println ( no) ;
}
public int test ( int num) {
if ( num == 1 ) {
return 1 ;
} else {
return num * test ( num - 1 ) ;
}
}
}