java的基础算法

Java排序算法的比较

import java.util.*; 
import java.io.*;

public class SortAlgorithm 

static Random rand = new Random();

void bubbleSort(int[] numlist) // 冒泡排序算法 

int temp; 
for(int j=1;j<numlist.length;j++) 
for(int i=0;i<numlist.length-j;i++) 
if(numlist>numlist[i+1]) 

temp = numlist[i+1]; 
numlist[i+1] = numlist; 
numlist = temp; 

}

void selectionSort (int[] numlist) //选择排序算法 

int temp; 
for(int i=0;i<numlist.length-1;i++) 
for(int j=i+1;j<numlist.length;j++) 
if(numlist>numlist[j]) 

temp = numlist[j]; 
numlist[j] = numlist; 
numlist = temp;


}

void insertSort (int[] numlist) //插入排序算法 

int temp,in,out; 
for(out=1;out<numlist.length;out++) 

temp=numlist[out]; 
in=out; 
while(in>0 && numlist[in-1]>=temp) 

numlist[in]=numlist[in-1]; 
--in; 

numlist[in]=temp; 
}

}

void display (int[] num) // 打印出排序结果 

for(int i = 0;i<num.length;i++) 
System.out.print(num+" "); 
System.out.println("");

}

static int pRand(int mod) // 生成随即数组 

return Math.abs(rand.nextInt())%mod;

}

public static void main(String args[])throws IOException 

SortAlgorithm sortAlgorithm = new SortAlgorithm(); 
int[] numList = new int[10];

for(int i = 0;i<numList.length;i++) 
numList = pRand(100); //调用pRand方法,把随即生成的数据输入到 
// 数组中

System.out.println("随即生成的数组是:"); 
// 打印出原数组, 
for(int j =0;j<numList.length;j++) 
System.out.print(numList[j]+" ");

System.out.println(""); 
long begin = System.currentTimeMillis(); //排序开始时间,调用系统的当前时间 
sortAlgorithm.bubbleSort(numList); //执行冒泡排序 
long end = System.currentTimeMillis(); //排序结束时间,调用系统当前时间 
System.out.println("冒泡排序用时为:" + (end-begin)); //排序用时 
System.out.println("排序后的数组为:"); 
sortAlgorithm.display(numList);

begin = System.currentTimeMillis(); 
sortAlgorithm.selectionSort(numList); 
end = System.currentTimeMillis(); 
System.out.println("选择排序用时为:" +(end-begin)); 
System.out.println("排序后的数组为:"); 
sortAlgorithm.display(numList);

begin = System.currentTimeMillis(); 
sortAlgorithm.insertSort(numList); 
end = System.currentTimeMillis(); 
System.out.println("插入排序用时为:" + (end-begin)); 
System.out.println("排序后的数组为:"); 
sortAlgorithm.display(numList);

}

}

题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。

