1. 冒泡排序
public static void main(String[] args) {
int m;
int a[] = { 10, 4, 3, 6, 2 };
for (int i = 0; i < a.length; ++i) {
for (int j = 0; j < a.length - i - 1; ++j) {
if (a[j] > a[j + 1]) {
m = a[j];
a[j] = a[j + 1];
a[j + 1] = m;
}
}
}
for (int n = 0; n < a.length; n++) {
System.out.print(a[n] + ",");
}
}
}
2. 递归
public static void main(String[] args) {
System.out.println(sum(5));
}
public static int sum(int num) {
if (num == 1) {
return 1;
} else {
return num + sum(num - 1);
}
}
3. 反转
public static void main(String[] args) {
reverse(12340567);
reverse(121314151);
}
public static void reverse(int a) {
int rs = 0;
while (a > 0) {
rs *= 10;
rs += a % 10;
a /= 10;
}
System.out.println(rs);
}
4. 快速排序
public class QuickSort{
public static void main(String[] args) {
int[] max ={6,5,2,9,7,4,0} ;
quickSort(max,0,max.length) ;
System.out.println(Arrays.toString(max)) ;
}
public static void quickSort(int[] max,int left,int right)
{
int i ,j ;
int middle,temp ;
i = left ;
j = right ;
middle = max[left] ;
while(true)
{
while((++i)<right-1 && max[i]<middle);
while((--j)>left && max[j]>middle);
if(i>=j){ break ;}
temp = max[i] ;
max[i] = max[j] ;
max[j] = temp ;
}
max[left] = max[j] ;
max[j] = middle ;
if(left<j)
quickSort(max,left,j) ;
if(right>i)
quickSort(max,i,right) ;
}
}
5. 遍历二叉树
public class BinaryTreeTest {
public static void main(String args[]) {
BinaryTreeTest b = new BinaryTreeTest();
int data[] = { 12, 11, 34, 45, 67, 89, 56, 43, 22, 98 };
BinaryTree root = new BinaryTree(data[0]);
System.out.print("二叉树的中的数据: ");
for (int i = 1; i < data.length + 1; i++)
{
root.insertTree(root, data[i - 1]);
System.out.print(data[i - 1] + ";");
}
System.out.println(data[data.length - 1]);
int key = Integer.parseInt(args[0]);
if (b.searchkey(root, key))
{
System.out.println("找到了:" + key);
}
else
{
System.out.println("没有找到:" + key);
}
}
public boolean searchkey(BinaryTree root, int key)
{
boolean bl = false;
if (root == null)
{
bl = false;
return bl;
}
else if (root.data == key)
{
bl = true;
return bl;
}
else if (key >= root.data)
{
return searchkey(root.rightpoiter, key);
}
return searchkey(root.leftpoiter, key);
}
}
class BinaryTree
{
int data;
BinaryTree leftpoiter;
BinaryTree rightpoiter;
BinaryTree(int data)
{
this.data = data;
leftpoiter = null;
rightpoiter = null;
}
public void insertTree(BinaryTree root, int data)
{
if (data >= root.data)
{
if (root.rightpoiter == null)
{
root.rightpoiter = new BinaryTree(data);
} else {
insertTree(root.rightpoiter, data);
}
} else {
if (root.leftpoiter == null) {
root.leftpoiter = new BinaryTree(data);
} else {
insertTree(root.leftpoiter, data);
}
}
}
}
运行时输入:java BinaryTree *(任意查找的整数)
6. 单例模式
第一种形式
public class danli {
private danli(){}
private static final danli instance=new danli();
public static danli getInstance(){
return instance;
}
}
第二种形式
public class danli {
private danli(){}
private static danli instance=null;
public static synchronized danli getInstance(){
if(instance==null)
{
instance=new danli();
}
return instance;
}
}
7. 分页(SQL SERVER\ORACLE)
SQL
select top 5 * from user where id not in (select top 5 * from user )
ORACLE
SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM <= pageNum*pageSize) WHERE RN >(pageNum-1)*pageSize
8. 一对兔子,三个月后生一对兔子,然后在每个月生一对小兔子,计算,共有多少兔子
public static void main(String[] args) {
for(int i=1;i<=12;++i)
{
System.out.println("第"+i+"月"+caclulate(i));
}
}
public static int caclulate(int m) //通过递归来计算
{
if (m <= 2)
{
return 1;
}
return caclulate(m-1) + caclulate(m-2);
}
9. 广度优先便利
template <int max_size>
void Digraph<max_size> ::
breadth_first(void (*visit)(Vertex &)) const
/* Post: The function *visit has been performed at each vertex of the Digraph in breadth-first order.
Uses: Methods of class Queue. */
{
Queue q;
bool visited [max_size];
Vertex v, w, x;
for (all v in G) visited [v] = false;
for (all v in G)
if (!visited [v]) {
q.append (v);
while (!q.empty ( )){
q.retrieve (w);
if (!visited [w]) {
visited [w] = true; (*visit) (w);
for (all x adjacent to w) q.append (x); }
q.serve ( ); } }
}
数据结构算法
最新推荐文章于 2020-11-19 10:12:45 发布