递归
int sum = getSum ( 100 ) ;
System. out. println ( sum) ;
}
public static int getSum ( int i) {
if ( i== 1 ) {
return 1 ;
} else {
return i+ getSum ( i- 1 ) ;
}
}
int result= getSum ( 5 ) ;
System. out. println ( result) ;
}
public static int getSum ( int i) {
if ( i== 1 ) {
return 1 ;
} else {
return i* getSum ( i- 1 ) ;
}
}
public static void main ( String[ ] args) {
int result= getCount ( 8 ) ;
System. out. println ( result) ;
}
public static int getCount ( int n) {
if ( n== 0 || n== 1 ) {
return 1 ;
} else {
return getCount ( n- 1 ) + getCount ( n- 2 ) ;
}
}
利用递归快速排序
public static void main ( String[ ] args) {
int [ ] arr = {
6 , 3 , 5 , 8 , 9 , 1 , 2 , 4 , 10 , 7 } ;
quickSort ( arr, 0 , arr. length- 1 ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
System. out. println ( arr[ i] ) ;
}
}
private static void quickSort ( int [ ] arr, int left, int right) {
if ( left> right) {
return ;
}
int left0 = left;
int right0= right;
int v= arr[ left0] ;
while ( left< right) {
while ( arr[ right] >= v&& left< right) {
right-- ;
}
while ( arr[ left] <= v&& left< right) {
left++ ;
}
swap ( arr<