static int[] bits = new int[] { 1, 2, 3, 4, 5 }; 
/** 
* @param args 
*/ 
public static void main(String[] args) { 
sort("", bits); 

private static void sort(String prefix, int[] a) { 
if (a.length == 1) { 
System.out.println(prefix + a[0]); 

for (int i = 0; i < a.length; i++) { 
sort(prefix + a, copy(a, i)); 


private static int[] copy(int[] a,int index){ 
int[] b = new int[a.length-1]; 
System.arraycopy(a, 0, b, 0, index); 
System.arraycopy(a, index+1, b, index, a.length-index-1); 
return b; 
}

**********************************************************************

基本思路: 
1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。 
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑: 
1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。 
2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果 
3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。 
采用二维数组定义图结构,最后的代码是: 
import java.util.Iterator; 
import java.util.TreeSet; 
public class TestQuestion { 
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"}; 
private int n = b.length; 
private boolean[] visited = new boolean[n]; 
private int[][] a = new int[n][n]; 
private String result = ""; 
private TreeSet set = new TreeSet(); 
public static void main(String[] args) { 
new TestQuestion().start(); 

private void start() { 
// Initial the map a[][] 
for (int i = 0; i < n; i++) { 
for (int j = 0; j < n; j++) { 
if (i == j) { 
a[j] = 0; 
} else { 
a[j] = 1; 



// 3 and 5 can not be the neighbor. 
a[3][5] = 0; 
a[5][3] = 0; 
// Begin to depth search. 
for (int i = 0; i < n; i++) { 
this.depthFirstSearch(i); 

// Print result treeset. 
Iterator it = set.iterator(); 
while (it.hasNext()) { 
String string = (String) it.next(); 
// "4" can not be the third position. 
if (string.indexOf("4") != 2) { 
System.out.println(string); 



private void depthFirstSearch(int startIndex) { 
visited[startIndex] = true; 
result = result + b[startIndex]; 
if (result.length() == n) { 
// Filt the duplicate value. 
set.add(result); 

for(int j = 0; j < n; j++) { 
if (a[startIndex][j] == 1 && visited[j] == false) { 
depthFirstSearch(j); 
} else { 
continue; 


// restore the result value and visited value after listing a node. 
result = result.substring(0, result.length() -1); 
visited[startIndex] = false; 

}

1.写一个方法,用一个for循环打印九九乘法表 
Java code 
/** 
* 打印九九乘法口诀表 
*/ 
public 
void nineNineMulitTable(){ 
for (int i = 
1,j = 
1; j <= 
9; i++) { 
System.out.print(i+"*"+j+"="+i*j+" 
"); 
if(i==j){ 
i=0; 
j++; 
System.out.println(); 


}

2.给定一个java.util.Date对象,如何转化为”2007-3-22 20:23:22”格式的字符串 
Java code 
/** 
* 将某个日期以固定格式转化成字符串 
* @param date 
* @return str 
*/ 
public String date2FormatStr(Date date) 

SimpleDateFormat sdf = 
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String str = sdf.format(date); 
return str; 
}

3.写一个方法,能够判断任意一个整数是否素数

/** 
* 判断任意一个整数是否素数 
* @param num 
* @return boolean 
*/ 
public boolean isPrimeNumber(int num) 

for (int i = 2; i <= Math.sqrt(num); i++) { 
if(num%i==0) 

return false; 


return true; 
}

4.写一个方法,输入任意一个整数,返回它的阶乘

Java code 
/** 
*获得任意一个整数的阶乘 
*@param n 
*@returnn
*/ 
public int factorial(int num) 

//递归 
if(num == 1) 

return 1; 

return num*factorial(num-1); 
}

5.写一个方法,用二分查找法判断任意整数在任意整数数组里面是否存在,若存在就返回它在数组中的索引位置,不存在返回-1

Java code 
/** 
*二分查找特定整数在整型数组中的位置(递归) 
*@param dataset 
*@param data 
*@param beginIndex 
*@param endIndex 
*@return index 
*/ 
public int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){ 
int midIndex = (beginIndex+endIndex)/2; 
//如果查找的数要比开始索引的数据要小或者是比结束索引的书要大,或者开始查找的索引值大于结束的索引值返回-1没有查到 
if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){ 
return -1; 

if(data <dataset[midIndex]){ 
return binarySearch(dataset,data,beginIndex,midIndex-1); 
}else if(data>dataset[midIndex]) 

return binarySearch(dataset,data,midIndex+1,endIndex); 
}else { 
return midIndex; 

}

/** 
*二分查找特定整数在整型数组中的位置(非递归) 
*@param dataset 
*@param data 
*@return index 
*/ 
public int binarySearch(int[] dataset ,int data) 

int beginIndex = 0; 
int endIndex = dataset.length - 1; 
int midIndex = -1; 
if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){ 
return -1; 

while(beginIndex <= endIndex) { 
midIndex = (beginIndex+endIndex)/2; 
if(data <dataset[midIndex]) { 
endIndex = midIndex-1; 
} else if(data>dataset[midIndex]) { 
beginIndex = midIndex+1; 
}else { 
return midIndex; 


return -1; 
}

原文地址:https://www.cnblogs.com/plas/p/9876696.html