递归
1.求n的阶乘递归算法:
public class Facterial {
public static int f(int n){
if(n == 1) return 1;
else{
return f(n-1)*n;
}
}
public static void main(String[] args) {
System.out.println(f(4));
}
}
求n的阶乘非递归算法:
public class Facterial {
public static int f(int n){
if(n == 1) return 1;
int fac = 1;
for(int i = 2; i<=n; i++){
fac = fac*i;
}
return fac;
}
public static void main(String[] args) {
System.out.println(f(5));
}
}
2.求菲波那契数列(递归):
public class Fibonacci {
public static int fibonacci(int n){
if(n<=2) {
return 1;
}
else return fibonacci(n-1)+fibonacci(n-2);
}
public static void main(String[] args) {
for(int i=1; i<10; i++){
System.out.print(fibonacci(i)+",");
}
}
(非递归的):
public class Fibonacci1 {
/**
* 非递归求Fibonacci
*/
public static int fibonacci(int n){
if(n<=2) return 1;
int n1=1, n2=1, sum=0;
for(int i=0; i<n-2; i++){
sum = n1+n2;
n1 = n2;
n2 = sum;
}
return sum;
}
public static void main(String[] args) {
for(int i=1; i<10; i++){
System.out.print(fibonacci(i)+" ");
}
}
}
3.Hanoi塔问题:
public class Hanoi {
/**
* hanoi问题
*/
public static void hanoi(int n, char x, char y, char z){
if(n == 1) move(n, x, z);
else{
hanoi(n-1, x, z, y);
move(n, x, z);
hanoi(n-1, y, x, z);
}
}
public static void move(int n, char a, char b){
System.out.println("把直径为"+n+"cm的盘从"+a+"移动到"+b+"处");
}
public static void main(String[] args) {
hanoi(3,'x','y','z');
}
}
4.冒泡排序:
public class Maopao {
/**
* 冒泡排序
*/
public static void main(String[] args) {
int[] a = {3,7,4,5,10,9,8};
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-1-i; j++){
if(a[j]>a[j+1]){
int t = a[j+1];
a[j+1] = a[j];
a[j] = t;
}
}
}
for(int i=0; i<a.length; i++){
System.out.print(a[i]+" ");
}
}
}
5.快速排序:
public class QuickSort {
/*
一趟快速排序的具体做法:先设两个位置的值low和high,设枢轴记录的关键字为pivotkey,则首先
从high所在的位置开始向前搜索找到第一个关键字小于pivotkey的记录和枢轴记录互相交换,然后
从low所在的位置起向后搜索,找到第一个关键字大于pivotkey的记录和枢轴记录互相交换,重复这
两步直至low=high为止
*/
private int partition(int[] list, int low, int high ){
int pivotkey = list[low];
while(low<high){
while(low<high && list[high]>pivotkey) --high;
list[low] = list[high];
while(low<high && list[low]<pivotkey) ++low;
list[high] = list[low];
}
list[low] = pivotkey;
return low;
}
//递归快排
private void qSort(int[] list, int low, int high){
if(low<high){
int mid = partition(list, low, high);
qSort(list, low, mid-1); //向低字表递归排序,mid是枢轴位置
qSort(list, mid+1, high); //向高子表递归排序
}
}
public void quickSort(int[] list){
if(list.length > 0){
qSort(list, 0 , list.length-1);
}
}
}
1.求n的阶乘递归算法:
public class Facterial {
public static int f(int n){
if(n == 1) return 1;
else{
return f(n-1)*n;
}
}
public static void main(String[] args) {
System.out.println(f(4));
}
}
求n的阶乘非递归算法:
public class Facterial {
public static int f(int n){
if(n == 1) return 1;
int fac = 1;
for(int i = 2; i<=n; i++){
fac = fac*i;
}
return fac;
}
public static void main(String[] args) {
System.out.println(f(5));
}
}
2.求菲波那契数列(递归):
public class Fibonacci {
public static int fibonacci(int n){
if(n<=2) {
return 1;
}
else return fibonacci(n-1)+fibonacci(n-2);
}
public static void main(String[] args) {
for(int i=1; i<10; i++){
System.out.print(fibonacci(i)+",");
}
}
(非递归的):
public class Fibonacci1 {
/**
* 非递归求Fibonacci
*/
public static int fibonacci(int n){
if(n<=2) return 1;
int n1=1, n2=1, sum=0;
for(int i=0; i<n-2; i++){
sum = n1+n2;
n1 = n2;
n2 = sum;
}
return sum;
}
public static void main(String[] args) {
for(int i=1; i<10; i++){
System.out.print(fibonacci(i)+" ");
}
}
}
3.Hanoi塔问题:
public class Hanoi {
/**
* hanoi问题
*/
public static void hanoi(int n, char x, char y, char z){
if(n == 1) move(n, x, z);
else{
hanoi(n-1, x, z, y);
move(n, x, z);
hanoi(n-1, y, x, z);
}
}
public static void move(int n, char a, char b){
System.out.println("把直径为"+n+"cm的盘从"+a+"移动到"+b+"处");
}
public static void main(String[] args) {
hanoi(3,'x','y','z');
}
}
4.冒泡排序:
public class Maopao {
/**
* 冒泡排序
*/
public static void main(String[] args) {
int[] a = {3,7,4,5,10,9,8};
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-1-i; j++){
if(a[j]>a[j+1]){
int t = a[j+1];
a[j+1] = a[j];
a[j] = t;
}
}
}
for(int i=0; i<a.length; i++){
System.out.print(a[i]+" ");
}
}
}
5.快速排序:
public class QuickSort {
/*
一趟快速排序的具体做法:先设两个位置的值low和high,设枢轴记录的关键字为pivotkey,则首先
从high所在的位置开始向前搜索找到第一个关键字小于pivotkey的记录和枢轴记录互相交换,然后
从low所在的位置起向后搜索,找到第一个关键字大于pivotkey的记录和枢轴记录互相交换,重复这
两步直至low=high为止
*/
private int partition(int[] list, int low, int high ){
int pivotkey = list[low];
while(low<high){
while(low<high && list[high]>pivotkey) --high;
list[low] = list[high];
while(low<high && list[low]<pivotkey) ++low;
list[high] = list[low];
}
list[low] = pivotkey;
return low;
}
//递归快排
private void qSort(int[] list, int low, int high){
if(low<high){
int mid = partition(list, low, high);
qSort(list, low, mid-1); //向低字表递归排序,mid是枢轴位置
qSort(list, mid+1, high); //向高子表递归排序
}
}
public void quickSort(int[] list){
if(list.length > 0){
qSort(list, 0 , list.length-1);
}
}
}