static public void BubbleSort(int[] Array) { for (int i = 0; i < Array.Length - 1; i++) { for (int j = 0; j < Array.Length - i - 1; j++, CompareCount++) { if (Array[j] > Array[j + 1]) { DatabaseController.SetIteration(Array); Swap(ref Array[j], ref Array[j + 1]); } } } }
public string SortArray(DataGridView dgvArray, DataGridView dgvSortedArray, string btnSort) { Sort.ResetCounters(); int[] Array = GetArray(dgvArray); DatabaseController.CreateTable(); Stopwatch timer = new Stopwatch(); timer.Start(); CallSort(Array, btnSort); timer.Stop(); SetSortedArray(dgvSortedArray, Array); DatabaseController.SetIteration(Array); DatabaseController.AddSort(btnSort.Substring(3), timer.Elapsed.ToString(), Sort.GetCompareCount(), Sort.GetSwapCount(), Array); return(timer.Elapsed.ToString()); }
static public void SelectionSort(int[] Array) { for (int i = 0; i < Array.Length - 1; i++) { int min = i; for (int j = i + 1; j < Array.Length; j++, CompareCount++) { if (Array[j] < Array[min]) { min = j; } } DatabaseController.SetIteration(Array); Swap(ref Array[i], ref Array[min]); } }
/// <summary> /// Заполняет файл случайными числами и сортирует их /// </summary> /// <param name="N">колличество случайных чисел</param> /// <returns>Время сортировки</returns> public string SortRandomArray(int N) { string FileName = "Array.txt"; StreamWriter SW = new StreamWriter(FileName); Random rand = new Random(); for (int i = 0; i < N; i++) { SW.WriteLine(rand.Next(0, 100)); } SW.Close(); string Array; MergeSort.ResetCounters(); DatabaseController.CreateTable(); Stopwatch timer = new Stopwatch(); timer.Start(); Array = MergeSort.NaturalMergeSort(FileName); timer.Stop(); DatabaseController.AddSort("MergeSort", timer.Elapsed.ToString(), MergeSort.GetCompareCount(), MergeSort.GetSwapCount(), Array); MessageBox.Show("Файл Array.txt заполнен " + N + " случайными числами и успешно отсортирован!", "Сортировка"); return(timer.Elapsed.ToString()); }
public DatabaseForm(StartForm main) { InitializeComponent(); MainForm = main; DatabaseController.LoadData(dgvSortings); }
/// <summary> /// Сортирует числа в файле методом естественного слияния /// </summary> /// <param name="FileName">Полный путь к сортируемому файлу</param> static public string NaturalMergeSort(string FileName) { int s1, s2, a1, a2, mark; StreamReader SR, SR1, SR2; StreamWriter SW, SW1, SW2; s1 = 1; s2 = 1; a1 = 0; a2 = 0; string array; while (s1 > 0 && s2 > 0) { mark = 1; s1 = 0; s2 = 0; array = ToStringArray(new StreamReader(FileName)); DatabaseController.SetIteration(array); SR = new StreamReader(FileName); SW1 = new StreamWriter("f1.txt"); SW2 = new StreamWriter("f2.txt"); if (!SR.EndOfStream) { a1 = Convert.ToInt32(SR.ReadLine()); SW1.WriteLine(a1); } while (!SR.EndOfStream) { a2 = Convert.ToInt32(SR.ReadLine()); if (a2 < a1) { switch (mark) { case 1: SW1.WriteLine('\''); mark = 2; s1++; break; case 2: SW2.WriteLine('\''); mark = 1; s2++; break; } } CompareCount++; SwapCount++; if (mark == 1) { SW1.WriteLine(a2); s1++; } else { SW2.WriteLine(a2); s2++; } a1 = a2; } if (s2 > 0 && mark == 2) { SW2.WriteLine('\''); SwapCount++; } if (s1 > 0 && mark == 1) { SW1.WriteLine('\''); SwapCount++; } SR.Close(); SW1.Close(); SW2.Close(); SW = new StreamWriter(FileName); SR1 = new StreamReader("f1.txt"); SR2 = new StreamReader("f2.txt"); if (!SR1.EndOfStream) { a1 = Convert.ToInt32(SR1.ReadLine()); } if (!SR2.EndOfStream) { a2 = Convert.ToInt32(SR2.ReadLine()); } bool file1, file2; while (!SR1.EndOfStream && !SR2.EndOfStream) { file1 = false; file2 = false; while (!file1 && !file2) { if (a1 <= a2) { SW.WriteLine(a1); file1 = EndBlock(SR1); a1 = Convert.ToInt32(SR1.ReadLine()); } else { SW.WriteLine(a2); file2 = EndBlock(SR2); a2 = Convert.ToInt32(SR2.ReadLine()); } CompareCount++; } while (!file1) { SW.WriteLine(a1); file1 = EndBlock(SR1); a1 = Convert.ToInt32(SR1.ReadLine()); } while (!file2) { SW.WriteLine(a2); file2 = EndBlock(SR2); a2 = Convert.ToInt32(SR2.ReadLine()); } } file1 = false; file2 = false; while (!file1 && !SR1.EndOfStream) { SW.WriteLine(a1); file1 = EndBlock(SR1); a1 = Convert.ToInt32(SR1.ReadLine()); } while (!file2 && !SR2.EndOfStream) { SW.WriteLine(a2); file2 = EndBlock(SR2); a2 = Convert.ToInt32(SR2.ReadLine()); } SW.Close(); SR1.Close(); SR2.Close(); } File.Delete("f1.txt"); File.Delete("f2.txt"); array = ToStringArray(new StreamReader(FileName)); DatabaseController.SetIteration(array); return(array); }