数据结构算法

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 ( ); } } 
  }

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值