⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compositordlg.cpp

📁 实现了插入排序
💻 CPP
字号:
// CompositorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Compositor.h"
#include "CompositorDlg.h"
#include<time.h>
#include<stdlib.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompositorDlg dialog

CCompositorDlg::CCompositorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCompositorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCompositorDlg)
	m_compare = 0.0;
	m_exchange = 0.0;
	m_num = 0;
	m_time = 0.0;
	m_move = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCompositorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCompositorDlg)
	DDX_Text(pDX, IDC_bijiao, m_compare);
	DDX_Text(pDX, IDC_jiaohuan, m_exchange);
	DDX_Text(pDX, IDC_number, m_num);
	DDV_MinMaxLong(pDX, m_num, 0, 30000);
	DDX_Text(pDX, IDC_time, m_time);
	DDX_Text(pDX, IDC_yidong, m_move);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCompositorDlg, CDialog)
	//{{AFX_MSG_MAP(CCompositorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_OK, OnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void paixu::insertsort(int r[],long m,long n)
{	compare=0;
	move=0;
	exchange=0;
  start=clock();
   long x,i,j;
  for(i=m+1;i<=n;i++)
  {
	  x=r[i];
	  j=i-1;
      compare++;
	  while(j>=m&&x<r[j])
	  {
		  move++;
		  compare++;
		  r[j+1]=r[j];
		  j--;
	  }
	  r[j+1]=x;
  }
   end=clock();
   time=end-start;
  return ;
}

//////////////////////////////////////////////////////////////////////////
//希尔排序
void paixu::shellsort(int r[],long m,long n)
{
	compare=0;
	move=0;
	exchange=0;
	start=clock();
	long i,j,gap;
	int temp;             //temp为临时变量
	gap=(n-m+1)/2;              //增量的初值为n/2
	while(gap>0)
	{
		for(i=gap+1;i<=n;i++)
		{
			j=i-gap;
			while(j>m-1)
			{   
				compare++;
				if(r[j]>r[j+gap])   //交换r[j]和r[j+gap]
				{
					temp=r[j];
					r[j]=r[j+gap];
					r[j+gap]=temp;
					exchange++;
					j=j-gap;
				}
				else j=m-1;     //通过给j赋值而退出while循环
			}
		}
		gap=gap/2;              //减小增量
	}
	 end=clock();
   time=end-start;
}
///////////////////////////////////////////////////////////////////
//冒泡排序
void paixu::bubblesort(int r[],long m,long n)
{
	compare=0;
	move=0;
	exchange=0;
    start=clock();
	long i,j,exchanges;
	int temp;
	for(i=m;i<=n-1;i++)
	{
		exchanges=0;
		for(j=n;j>=i+1;j--)
		{
			compare++;
			if(r[j]<r[j-1])    //比较
			{
				temp=r[j];
				r[j]=r[j-1];  //交换
				r[j-1]=temp;
				exchanges=1;
				exchange++;
			}
		}
        if(exchanges==0)  //本趟无交换发生,则结束
		{ end=clock();
            time=end-start;
			return;
		}
	}
	 end=clock();
   time=end-start;
}
////////////////////////////////////////////////////////////////
//快速排序
void paixu::quick(int r[],long s,long t)
{
	long i=s,j=t;
	if(s<t)
	{
		r[0]=r[s];
		do                                 //以r[0]为基准将r分成两部分
		{   
			compare++;
			while(j>i&&r[j]>r[0])         //从右向左找大于基准的纪录r[j]
			{
				compare++;
				j--;
			}
			if(i<j)
			{
				r[i]=r[j];
				move++;
				i++;
			}
			compare++;
			while(i<j&&r[i]<r[0])        //从左向右找大于基准的纪录r[i]
			{
				compare++;
				i++;
			}
			if(i<j)
			{
				move++;
				r[j]=r[i];
				j--;
			}
		}while(i<j);
		r[i]=r[0];
		quick(r,s,j-1);
		quick(r,j+1,t);
	}
}
void paixu::quicksort(int r[],long s,long t)
{
	compare=0;
	move=0;
	exchange=0;
	start=clock();
	quick(r,s,t);
	 end=clock();
   time=end-start;
}

