Int* tForm1::merge(int *m1, int* m2, int l1, int l2)
{
int* ret = new int[l1+l2];
intn = 0; CountOpP+=2;
// Сливаем массивы, пока один не закончится
while (l1 && l2){
if (*m1 < *m2){
ret[n] = *m1;
m1++; l1--; CountOpP+=4;}
else {
ret[n] = *m2;
m2++; l2--; }
n++; CountOpP+=4;}
// Если закончился первый массив
if (l1 == 0){
for (inti=0; i<l2; i++){
ret[n++] = *m2++; CountOpP+=3;}}
// Если закончился второй массив
else {
for (inti=0; i<l1; i++){
ret[n++] = *m1++; CountOpP+=3;}}
return ret;
}
// ---------------------------------------------------------------------------
// Функция сортировки вставками
voidTForm1::SortInsert() {
int * mas = new int[EMn->Text.ToInt()];
int x, i, j;
for (inti = 0; i<EMn->Text.ToInt(); i++)
mas[i] = array[i];
TTime time, temp;
time = Time();
if (CRealTime->Checked) {
for (i = 0; i<EMn->Text.ToInt(); i++) {
CountOpI += 3;
x = mas[i];
// поиск места элемента в готовой последовательности
for (j = i - 1; j >= 0 && mas[j] > x; j--) {
CountOpI += 3;
mas[j + 1] = mas[j]; // сдвигаем элемент направо, пока не дошли
Memo4->Lines->Text = Print(mas);
Memo4->Refresh();
} // место найдено, вставить элемент
CountOpI++;
mas[j + 1] = x;
temp = Time() - time;
LTimeI->Caption = temp.FormatString("nn:ss:zz");
LOpI->Caption = FormatInt(CountOpI);
LOpI->Refresh();
LTimeI->Refresh();
}
}
else {
for (i = 0; i<EMn->Text.ToInt(); i++) {
CountOpI += 3;
x = mas[i];
// поиск места элемента в готовой последовательности
for (j = i - 1; j >= 0 && mas[j] > x; j--) {
CountOpI += 3;
mas[j + 1] = mas[j]; // сдвигаем элемент направо, пока не дошли
} // место найдено, вставить элемент
CountOpI++;
mas[j + 1] = x;
}
}
time = Time() - time;
LTimeI->Caption = time.FormatString("nn:ss:zz");
Memo4->Lines->Text = Print(mas);
LOpI->Caption = FormatInt(CountOpI);
delete[]mas;
}
// ---------------------------------------------------------------------------
// Сортировка по методу шелла
VoidTForm1::SortShell() {
int * mas = new int[EMn->Text.ToInt()];
long i, j, g;
short c; // флаг
float temp;
for (inti = 0; i<EMn->Text.ToInt(); i++)
mas[i] = array[i];
TTime time, ttemp;
if (CRealTime->Checked) {
time = Time();
g = EMn->Text.ToInt() / 2;
CountOpS+=2;
do {
i = g;
CountOpS++;
do {
j = i - g;
c = 1;
CountOpS += 3;
do {
if (mas[j] <= mas[j + g]) {
c = 0;
CountOpS += 2;
}
else {
temp = mas[j];
mas[j] = mas[j + g];
mas[j + g] = temp;
CountOpS += 3;
ttemp = Time() - time;
LTimeS->Caption = ttemp.FormatString("nn:ss:zz");
Memo5->Lines->Text = Print(mas);
LOpS->Caption = FormatInt(CountOpS);
Memo5->Refresh();
LOpS->Refresh();
LTimeS->Refresh();
}
j--;
CountOpS += 2;
}
while (j >= 0 && c);
i++;
CountOpS += 1;
}
while (i<EMn->Text.ToInt());
g = g / 2;
CountOpS += 1;
}
while (g > 0);
}
else {
time = Time();
g = EMn->Text.ToInt() / 2;
CountOpS++;
do {
i = g;
CountOpS++;
do {
j = i - g;
c = 1;
CountOpS += 2;
do {
if (mas[j] <= mas[j + g]) {
c = 0;
CountOpS += 3;
}
else {
temp = mas[j];
mas[j] = mas[j + g];
mas[j + g] = temp;
CountOpS += 3;
}
j--;
CountOpS += 2;
}
while (j >= 0 && c);
i++;
CountOpS += 2;
}
while (i<EMn->Text.ToInt());
g = g / 2;
CountOpS += 2;
}
while (g > 0);
}
time = Time() - time;
LTimeS->Caption = time.FormatString("nn:ss:zz");
Memo5->Lines->Text = Print(mas);
LOpS->Caption = FormatInt(CountOpS);
delete[]mas;
}
// ---------------------------------------------------------------------------
// Функциявызовасортировок
void __fastcall TForm1::StartClick(TObject *Sender) {
Start->Enabled = false;
Start->Refresh();
Memo2->Clear();
Memo3->Clear();
Memo4->Clear();
Memo5->Clear();
LTimeX->Caption = "00:00:00";
LTimeP->Caption = "00:00:00";
LTimeI->Caption = "00:00:00";
LTimeS->Caption = "00:00:00";
CountOpX = 0;
CountOpP = 0;
CountOpI = 0;
CountOpS = 0;
LOpP->Caption = 0;
LOpX->Caption = 0;
LOpI->Caption = 0;
LOpS->Caption = 0;
Result->Refresh();
ok[0] = false;
ok[1] = false;
ok[2] = false;
ok[3] = false;
DWORD threadID;
if (CParalel->Checked) {
if (CPusyr->Checked) {
threadID = 0;
hthread1 = CreateThread(0, 0, SortP, NULL, 0, &threadID);
SetThreadPriority(hthread1, THREAD_PRIORITY_HIGHEST);
}
if (CXoar->Checked) {
threadID = 1;
hthread2 = CreateThread(0, 0, SortX, NULL, 0, &threadID);
SetThreadPriority(hthread2, THREAD_PRIORITY_HIGHEST);
}
if (CInsert->Checked) {
threadID = 2;
hthread3 = CreateThread(0, 0, SortI, NULL, 0, &threadID);
SetThreadPriority(hthread3, THREAD_PRIORITY_HIGHEST);
}
if (CShell->Checked) {
threadID = 4;
hthread4 = CreateThread(0, 0, SortS, NULL, 0, &threadID);
SetThreadPriority(hthread4, THREAD_PRIORITY_HIGHEST);
}
threadID = 3;
hthread5 = CreateThread(0, 0, StartChek, NULL, 0, &threadID);
}
else {
if (CXoar->Checked)
SortXoar();
if (CInsert->Checked)
SortInsert();
if (CPusyr->Checked)
SortMarge();
if (CShell->Checked)
SortShell();
Start->Enabled = true;
}
}
// ---------------------------------------------------------------------------
/*- Функции для потоков -*/
DWORD __stdcall TForm1::SortX(LPVOID pParams) {
Form1->SortXoar();
Form1->ok[1] = true;
return 1;
}
// ---------------------------------------------------------------------------
DWORD __stdcall TForm1::SortS(LPVOID pParams) {
Form1->SortShell();
Form1->ok[3] = true;
return 1;
}
// ---------------------------------------------------------------------------
DWORD __stdcall TForm1::SortP(LPVOID pParams) {
Form1->SortMarge();
Form1->ok[2] = true;
return 1;
}
// ---------------------------------------------------------------------------
DWORD __stdcall TForm1::SortI(LPVOID pParams) {
Form1->SortInsert();
Form1->ok[0] = true;
return 1;
}
// ---------------------------------------------------------------------------
/*- .Функциидляпотоков -*/
void __fastcall TForm1::EMnChange(TObject *Sender) {
Start->Enabled = false;
}
// ---------------------------------------------------------------------------
// Функция слежения за окончанием работы потоков
DWORD __stdcall TForm1::StartChek(LPVOID pParams) {
Form1->TerminatedThear->Enabled = true;
chek:
if (Form1->ok[0] && Form1->ok[1] && Form1->ok[2] && Form1->ok[3])
{
Form1->Start->Enabled = true;
Form1->TerminatedThear->Enabled = false;
CloseHandle( Form1->hthread1 );
CloseHandle( Form1->hthread2 );
CloseHandle( Form1->hthread3 );
CloseHandle( Form1->hthread4 );
CloseHandle( Form1->hthread5 );
}
else
gotochek;
return 1;
}
// ---------------------------------------------------------------------------
// Функция для вывода числа разрядами в строковый параметр
String TForm1::FormatInt(double num) {
int del = 1;
String result;
doubleost = 0;
for (int u = 0; u < (String(num).Length() - 1) / 3; u++) {
del = del * 1000;
}
while (String(num).Length() > 3) {
modf(num / del, &ost);
num = fmod(num, del);
result += String(ost) + ",";
del /= 1000;
}
if (String(num).Length() < 1)
result = result + "000";
else if (String(num).Length() < 2)
result = result + "00" + String(num);
else if (String(num).Length() < 3)
result = result + "0" + String(num);
else
result += String(num);
return result;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::CNegativeClick(TObject *Sender) {
Start->Enabled = false;
}
// ---------------------------------------------------------------------------
// Экстренное завершение потоков
void __fastcall TForm1::TerminatedThearClick(TObject *Sender)
{
if(hthread1)
TerminateThread(hthread1,false);
if(hthread2)
TerminateThread(hthread2,false);
if(hthread3)
TerminateThread(hthread3,false);
if(hthread4)
TerminateThread(hthread4,false);
if(hthread5)
TerminateThread(hthread5,false);
CloseHandle( hthread1 );
CloseHandle( hthread2 );
CloseHandle( hthread3 );
CloseHandle( hthread4 );
CloseHandle( hthread5 );
Form1->Start->Enabled = true;
Form1->TerminatedThear->Enabled = false;
}
//---------------------------------------------------------------------------