java 坦克大战

#include
    
    
     
     
#include
     
     
      
      
using namespace std;
void print(int *a) { int k; for (k = 0; k < 10; k++) cout << a[k] << " "; cout << endl; }
void shellinsert(int *a, int n, int d) //间隔d进行排序
{
	for (int i = d; i < n; i++) //第d+1个数
	{
		int x = a[i];   //记录
		int j = i;
		while (j >= d&&x < a[j - d]) //如第i-d个数比较,如果大于,就不用排了直接插入;如果小于,a[j-d]后移,继续往前找,直到在合适的地方插入;
		{			
			a[j] = a[j - d];
			j = j - d;
		}
		a[j] = x;//插入
		print(a);
	}

}
void shell(int* a, int n) //shell插入排序
{
	int d = n/2;
	while (d>=1)
	{
		shellinsert(a, n, d);
		print(a);
	
		d /= 2;
	}
}
void main()
{
	int a[] = { 49,38,65,97,76,13,24,49,55,04 };
	
	print(a);
	
	shell(a, 10); //shell排序
	shellinsert(a, 10, 1); //d=1时的shell插入就是简单的插入排序
	

}#include
      
      
       
       
#include
       
       
        
        
using namespace std;
const int N = 10;
void print(int *a) { int k; for (k = 0; k < N; k++) cout << a[k] << " "; cout << endl; }
void heapadjust(int *a, int location, int length) //调整堆
{
	int child = 2 * location + 1;
	while (child
        
        
          a[location])//如果孩子更大 { int temp = a[child]; a[child] = a[location]; a[location] = temp; //交换child和location的元素 } else break;//如果爸爸更大,就不用调整了 location = child; //下移 child = 2 * child + 1; } } void buildheap(int *a, int length) //建立堆 { int i = (length - 1) / 2;//最后一个有孩子的节点开始,调整堆 for (i; i >=0; i--) // { heapadjust(a,i, length); } } void heapsort(int* a, int length) //堆排序 { buildheap(a, length);//建立堆 print(a); while (length>0) { int temp = a[0]; a[0] = a[length - 1]; a[length - 1] = temp; //将堆顶元素删除,移动到最后 heapadjust(a, 0, --length); print(a); } } void main() { int a[] = { 49,38,65,97,76,13,24,49,55,04 }; print(a); heapsort(a, N); print(a); } 
        
       
       
      
      
     
     
    
    


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值