-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSortAlgoPage.xaml.cpp
1731 lines (1598 loc) · 63 KB
/
SortAlgoPage.xaml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SortAlgoPage.xaml.cpp
// SortAlgoPage 类的实现
//
#include "pch.h"
#include "SortAlgoPage.xaml.h"
#include "Histogram.h"
#include <vector>
#include "ExecuteStep.h"
#include "Util.h"
#include "CodeDrawable.h"
using namespace AlgorithmVisualization;
using namespace Platform;
using namespace std;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Xaml::Shapes;
using namespace Microsoft::UI::Xaml::Media;
using namespace Windows::UI;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::System::Threading;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml::Documents;
// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板
/// <summary>
/// 导航栏项目
/// </summary>
struct NavigateItem
{
String^ Tag;
String^ Content;
};
const vector<NavigateItem> PrimarySortNavItems = {
NavigateItem{L"BubbleSort", L"冒泡排序"},
NavigateItem{L"SelectionSort", L"选择排序"},
NavigateItem{L"InsertionSort", L"插入排序"},
NavigateItem{L"ShellSort", L"希尔排序"}
}; //初等排序导航项目
const vector<NavigateItem> AdvancedComparativeSortNavItems = {
NavigateItem{L"MergeSort", L"归并排序"},
NavigateItem{L"QuickSort", L"快速排序"},
NavigateItem{L"HeapSort", L"堆排序"}
}; //高等比较排序导航项目
const vector<NavigateItem> AdvancedNonComparativeSortNavItems = {
NavigateItem{L"CountingSort", L"计数排序"},
NavigateItem{L"BucketSort", L"桶排序"},
NavigateItem{L"RadixSort", L"基数排序"}
}; //高等非比较排序导航项目
/// <summary>
/// 排序算法页面构造函数
/// </summary>
SortAlgoPage::SortAlgoPage()
{
InitializeComponent();
InitNavViewItems(((App^)(Application::Current))->sortAlgorithmType); //初始化导航栏
}
/// <summary>
/// 导航栏的加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AlgorithmVisualization::SortAlgoPage::SortNavView_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
SortNavView->SelectedItem = SortNavView->MenuItems->GetAt(0); //默认选择第一个
}
/// <summary>
/// 当项目被点击时
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
void AlgorithmVisualization::SortAlgoPage::SortNavView_ItemInvoked(Microsoft::UI::Xaml::Controls::NavigationView^ sender, Microsoft::UI::Xaml::Controls::NavigationViewItemInvokedEventArgs^ args)
{
auto tag = args->InvokedItemContainer->Tag->ToString(); //获取点击Tag
Executor->SortVector->Clear(); //清空向量
Executor->StopTimer(); //关闭计时器
InitAlgorithm(tag); //初始化对应的算法
}
/// <summary>
/// 调试按钮被点击时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AlgorithmVisualization::SortAlgoPage::Debug_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
}
/// <summary>
/// 排序柱状图监测尺寸改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AlgorithmVisualization::SortAlgoPage::SortHistogram_SizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e)
{
lastHistogramWidth = e->NewSize.Width; //记录宽度
lastHistogramHeight = e->NewSize.Height; //记录高度
if (Executor->NeedAssistHistogram)
{
Executor->MainHistogram->onSizeChanged(e->NewSize.Width, e->NewSize.Height / 2); //自适应布局调整大小
Executor->AssistHistogram->onSizeChanged(e->NewSize.Width, e->NewSize.Height / 2); //自适应布局调整大小
}
else
{
Executor->MainHistogram->onSizeChanged(e->NewSize.Width, e->NewSize.Height); //自适应布局调整大小
}
}
/// <summary>
/// 初始化算法
/// </summary>
/// <param name="tag"></param>
void AlgorithmVisualization::SortAlgoPage::InitAlgorithm(String^ tag)
{
float width = lastHistogramWidth;
float height = defaultHeight; //不使用动态高度
Introduction->TextHighlighters->Clear(); //清空高亮
if (tag == L"BubbleSort") {
AlgorithmName->Text = L"冒泡排序";
Executor = ref new SortExcute(20, width, height, false); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitBubbleSort();
}
else if (tag == L"SelectionSort")
{
AlgorithmName->Text = L"选择排序";
Executor = ref new SortExcute(20, width, height, false); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitSelectionSort();
}
else if (tag == L"InsertionSort")
{
AlgorithmName->Text = L"插入排序";
Executor = ref new SortExcute(20, width, height, false); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitInsertionSort();
}
else if (tag == L"ShellSort")
{
AlgorithmName->Text = "希尔排序";
Executor = ref new SortExcute(20, width, height, false); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitShellSort();
}
else if (tag == L"MergeSort")
{
AlgorithmName->Text = "归并排序";
Executor = ref new SortExcute(32, width, height, true); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitMergeSort();
}
else if (tag == L"QuickSort")
{
AlgorithmName->Text = "快速排序";
Executor = ref new SortExcute(32, width, height, false); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitQuickSort();
}
else if (tag == L"HeapSort")
{
AlgorithmName->Text = "堆排序";
Executor = ref new SortExcute(32, width, height, false); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitHeapSort();
}
else if (tag == L"CountingSort")
{
AlgorithmName->Text = "计数排序";
Executor = ref new SortExcute(32, width, height, true); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitCountingSort();
}
else if (tag == L"BucketSort")
{
AlgorithmName->Text = "桶排序";
Executor = ref new SortExcute(32, width, height, true); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitBucketSort();
}
else if (tag == L"RadixSort")
{
AlgorithmName->Text = "基数排序";
Executor = ref new SortExcute(32, width, height, true); //实例化排序可执行 初等排序设置小一点
InitExecutor();
InitRadixSort();
}
SortHistogram->Children->Clear();
if (Executor->NeedAssistHistogram)
{
auto MainTextBlock = ref new TextBlock();
MainTextBlock->Text = L"排序柱状图";
MainTextBlock->FontWeight = Windows::UI::Text::FontWeights::Bold;
MainTextBlock->Margin = Windows::UI::Xaml::Thickness(6);
SortHistogram->Children->Append(MainTextBlock); //向父级容器中添加文字
auto mainHistogramView = Executor->MainHistogram->container;
SortHistogram->Children->Append(mainHistogramView); //向父级容器中添加柱状图的容器
auto AssistTextBlock = ref new TextBlock();
AssistTextBlock->Text = L"辅助柱状图";
AssistTextBlock->FontWeight = Windows::UI::Text::FontWeights::Bold;
AssistTextBlock->Margin = Windows::UI::Xaml::Thickness(6);
SortHistogram->Children->Append(AssistTextBlock); //向父级容器中添加文字
auto assistHistogramView = Executor->AssistHistogram->container;
assistHistogramView->Height = height / 2;
SortHistogram->Children->Append(assistHistogramView); //向父级容器中添加柱状图的容器
}
else
{
SortHistogram->Children->Append(Executor->MainHistogram->container); //向父级容器中添加柱状图的容器
}
Executor->NavigateToFirst();
}
/// <summary>
/// 初始化导航栏项目
/// </summary>
/// <param name="sortAlgorithmType">排序算法类型</param>
void AlgorithmVisualization::SortAlgoPage::InitNavViewItems(int sortAlgorithmType)
{
AlgorithmType = sortAlgorithmType;
SortNavView->MenuItems->Clear();
vector<NavigateItem> NavigateItems;
switch (sortAlgorithmType) //选择对应的排序算法项目向量
{
case 0:
NavigateItems = PrimarySortNavItems;
InitAlgorithm(L"BubbleSort");
break;
case 1:
NavigateItems = AdvancedComparativeSortNavItems;
InitAlgorithm(L"MergeSort");
break;
case 2:
NavigateItems = AdvancedNonComparativeSortNavItems;
InitAlgorithm(L"CountingSort");
break;
default:
break;
}
for (NavigateItem i : NavigateItems)
{
auto menuItem = ref new Microsoft::UI::Xaml::Controls::NavigationViewItem(); //创建菜单栏目
menuItem->Content = i.Content; //设置内容
menuItem->Tag = i.Tag; //设置标签
SortNavView->MenuItems->Append(menuItem); //追加进入菜单栏
}
}
/// <summary>
/// 初始化执行器
/// </summary>
void AlgorithmVisualization::SortAlgoPage::InitExecutor()
{
Executor->ProcessText = ProcessText;
Executor->ProgressSlider = ProgressSlider;
Executor->SpeedText = SpeedText;
Executor->SpeedSlider = SpeedSlider;
Executor->CodeText = CodeText;
Executor->CodeDrawable = ref new CodeDrawable();
SpeedSlider->Maximum = Executor->SpeedList->Size - 1; //设置滑块的最大值
SpeedSlider->Minimum = 1; //设置滑块的最小值
}
/// <summary>
/// 显示调试信息
/// </summary>
/// <param name="message">调试信息</param>
void AlgorithmVisualization::SortAlgoPage::Debug(String^ message)
{
Console->Text += message + "\n";
}
/// <summary>
/// 开始计时器线程
/// </summary>
void AlgorithmVisualization::SortAlgoPage::StartThreadTimer()
{
if (!Executor->TimerIsRunning()) //没有开始运行才创建
{
Windows::Foundation::TimeSpan period{};
period.Duration = Executor->Speed * 10000; //设置时间间隔
Executor->ThreadTimer = ThreadPoolTimer::CreatePeriodicTimer(
ref new TimerElapsedHandler([this](ThreadPoolTimer^ source)
{
if (Executor->IsLastStep())
{
//已经到达最后一步则结束计时器
Executor->StopTimer();
return;
}
CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
ref new DispatchedHandler([this]()
{
if (Executor->IsLastStep())
{
//已经到达最后一步则结束计时器
Executor->StopTimer();
return;
}
Executor->NavigateToNext(); //如果没有到最后一步则导航到下一步
}
));
}), period
);
}
}
/// <summary>
/// 导航到步骤
/// </summary>
/// <param name="index">序号</param>
/// <returns></returns>
SingleStep^ AlgorithmVisualization::SortExcute::NavigateToStep(int index)
{
CurrentStep = index; //设置当前步骤
ProcessText->Text = CurrentStep.ToString() + "/" + (StepList->Size - 1).ToString(); //设置文字
ProgressSlider->Value = CurrentStep;
if (index >= 0 && index < (int)StepList->Size)
{
//ThisState中[0]保存主柱状图数组
//ThisState中[1]保存主柱状图数组的状态
//ThisState中[2]保存主柱状图数组的临时标签
//ThisState中[3]保存辅助柱状图数组
//ThisState中[4]保存辅助柱状图数组的状态
auto thisStep = StepList->GetAt(index);
auto mainStateList = ref new Vector<PillarState>(); //实例化状态列表
auto assistStateList = ref new Vector<PillarState>(); //实例化状态列表
for (int i : thisStep->ThisState->GetAt(1))
{
mainStateList->Append((PillarState)i);
}
if (NeedAssistHistogram)
{
for (int i : thisStep->ThisState->GetAt(4))
{
assistStateList->Append((PillarState)i);
}
}
int size = thisStep->ThisState->Size;
if (thisStep->ThisState->Size == 2) //没有临时变量的时候
MainHistogram->load(thisStep->ThisState->GetAt(0), mainStateList); //加载步骤的内容
else //有临时变量
MainHistogram->load(thisStep->ThisState->GetAt(0), mainStateList, thisStep->ThisState->GetAt(2)); //加载步骤的内容
if (NeedAssistHistogram)
AssistHistogram->load(thisStep->ThisState->GetAt(3), assistStateList); //加载步骤的内容
int i = thisStep->ThisState->Size;
ShowCodeChange(StepList->GetAt(index)->HighlightLines); //显示代码的变化
return StepList->GetAt(index);
}
else
{
throw ref new InvalidArgumentException("传入的序号越界"); //抛出数组越界
}
}
/// <summary>
/// 导航到下一步
/// </summary>
/// <returns></returns>
SingleStep^ AlgorithmVisualization::SortExcute::NavigateToNext()
{
CurrentStep++;
return NavigateToStep(CurrentStep);
}
/// <summary>
/// 导航到上一步
/// </summary>
/// <returns></returns>
SingleStep^ AlgorithmVisualization::SortExcute::NavigateToPrevious()
{
CurrentStep--;
return NavigateToStep(CurrentStep);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="n">要排序的数字个数</param>
AlgorithmVisualization::SortExcute::SortExcute(int n, float width, float height, bool needAssist)
{
NeedAssistHistogram = needAssist;
DefaultInit(); //默认的初始化
if (needAssist)
{
MainHistogram = ref new Histogram(width, height / 2); //初始化容器
AssistHistogram = ref new Histogram(width, height / 2); //初始化容器
}
else
{
MainHistogram = ref new Histogram(width, height); //初始化容器
}
SortVector = ref new Vector<int>(); //初始化要排序的向量
for (int i = 0; i < n; ++i)
{
int value = rand() % 99; //生成随机数用于排序
SortVector->Append(value);
}
SortStates = ref new Vector<int>(n, (int)PillarState::Default);
if (needAssist)
{
AssistVector = ref new Vector<int>(n, 0);
AssistStates = ref new Vector<int>(n, (int)PillarState::Default);
}
}
/// <summary>
/// 展示代码的修改
/// </summary>
/// <param name="highlighters"></param>
void AlgorithmVisualization::SortExcute::ShowCodeChange(IVector<int>^ highlighters)
{
auto generateRes = CodeDrawable->GenerateDrawable(highlighters); //生成可绘制的代码
CodeText->Blocks->Clear();
CodeText->Blocks->Append((Paragraph^)(generateRes->GetAt(0))); //加载段落
CodeText->TextHighlighters->Clear();
CodeText->TextHighlighters->Append((TextHighlighter^)(generateRes->GetAt(1))); //加载高亮
}
/// <summary>
/// 上一步
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AlgorithmVisualization::SortAlgoPage::Previous_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (!Executor->IsFirstStep()) Executor->NavigateToPrevious();
//Debug("当前步骤:" + executor->CurrentStep);
}
/// <summary>
/// 开始按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AlgorithmVisualization::SortAlgoPage::Start_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
StartThreadTimer(); //启动计时器线程
}
/// <summary>
/// 暂停按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AlgorithmVisualization::SortAlgoPage::Pause_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Executor->StopTimer(); //结束计时器
}
/// <summary>
/// 下一步
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AlgorithmVisualization::SortAlgoPage::Next_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (!Executor->IsLastStep()) Executor->NavigateToNext();
//Debug("当前步骤:" + executor->CurrentStep);
}
/// <summary>
/// 初始化辅助步骤
/// </summary>
/// <param name="step"></param>
void AlgorithmVisualization::SortAlgoPage::InitAssistStep(SingleStep^ step)
{
if (Executor->NeedAssistHistogram)
{
step->ThisState->Append(Util::CopyVector(Executor->AssistVector));
step->ThisState->Append(Util::CopyVector(Executor->AssistStates));
step->ThisState->Append(Util::CopyVector(Executor->AssistVector));
}
}
/// <summary>
/// 交换向量中数字
/// </summary>
/// <param name="arr"></param>
/// <param name="left"></param>
/// <param name="right"></param>
void AlgorithmVisualization::SortAlgoPage::SwapInVector(IVector<int>^ arr, int left, int right)
{
int temp = arr->GetAt(left);
arr->SetAt(left, arr->GetAt(right));
arr->SetAt(right, temp);
}
/// <summary>
/// 添加空步骤
/// </summary>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddEmptyStep(IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(0); //比较步骤
tempStep->HighlightLines->Append(-1);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加设置步骤
/// </summary>
/// <param name="pos"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddSetStep(int pos, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(0); //设置步骤
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
Executor->SortStates->SetAt(pos, (int)PillarState::SetValue); //设置去向
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加有来源的设置步骤
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddSetFromToStep(int from, int to, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(0); //设置步骤
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
Executor->SortStates->SetAt(from, (int)PillarState::Selected); //设置来源
Executor->SortStates->SetAt(to, (int)PillarState::SetValue); //设置去向
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 新建跨柱状图的有来源步骤
/// </summary>
/// <param name="fromHis"></param>
/// <param name="fromPos"></param>
/// <param name="toHis"></param>
/// <param name="toPos"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddSetFromToStep(int fromHis, int fromPos, int toHis, int toPos, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(0); //设置步骤
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
//获取合适的状态向量
IVector<int>^ leftHisStates = fromHis == 0 ? Executor->SortStates : Executor->AssistStates;
IVector<int>^ rightHisStates = toHis == 0 ? Executor->SortStates : Executor->AssistStates;
//设置来源和去向
leftHisStates->SetAt(fromPos, (int)PillarState::Selected); //设置来源
rightHisStates->SetAt(toPos, (int)PillarState::SetValue); //设置去向
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加比较步骤
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddCompareStep(int left, int right, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(0); //比较步骤
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
//设置两个位置为比较状态
Executor->SortStates->SetAt(left, (int)PillarState::Compared);
Executor->SortStates->SetAt(right, (int)PillarState::Compared);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加跨柱状图的比较
/// </summary>
/// <param name="leftHisIndex"></param>
/// <param name="leftPos"></param>
/// <param name="rightHisIndex"></param>
/// <param name="rightPos"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddCompareStep(int leftHisIndex, int leftPos, int rightHisIndex, int rightPos, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(0); //比较步骤
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
//获取合适的状态向量
IVector<int>^ leftHisStates = leftHisIndex == 0 ? Executor->SortStates : Executor->AssistStates;
IVector<int>^ rightHisStates = rightHisIndex == 0 ? Executor->SortStates : Executor->AssistStates;
//设置两个位置为比较状态
leftHisStates->SetAt(leftPos, (int)PillarState::Compared);
rightHisStates->SetAt(rightPos, (int)PillarState::Compared);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加交换步骤
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddSwapStep(int left, int right, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(1);
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
//设置两个位置为交换状态
Executor->SortStates->SetAt(left, (int)PillarState::Swapping);
Executor->SortStates->SetAt(right, (int)PillarState::Swapping);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加跨柱状图的交换
/// </summary>
/// <param name="leftHisIndex"></param>
/// <param name="leftPos"></param>
/// <param name="rightHisIndex"></param>
/// <param name="rightPos"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddSwapStep(int leftHisIndex, int leftPos, int rightHisIndex, int rightPos, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(1);
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
//获取合适的状态向量
IVector<int>^ leftHisStates = leftHisIndex == 0 ? Executor->SortStates : Executor->AssistStates;
IVector<int>^ rightHisStates = rightHisIndex == 0 ? Executor->SortStates : Executor->AssistStates;
//设置两个位置为交换状态
leftHisStates->SetAt(leftPos, (int)PillarState::Swapping);
rightHisStates->SetAt(rightPos, (int)PillarState::Swapping);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 设置为默认状态
/// </summary>
/// <param name="his0"></param>
/// <param name="index0"></param>
/// <param name="his1"></param>
/// <param name="index1"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::SetToDefault(int his0, int index0, int his1, int index1)
{
//获取合适的状态向量
IVector<int>^ leftHisStates = his0 == 0 ? Executor->SortStates : Executor->AssistStates;
IVector<int>^ rightHisStates = his1 == 0 ? Executor->SortStates : Executor->AssistStates;
leftHisStates->SetAt(index0, (int)PillarState::Default);
rightHisStates->SetAt(index1, (int)PillarState::Default);
return true;
}
/// <summary>
/// 添加恢复步骤
/// </summary>
/// <param name="recover"></param>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddRecoverStep(IVector<int>^ recover, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(2);
tempStep->HighlightLines->Append(-1);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
for (auto i : recover)
{
Executor->SortStates->SetAt(i, (int)PillarState::Default); //遍历设为默认状态
}
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加完成步骤
/// </summary>
/// <param name="complete"></param>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddCompleteStep(IVector<int>^ complete, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(3);
tempStep->HighlightLines->Append(-1);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
for (auto i : complete)
{
Executor->SortStates->SetAt(i, (int)PillarState::Completed); //遍历设为完成状态
}
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加选中步骤
/// </summary>
/// <param name="select"></param>
/// <param name="highlightLine"></param>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddSelectStep(IVector<int>^ select, int highlightLine, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(4);
tempStep->HighlightLines->Append(highlightLine);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
for (auto i : select)
{
Executor->SortStates->SetAt(i, (int)PillarState::Selected); //遍历设为完成状态
}
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 添加全部完成步骤
/// </summary>
/// <param name="isTemp"></param>
/// <param name="histogramIndex"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddAllCompleteStep(IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(3);
tempStep->HighlightLines->Append(-1);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
for (unsigned int i = 0; i < Executor->SortStates->Size; ++i)
{
Executor->SortStates->SetAt(i, (int)PillarState::Completed); //遍历设为完成状态
}
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 设置跨柱状图的全部完成
/// </summary>
/// <param name="hisIndex"></param>
/// <param name="isTemp"></param>
/// <returns></returns>
bool AlgorithmVisualization::SortAlgoPage::AddAllCompleteStep(int hisIndex, IVector<int>^ isTemp)
{
auto tempStep = ref new SingleStep(3);
tempStep->HighlightLines->Append(-1);
tempStep->ThisState->Append(Util::CopyVector(Executor->SortVector));
IVector<int>^ hisStates = hisIndex == 0 ? Executor->SortStates : Executor->AssistStates;
for (unsigned int i = 0; i < Executor->SortStates->Size; ++i)
{
hisStates->SetAt(i, (int)PillarState::Completed); //遍历设为完成状态
}
tempStep->ThisState->Append(Util::CopyVector(Executor->SortStates));
tempStep->ThisState->Append(isTemp);
InitAssistStep(tempStep);
Executor->AddStep(tempStep);
return true;
}
/// <summary>
/// 初始化冒泡排序算法
/// </summary>
void AlgorithmVisualization::SortAlgoPage::InitBubbleSort()
{
Introduction->Text = L"时间复杂度:O(n²)\n它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果顺序(如从大到小、首字母从Z到A)错误就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素列已经排序完成。";
auto codeDrawable = Executor->CodeDrawable;
codeDrawable->Texts->Clear();
codeDrawable->Texts->Append("for (int i = 0; i < n - 1; i++)\n" +
"{\n" +
" for (int j = 0; j < n - i - 1; j++)\n" +
" {\n");
codeDrawable->Texts->Append(" if (arr[j] > arr[j + 1]) \n");
codeDrawable->Texts->Append(" std::swap(arr[j], arr[j + 1]); \n");
codeDrawable->Texts->Append(" }\n" +
"}");
SpeedSlider->Value = 13; //默认滑块速度
auto n = (int)Executor->SortVector->Size; //数字总数
auto stateList = Executor->SortStates; //获取状态列表
AddRecoverStep(ref new Vector<int>()); //一开始加入一个空的步骤
for (auto i = 0; i < n - 1; i++)
{
for (auto j = 0; j < n - i - 1; j++)
{
AddCompareStep(j, j + 1, 1); //加入比较步骤
if (Executor->SortVector->GetAt(j) > Executor->SortVector->GetAt(j + 1)) { //如果后者更大
//交换j和j+1位置上的数字
int temp = Executor->SortVector->GetAt(j + 1);
Executor->SortVector->SetAt(j + 1, Executor->SortVector->GetAt(j));
Executor->SortVector->SetAt(j, temp);
//加入交换步骤
AddSwapStep(j, j + 1, 2);
}
stateList->SetAt(j, (int)PillarState::Default); //设置前者为默认状态
}
AddCompleteStep(ref new Vector<int>{ (int)n - i - 1 }); //设置本次的末尾完成
}
AddCompleteStep(ref new Vector<int>{ 0 }); //加入完成步骤
ProcessText->Text = "0/" + (Executor->StepList->Size - 1).ToString(); //在初始化完毕后设置文本
ProgressSlider->Maximum = Executor->StepList->Size - 1; //设置进度滑块最大值
}
/// <summary>
/// 初始化选择排序算法
/// </summary>
void AlgorithmVisualization::SortAlgoPage::InitSelectionSort()
{
Introduction->Text = L"时间复杂度:O(n²)\n它的工作原理是:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。选择排序是不稳定的排序方法。";
auto codeDrawable = Executor->CodeDrawable;
codeDrawable->Texts->Clear();
codeDrawable->Texts->Append("for (int i = 0; i < n - 1; ++i) {\n int min = i;\n for (int j = i + 1; j < n; ++j) {\n");
codeDrawable->Texts->Append(" if (arr[j] < arr[min]) min = j; \n");
codeDrawable->Texts->Append(" }\n");
codeDrawable->Texts->Append(" swap(arr[i], arr[min]); \n");
codeDrawable->Texts->Append("}");
SpeedSlider->Value = 13; //默认滑块速度
auto n = (int)Executor->SortVector->Size; //数字总数
auto stateList = Executor->SortStates; //获取状态列表
AddRecoverStep(ref new Vector<int>()); //一开始加入一个空的步骤
for (int i = 0; i < n - 1; ++i) {
int min = i; //初始化最小的下标
for (int j = i + 1; j < n; ++j) {
AddCompareStep(min, j, 1); //加入比较步骤
if (Executor->SortVector->GetAt(j) < Executor->SortVector->GetAt(min)) { //如果新的更小
stateList->SetAt(min, (int)PillarState::Default); //取消之前的最小值
min = j;
}
stateList->SetAt(j, (int)PillarState::Default); //设置前者为默认状态
}
//交换i和min位置上的数字
int temp = Executor->SortVector->GetAt(i);
Executor->SortVector->SetAt(i, Executor->SortVector->GetAt(min));
Executor->SortVector->SetAt(min, temp);
//加入交换步骤
AddSwapStep(i, min, 3);
AddRecoverStep(ref new Vector<int>{ i, min });
AddCompleteStep(ref new Vector<int>{ i }); //加入完成步骤
}
AddCompleteStep(ref new Vector<int>{ n - 1 }); //加入完成步骤
ProcessText->Text = "0/" + (Executor->StepList->Size - 1).ToString(); //在初始化完毕后设置文本
ProgressSlider->Maximum = Executor->StepList->Size - 1; //设置进度滑块最大值
}
/// <summary>
/// 初始化插入排序算法
/// </summary>
void AlgorithmVisualization::SortAlgoPage::InitInsertionSort()
{
Introduction->Text = L"时间复杂度:O(n²)\n它的基本思想是将一个记录插入到已经排好序的有序表中,从而一个新的、记录数增1的有序表。在其实现过程使用双层循环,外层循环对除了第一个元素之外的所有元素,内层循环对当前元素前面有序表进行待插入位置查找,并进行移动。";
auto codeDrawable = Executor->CodeDrawable;
codeDrawable->Texts->Clear();
codeDrawable->Texts->Append("for (int i = 0; i < n; i++) {\n");
codeDrawable->Texts->Append(" for (int j = i; j > 0; j--) {\n");
codeDrawable->Texts->Append(" if (arr[j] < arr[j - 1]) \n");
codeDrawable->Texts->Append(" std::swap(arr[j], arr[j - 1]); \n");
codeDrawable->Texts->Append(" else break;\n");
codeDrawable->Texts->Append(" }\n");
codeDrawable->Texts->Append("}\n");
SpeedSlider->Value = 13; //默认滑块速度
auto n = (int)Executor->SortVector->Size; //数字总数
auto stateList = Executor->SortStates; //获取状态列表
AddRecoverStep(ref new Vector<int>()); //一开始加入一个空的步骤
for (int i = 0; i < n; i++) {
for (int j = i; j > 0; j--) {
AddCompareStep(j - 1, j, 2); //加入比较步骤
//恢复默认状态
stateList->SetAt(j, (int)PillarState::Default);
stateList->SetAt(j - 1, (int)PillarState::Default);
if (Executor->SortVector->GetAt(j) < Executor->SortVector->GetAt(j - 1)) //比较
{
//交换数据
int temp = Executor->SortVector->GetAt(j);
Executor->SortVector->SetAt(j, Executor->SortVector->GetAt(j - 1));
Executor->SortVector->SetAt(j - 1, temp);
AddSwapStep(j - 1, j, 3); //添加交换步骤
}
else break;
//恢复默认状态
stateList->SetAt(j - 1, (int)PillarState::Default);
stateList->SetAt(j, (int)PillarState::Default);
}
}
AddAllCompleteStep(); //最后全部完成
ProcessText->Text = "0/" + (Executor->StepList->Size - 1).ToString(); //在初始化完毕后设置文本
ProgressSlider->Maximum = Executor->StepList->Size - 1; //设置进度滑块最大值
}
/// <summary>
/// 初始化希尔排序算法
/// </summary>
void AlgorithmVisualization::SortAlgoPage::InitShellSort()
{
Introduction->Text = L"时间复杂度:O(n^(1.3~2))\n希尔排序是直接插入排序算法的一种更高效的改进版本,是非稳定排序算法。希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至 1 时,整个文件恰被分成一组,算法便终止。";
auto codeDrawable = Executor->CodeDrawable;
codeDrawable->Texts->Clear();
codeDrawable->Texts->Append("for (int gap = n / 2; gap > 0; gap /= 2) {\n");
codeDrawable->Texts->Append(" for (int i = gap; i < n; ++i) {\n");
codeDrawable->Texts->Append(" int temp = arr[i]; \n");
codeDrawable->Texts->Append(" int j;\n");
codeDrawable->Texts->Append(" for (j = i; j >= gap; j -= gap) {\n");
codeDrawable->Texts->Append(" if (temp < arr[j - gap]) \n");
codeDrawable->Texts->Append(" arr[j] = arr[j - gap]; \n");
codeDrawable->Texts->Append(" else break; \n");
codeDrawable->Texts->Append(" }\n");
codeDrawable->Texts->Append(" arr[j] = temp; \n");
codeDrawable->Texts->Append(" }\n");