常见算法。

  太久没有打代码,发现打起来特别的痛苦,几个小时也仅仅能打一点点,然后错误百出。果然还是要多打。

class  Sort
{ 
	  static void BubbleS( int []b){
	 int c; 
	  for (int i = 0;i<b.length-1;i++)
	  {
		  for (int j =0;j<b.length-1-i;j++)
		  {
			   if (b[j]>b[j+1]) 
				   {	c=b[j];b[j]=b[j+1];b[j+1]=c;
				   };
		  }

	  }
	 
}
	  static void SellectS(int[]b){

		  int c,q;
		  for (int i=0;i<b.length-1;i++)
		  {	
			  c=i;
			  for (int j=i+1;j<b.length;j++)
			  {
				  if (b[c]>b[j])	c=j; 
				
			  } 
			  q=b[c];b[c]=b[i];b[i]=q;
		  }
			 
		  
		  
		 
	  }
	  static void QuickS(int []b){
		  int a,q; 
		  a=b[b.length/2];
		  for (int m=0;m<b.length/2;)
		  {
			  for (int n=b.length-1;n>b.length/2;)
			  {
			  if  (b[m]>a)
		 {
				  if (b[n]<a)
				  {
					  q=b[m];b[m]=b[n];b[n]=q;
				  }
				  else n--;
		 }
			  else m++;
		 }
			  }
		 
	  }

	  static void InsertS(int []b){
		 
	
		  for(int i=1;i<b.length;i++)
	        {
	            int t = b[i];  int j=i-1;
	              
	            while(j>=0&&t<b[j])   // j>=0 要放在前面 
	            {	               
	                b[j+1]=b[j];
	                j--;
	            }
	            b[j+1]=t;
	        }
	    
		
	  }
}
  

  
 
public class CTest {
	 public static void main (String[] args)
	 {
	 int[] a1 ={10,40,4,5,6,9,11,2,5,1,7};  
	 int[] a2 ={10,40,4,5,6,9,11,2,5,1,7};  
	 int[] a3 ={10,40,4,5,6,9,11,2,5,1,7};  
	 int[] a4 ={10,40,4,5,6,9,11,2,5,1,7};
	 int[] a5 ={10,40,4,5,6,9,11,2,5,1,7};
	 
	 
	 
	 for (int x:a1)
	 System.out.print("     "+x);
	 System.out.println();
	 
	 Sort.BubbleS(a1);
	 for (int x:a1)
		 System.out.print("    "+x);
	 System.out.println();
	 
	  Sort.SellectS(a3);
	 for (int x:a3)
		 System.out.print("    "+x);
	 
	 System.out.println();
	 Sort.QuickS(a4);
	 for (int z=0;z<a4.length;z++)
	 {  
		 System.out.print("     "+a4[z]) ; 
		
	 }
	 System.out.println();
	 Sort.InsertS(a5);
	 for (int x :a5)
		 System.out.print("     " + x );
	 }
}

还没有把所有常见算法都打一遍,还差几个,慢慢补齐。过程中发现的问题:

1、数组操作还不是很清楚,得补。

2、唯一的注释那里 报错是 超过数组下标,有待解决。

3、代码略乱...

4、最开始的方法是想返回数组的,不知道功能有什么影响,不过那代码看起来感觉怪怪的,已改过。得补。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值