顺序表上实现堆排序

 堆排序

1)定义顺序表的存储结构;

2)在顺序表上实现堆排序;

3)用大量的数据测试最好、最坏和平均情况下的排序速度。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
//定义顺序表的存储结构;
typedef struct {
	int key;//关键字项
	int otherinfo;//其他数据元素
}RedType;
typedef struct {
	RedType r[105];//r[0]闲置或用作哨兵单元
	int length;//顺序表表长
}SqList;//顺序表类型

typedef SqList HeapType;

void CreatSq(SqList &L) {
	printf("请输入数据个数:");
	scanf("%d", &L.length);
	printf("请输入%d个数据元素:", L.length);
	for (int i = 1; i <= L.length; i++)
		scanf("%d", &L.r[i].key);
}
void Print(SqList L) {
	printf("降序输出:");
	for (int i = 1; i <= L.length; i++)
		printf("%d ", L.r[i].key);
	printf("\n\n");
}

void HeapAdjust(HeapType &H, int s, int m) {
	//已知H.r[s..m]中记录的关键字除H.r[s].key之外均满足堆的定义
	//本函数调整H.r[s]的关键字,使H.r[s..m]成为一个大顶堆
	RedType rc = H.r[s];
	for (int j = 2 * s; j <= m; j *= 2) {      //沿key较大的孩子节点向下筛选
		if (j < m && H.r[j].key >= H.r[j + 1].key) ++j;//j为key较大的记录的下标
		if (rc.key < H.r[j].key) break;        //rc应插入在位置s上
		H.r[s] = H.r[j];
		s = j;
	}
	H.r[s] = rc;                               //插入
}

void HeapSort(HeapType &H) {
	//对顺序表H进行堆排序
	for (int i = H.length / 2; i > 0; i--)  //把H.r[1..H.length]建成大顶堆
		HeapAdjust(H, i, H.length);
	for (int i = H.length; i > 1; i--) {
		swap(H.r[1], H.r[i]);   //将堆顶记录和当前未经排序的子序列Hr[1..i]中最后一个记录相互交换
		HeapAdjust(H, 1, i - 1);//将H.r[1..i-1]重新调整为大顶堆
	}
}
int main()
{
	SqList L;
	printf("堆排序排序\n");
	CreatSq(L);
	HeapSort(L);
	Print(L);
	system("pause");
	return 0;
}



  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构试验堆排序MFC // HeapSortDlg.h : header file // #if !defined(AFX_HEAPSORTDLG_H__DA227A0F_D8D2_459E_A6AE_1F11F292DDDD__INCLUDED_) #define AFX_HEAPSORTDLG_H__DA227A0F_D8D2_459E_A6AE_1F11F292DDDD__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #define LIST_INIT_SIZE 100//线性表储存空间的初始分量 #define LISTINCREMENT 10//线性表储存空间的分配增量 #define OVERFLOW 0 #define OK 1 ///////////////////////////////////////////////////////////////////////////// // CHeapSortDlg dialog //定义顺序表 typedef int Status; //typedef int ElemType; typedef struct { int key; }ElemType; typedef struct { ElemType *elem;//"\r\n" int length; int listsize; }SqList; //堆采用顺序表储存表示 typedef SqList HeapType; class CHeapSortDlg : public CDialog { // Construction public: CHeapSortDlg(CWnd* pParent = NULL); // standard constructor void HeapSort(HeapType &H); void HeapAdjust(HeapType &H,int s,int m); // Dialog Data //{{AFX_DATA(CHeapSortDlg) enum { IDD = IDD_HEAPSORT_DIALOG }; CString m_input; CString m_output; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CHeapSortDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CHeapSortDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnInput(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_HEAPSORTDLG_H__DA227A0F_D8D2_459E_A6AE_1F11F292DDDD__INCLUDED_)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值