///////////////////////////////////////////////////////////////////
//选择排序
void paixu::selectsort(int r[],long m,long n)
{
	compare=0;
	move=0;
	exchange=0;
	start=clock();
	long i,j,k;
	int temp;
	for(i=m;i<=n-1;i++)
	{
		k=i;
		for(j=i+1;j<=n;j++)
		{
			compare++;
			if(r[j]<r[k])
				k=j;                          //用k指出每趟在无序区段的最小元素
		}
		    exchange++;
			temp=r[i];                           //将r[k]和r[i]交换 
			r[i]=r[k];
			r[k]=temp;
	}
	 end=clock();
   time=end-start;
}
//////////////////////////////////////////////////////////////////
//堆排序***********
void paixu::sift(int r[],long l,long m)
{
	long i,j;
	int x;
	i=l;
	j=2*i;                       //r[j]是r[i]的左孩子
	x=r[i];
	while(j<m)
	{
		compare++;
		if(j<m&&r[j]<r[j+1])    //若右孩子较大,则把j修改为右孩子的下标
			j++;
		compare++;
		if(x<r[j])
        {
			move++;
			r[i]=r[j];        //将r[j]调到父亲的位置上
			i=j;             //修改i和j的值,以便继续往下筛
			j=2*i;
		}
        else j=m+1;         //筛运算完成,令j=m+1,以便终止循环
	}
	r[i]=x;                    //被筛节点的值放入最终位置
}
void paixu::heapsort(int r[],long n)
{
	compare=0;
	move=0;
	exchange=0;
	start=clock();
	long i;
	int m;
	for(i=n/2;i>=1;i--)      //建立初始堆
		sift(r,i,n);
	for(i=n;i>=2;i--)       //进行n-1次循环,完成堆排序
	{
		m=r[i];            //将第一个元素同当前区间内最后一个元素对换
		r[i]=r[1];
		r[1]=m;
		exchange++;
		sift(r,1,i-1);  //筛r[1]节点,得到i-1个节点的堆
	}
	 end=clock();
   time=end-start;
}
/////////////////////////////////////////////////////////////////////////
//二路归并排序
void paixu::merge(int a[],long s,long m,long n,int b[])
{
  long i,j,k;
  i=s;j=m+1;k=0;  //k为b的指示器,i,j分别为s1,s2的指示器
  while(i<=m&&j<=n)
  {
	  compare++;
	  move++;
	  if(a[i]<=a[j])b[k++]=a[i++];
	  else b[k++]=a[j++];
  }
  while(i<=m){b[k++]=a[i++];move++;}   
  while(j<=n){b[k++]=a[j++];move++;}   
	  return;
}
//////////////////////////////////////////////////////////
//归并排序
void paixu::mmergesort(int a[],long p1,long p2,long len,int b[])
{//多段二路合并:r[p1]~r[p2]中,每len个元素为一个有序段,将从头起的每个连续的
	long i,j,k;
	i=p1;
	k=0;
	while(i+2*len-1<=p2)
	{//当从i起的后面有完整的两段时,合并之
		merge(a,i,i+len-1,i+2*len-1,b+k);
	    i+=2*len;
		k+=2*len;
	}
	if(i+len<=p2)//剩两段,但最后段的段长不足len
		merge(a,i,i+len-1,p2,b+k);
	else     //剩一段,或不够
		for(j=i;j<=p2;j++)
		{
			b[k++]=a[j];
			move++;
		}
}
void paixu::sortmerge(int a[],long p1,long p2,int b[])
{//二路归并排序,将a[p1]~a[p2]中的元素排序,结果在b[0]~b[p2-p1]中
	compare=0;
	move=0;
	exchange=0;	
	start=clock();
	long len;
	len=1;
	while(len<p2-p1+1)
	{
		mmergesort(a,p1,p2,len,b);//将a[p1]~a[p2]中的长度为len的相邻有序段两两合并到b[0]~[p2-p1]
		len=len*2;
		mmergesort(b,0,p2-p1,len,a+p1);//将b[0]~b[p2-p1]中长度为len的相邻有序段两两合并到a[p1]~a[p2]
	}
	end=clock();
   time=end-start;
}
/////////////////////////////////////////////////////////////////////////////
// CCompositorDlg message handlers

BOOL CCompositorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCompositorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCompositorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCompositorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCompositorDlg::OnButton1() 
{  
	for(int i=1;i<=m_num;i++)
		b[i]=a[i];
    paixu ab;
	ab.insertsort(b,1,m_num);
	UpdateData(TRUE);
	m_compare=ab.compare;
	m_move=ab.move;
	m_exchange=ab.exchange;
	m_time=ab.time;
    UpdateData(FALSE);

	
	
}

void CCompositorDlg::OnButton2() 
{
	for(int i=1;i<=m_num;i++)
		b[i]=a[i];
    paixu ab;
	ab.shellsort(b,1,m_num);
	UpdateData(TRUE);
	m_compare=ab.compare;
	m_move=ab.move;
	m_exchange=ab.exchange;
	m_time=ab.time;
    UpdateData(FALSE);
	
}

void CCompositorDlg::OnButton3() 
{
  for(int i=1;i<=m_num;i++)
		b[i]=a[i];
    paixu ab;
	ab.bubblesort(b,1,m_num);
	UpdateData(TRUE);
	m_compare=ab.compare;
	m_move=ab.move;
	m_exchange=ab.exchange;
	m_time=ab.time;
    UpdateData(FALSE);
	
}

void CCompositorDlg::OnButton4() 
{
for(int i=1;i<=m_num;i++)
		b[i]=a[i];
    paixu ab;
	ab.quicksort(b,1,m_num);
	UpdateData(TRUE);
	m_compare=ab.compare;
	m_move=ab.move;
	m_exchange=ab.exchange;
	m_time=ab.time;
    UpdateData(FALSE);
	
}

void CCompositorDlg::OnButton5() 
{
for(int i=1;i<=m_num;i++)
		b[i]=a[i];
    paixu ab;
	ab.selectsort(b,1,m_num);
	UpdateData(TRUE);
	m_compare=ab.compare;
	m_move=ab.move;
	m_exchange=ab.exchange;
	m_time=ab.time;
    UpdateData(FALSE);
}

void CCompositorDlg::OnButton6() 
{
for(int i=1;i<=m_num;i++)
		b[i]=a[i];
    paixu ab;
	ab.heapsort(b,m_num);
	UpdateData(TRUE);
	m_compare=ab.compare;
	m_move=ab.move;
	m_exchange=ab.exchange;
	m_time=ab.time;
    UpdateData(FALSE);
	
}

void CCompositorDlg::OnButton7() 
{
	for(int i=1;i<=m_num;i++)
		b[i]=a[i];
    paixu ab;
	ab. sortmerge(b,1,m_num,r);
	UpdateData(TRUE);
	m_compare=ab.compare;
	m_move=ab.move;
	m_exchange=ab.exchange;
	m_time=ab.time;
    UpdateData(FALSE);
	
}

void CCompositorDlg::OnExit() 
{
	CDialog::OnCancel();
	
}

void CCompositorDlg::OnOk() 
{
	UpdateData(TRUE);
  srand(time(NULL));
	for(int i=1;i<=m_num;i++)        //产生num个随机数
    a[i]=rand();
    UpdateData(FALSE);
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -