用c语言手写堆排序以及来道简单题吧 1046. 最后一块石头的重量

堆排序手写一遍吧

#include<stdio.h>
#include<stdlib.h>
void swap(int *a,int *b)
{
	int temp=*a;
	*a=*b;
	*b=temp;
 } 
void HeapAdjust(int *heap,int i,int size)
{
	int lchild=2*i+1;//左孩子序号 
	int rchild=2*i+2;//右孩子序号
	int temp =i;
	if(i<=size/2-1)
	{
		if(lchild<=size-1&&heap[lchild]>heap[temp])
		{
			temp=lchild;
		}
		if(rchild<=size-1&&heap[rchild]>heap[temp])
		{
			temp=rchild;
		}
		if(temp!=i)
		{
			swap(&heap[i],&heap[temp]);
			HeapAdjust(heap,temp,size);//避免交换后的子树不满足堆 
		}
	}	
} 

void BuildHeap(int *heap,int size)
{
	int i;
	for(i=size/2-1;i>=0;i--)
	{
		HeapAdjust(heap,i,size);
	}	
}

void HeapSort(int *heap,int size)
{
	int i;
	BuildHeap(heap,size);
	for(i=size-1;i>=0;i--)
	{
		swap(&heap[0],&heap[i]);//每次将最大的丢在数组末尾,实现排序 
		HeapAdjust(heap,0,i-1);//将剩余结点重新调整成大顶堆 
	}
}

Leetcode例题 1046. 最后一块石头的重量

在这里插入图片描述

解法

void swap(int *a,int *b)
{
	int temp=*a;
	*a=*b;
	*b=temp;
} 
void HeapAdjust(int *heap, int i,int size)
{

    int lchild=2*i+1;//左孩子序号 
	int rchild=2*i+2;//右孩子序号
	int temp =i;
	if(i<=size/2-1)
	{
		if(lchild<=size-1&&heap[lchild]>heap[temp])
		{
			temp=lchild;
		}
		if(rchild<=size-1&&heap[rchild]>heap[temp])
		{
			temp=rchild;
		}
		if(temp!=i)
		{
			swap(&heap[i],&heap[temp]);
			HeapAdjust(heap,temp,size);//避免交换后的子树不满足堆 
		}
	}	
}

void buildHeap(int *heap, int size) {
    for (int i=size-1; i>=0; --i) {
        HeapAdjust(heap, i, size);
    }
}

int fetch(int *heap, int *size) {
    int x = heap[0];
    heap[0] = heap[--(*size)];
    HeapAdjust(heap, 0, *size);
    return x;
}

void push(int *heap, int *size, int x) {
    heap[(*size)++] = x;
    HeapAdjust(heap, *size-1, *size);
}

int lastStoneWeight(int* stones, int stonesSize){
    buildHeap(stones, stonesSize);
    while (stonesSize > 1) {
        int x = fetch(stones, &stonesSize);
        int y = fetch(stones, &stonesSize);
        if (x == y) {
            // do nothing
        } else {
            int t = abs(x-y);
            push(stones, &stonesSize, t);
        }
    }
    return stonesSize ? stones[0] : 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
实验十二 排序技术实验 1. 实验目的 1. 掌握各种排序算法的基本思想; 掌握各种排序算法的实现方法; 掌握各种排序算法的时间性能及其花费时间的计算。 掌握各种排序算法所适应的不同场合。 2. 实验内容 1、随机函数产生10000个随机数,用直接插入、希尔、冒泡、直接选择等排序方法 排序,并统计每一种排序所花费的时间。 2、随机函数产生30000个随机数,用快速、堆、归并等排序方法排序,并统计每一种 排序所花费的时间。 3. 设计与编码 #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; void ShuChu(int r[],int n) { cout<<"输出:"; for(int i=0;i<n;i++) cout<<r[i]<<"\t"; cout<<endl; cout<<"*****************************************"<<endl; } void InsertSort(int r[],int n) //插入排序 { int a=r[0]; for(int i=2;i<n;i++) { r[0]=r[i]; for(int j=i-1;r[0]<r[j];j--) r[j+1]=r[j]; r[j+1]=r[0]; } r[0]=a; } void ShellSort(int r[],int n) //希尔排序 { int a=r[0]; for(int d=n/2;d>=1;d=d/2) { for(int i=d+1;i<=n-1;i++) { r[0]=r[i]; for(int j=i-d;j>0 && r[0]<r[j];j=j-d) r[j+d]=r[j]; r[j+d]=r[0]; } } r[0]=a; } void BubbleSort(int r[],int n) //冒泡排序 { int exchange=n-1; while(exchange!=0) { int bound=exchange;exchange=0; for(int j=1;j<bound;j++) if(r[j]>r[j+1]) { int s=r[j+1]; r[j+1]=r[j]; r[j]=s; exchange=j; } } } void SelectSort(int r[],int n) //选择排序 { for(int i=0;i<n-1;i++) { int index=i; for(int j=i+1;j<=n-1;j++) if(r[j]<r[index]) index=j; if(index!=i) { int a=r[index]; r[index]=r[i]; r[i]=a; } } ShuChu(r,n); } int Partition(int r[],int first,int end) //快速排序一次划分算法 { int i=first; int j=end; r[0]=r[i]; int p=r[i]; while(i<j) { while(i<j&&r[i]<=r[j])j--; if(i<j) { int a=r[j]; r[j]=r[i]; r[i]=a; i++; } while(i<j&&r[i]<=r[j])i++; if(i<j) { int b=r[j]; r[j]=r[i]; r[i]=b; j--; } } return i; } void QuickSort(int r[],int first,int end) //快速排序算法 { int a=r[0]; if(first<end) { int pivot=Partition(r,first,end); QuickSort(r,first,pivot-1); QuickSort(r,pivot+1,end); } r[0]=a; } void Sift(int r[],int k,int m) //筛选法调整堆的算法 { int i=k; int j=2*i; while(j<=m) { if(j<m&&r[j]<r[j+1])j++; if(r[i]>=r[j])break; else { int a=r[j]; r[j]=r[i]; r[i]=a; i=j; j=2*i; } } } void HeapSort(int r[],int n) //堆排序算法 { for(int i=n/2;i>=1;i--) Sift(r,i,n-1); for(i=2;i<n-1;i++) { int a=r[n-i+1]; r[n-i+1]=r[1]; r[1]=a; Sift(r,1

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值