📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(m_SortType==0)
{
ShowMessage("错误:没有选择排序算法!");
return;
}
m_RunCount = 0;
Memo2->Clear();
m_pArray = new int[m_Count];
if(Memo1->Lines->Count >= m_Count)
{
for(int i=0;i<m_Count;i++)
{
m_pArray[i] = StrToInt(Memo1->Lines->Strings[i]);
}
}
else
{
for(int i=0;i<Memo1->Lines->Count;i++)
{
m_pArray[i] = StrToInt(Memo1->Lines->Strings[i]);
}
for(int j=Memo1->Lines->Count;j<m_Count;j++)
{
m_pArray[j] = 0;
}
}
if(m_SortType==1)
{
QuickSort(m_pArray,0,m_Count);
}
else if(m_SortType==2)
{
UpSort(m_pArray,m_Count-1);
}
for(int k=0;k<m_Count;k++)
{
Memo2->Lines->Append(IntToStr(m_pArray[k]));
}
//Memo2->Enabled = true;
Edit2->Text = IntToStr(m_RunCount);
Memo2->Color = clWindow;
Memo1->Enabled = false;
Memo1->Color = clActiveBorder;
delete m_pArray;
m_pArray = NULL;
}
//---------------------------------------------------------------------------
//快速排序函数:
void TForm1::QuickSort(int *m_Array,int low,int high)
{
int i,j;
int temp;
temp = m_Array[low];
i = low;
j = high;
if(i>j) return;
while(i<j)
{
while(m_Array[j]>=temp && i<j)
{
--j;
m_RunCount++;
}
m_Array[i] = m_Array[j];
while(m_Array[i]<=temp && i<j)
{
++i;
m_RunCount++;
}
m_Array[j] = m_Array[i];
}
m_Array[i] = temp;
if(i-1>low) QuickSort(m_Array,low,i-1);
if(high>i+1) QuickSort(m_Array,i+1,high);
}
//-------------------------------------------------------------------------
//冒泡法排序
void TForm1::UpSort(int *m_Array,int n)
{
int temp;
for(int i=1;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(m_Array[j]>m_Array[j+1])
{
temp = m_Array[j];
m_Array[j] = m_Array[j+1];
m_Array[j+1] = temp;
m_RunCount++;
}
}
}
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
m_Count = StrToInt(Edit1->Text);
Memo1->Enabled = true;
Memo1->Color = clWindow;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
m_SortType = 0;
Memo1->Enabled = false;
Memo1->Color = clActiveBorder;
Memo2->Enabled = false;
Memo2->Color = clActiveBorder;
Edit2->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Memo1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Memo2->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
if(ComboBox1->Text =="冒泡排序")
{
m_SortType = 1;
}
else if(ComboBox1->Text =="快速排序")
{
m_SortType = 2;
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -