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

📄 a33dlg.cpp

📁 几种排序算法,即一些常用的排序方法 几种排序算法,即一些常用的排序方法
💻 CPP
字号:
// a33Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "a33.h"
#include "a33Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CA33Dlg dialog

CA33Dlg::CA33Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CA33Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CA33Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CA33Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CA33Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CA33Dlg, CDialog)
	//{{AFX_MSG_MAP(CA33Dlg)
	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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CA33Dlg message handlers

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

	// 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
}

// 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 CA33Dlg::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 CA33Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CA33Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
	CString w1,w2="";
	int a=0,b=100;

	//产生20个0到100之间的随机数
	for(int i=1;i<=20;i++)        
	{		
		m_ia[i]=a+rand()%b;     //m_ia[i]在[a,b]之间 
		w1.Format("%d ",m_ia[i]);
		w2+=w1;
	}
	
	((CListBox*)GetDlgItem(IDC_LIST1))->AddString(w2);	
}

void CA33Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	
	mf_InsertSort(m_ia,1,20);	//插入排序法(数组,数组起始位置,排序个数)

	CString w1,w2="";
	for(int i=1;i<=20;i++)       
	{	
		w1.Format("%d ",m_ia[i]);
		w2+=w1;
	}
	((CListBox*)GetDlgItem(IDC_LIST1))->AddString(w2);	
}

void CA33Dlg::mf_InsertSort(int r[],long m,long n)	//插入排序法(数组,数组起始位置,排序个数)
{
	long x,i,j;
	for (i=m+1;i<=n;i++)
	{
		x=r[i];
		j=i-1;		
		while (j>=m && x<r[j])
		{			
			r[j+1]=r[j];
			j--;
		}
		r[j+1]=x;
	}
}

//希尔排序
void CA33Dlg::mf_ShellSort(int r[],long m,long n)	//希尔排序法(数组,数组起始位置,排序个数)
{	
	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)
			{ 			
				if(r[j] > r[j+gap])	//交换r[j]和r[j+gap]
				{
					temp=r[j];
					r[j]=r[j+gap];
					r[j+gap]=temp;				
					j=j-gap;
				}
				else j=m-1;     //通过给j赋值而退出while循环
			}
		}
		gap=gap/2;              //减小增量
	}	 
}

void CA33Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	
	mf_ShellSort(m_ia,1,20);	//希尔排序法(数组,数组起始位置,排序个数)

	CString w1,w2="";
	for(int i=1;i<=20;i++)       
	{	
		w1.Format("%d ",m_ia[i]);
		w2+=w1;
	}
	((CListBox*)GetDlgItem(IDC_LIST1))->AddString(w2);	
}

void CA33Dlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	
	mf_BubbleSort(m_ia,1,20);	//冒泡排序法(数组,数组起始位置,排序个数)

	CString w1,w2="";
	for(int i=1;i<=20;i++)       
	{	
		w1.Format("%d ",m_ia[i]);
		w2+=w1;
	}
	((CListBox*)GetDlgItem(IDC_LIST1))->AddString(w2);	
}

void CA33Dlg::mf_BubbleSort(int r[],long m,long n)	//冒泡排序法(数组,数组起始位置,排序个数)
{	
	long i,j,exchanges;
	int temp;
	for(i=m;i<=n-1;i++)
	{
		exchanges=0;
		for(j=n;j>=i+1;j--)
		{
			
			if(r[j]<r[j-1])		//比较
			{
				temp=r[j];
				r[j]=r[j-1];	//交换
				r[j-1]=temp;
				exchanges=1;			
			}
		}
        if(0 == exchanges)		//本趟无交换发生,则结束
		{ 
			return;
		}
	}	 
}

void CA33Dlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	
	mf_QuickSort(m_ia,1,20);	//快速排序法(数组,数组起始位置,排序个数)

	CString w1,w2="";
	for(int i=1;i<=20;i++)       
	{	
		w1.Format("%d ",m_ia[i]);
		w2+=w1;
	}
	((CListBox*)GetDlgItem(IDC_LIST1))->AddString(w2);	
}

void CA33Dlg::mf_QuickSort(int r[],long s,long t)	//快速排序法(数组,数组起始位置,排序个数)
{
	long i=s,j=t;
	if (s < t)
	{
		r[0]=r[s];
		do								//以r[0]为基准将r分成两部分
		{ 			
			while (j>i && r[j]>r[0])	//从右向左找大于基准的纪录r[j]
			{			
				j--;
			}
			if(i<j)
			{
				r[i]=r[j];				
				i++;
			}		
			while (i<j && r[i]<r[0])	//从左向右找大于基准的纪录r[i]
			{			
				i++;
			}
			if(i<j)
			{				
				r[j]=r[i];
				j--;
			}
		} while (i < j);
		r[i]=r[0];
		mf_QuickSort(r,s,j-1);
		mf_QuickSort(r,j+1,t);
	}
}

void CA33Dlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	
	mf_SelectSort(m_ia,1,20);	//选择排序法(数组,数组起始位置,排序个数)

	CString w1,w2="";
	for(int i=1;i<=20;i++)       
	{	
		w1.Format("%d ",m_ia[i]);
		w2+=w1;
	}
	((CListBox*)GetDlgItem(IDC_LIST1))->AddString(w2);	
}

void CA33Dlg::mf_SelectSort(int r[],long m,long n)	//选择排序法(数组,数组起始位置,排序个数)
{	
	long i,j,k;
	int temp;
	for (i=m;i<=n-1;i++)
	{
		k=i;
		for (j=i+1;j<=n;j++)
		{			
			if(r[j]<r[k])
				k=j;			//用k指出每趟在无序区段的最小元素
		}
		    
			temp=r[i];			//将r[k]和r[i]交换 
			r[i]=r[k];
			r[k]=temp;
	}	 
}

void CA33Dlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	
	((CListBox*)GetDlgItem(IDC_LIST1))->ResetContent();
}




⌨️ 快捷键说明

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