static public void QuickSort(int[] Array, int Start, int End) { if (Start >= End) { return; } int Border = Start; int Pivot = (new Random()).Next(Start, End + 1); for (int i = Start; i <= End; i++, CompareCount++) { if (Array[i] < Array[Pivot]) { DatabaseController.SetIteration(Array); Swap(ref Array[i], ref Array[Border]); if (Border == Pivot) { Pivot = i; } Border++; } } DatabaseController.SetIteration(Array); Swap(ref Array[Pivot], ref Array[Border]); QuickSort(Array, Start, Border - 1); QuickSort(Array, Border + 1, End); }
static public void ShakerSort(int[] Array) { int Left = 0; int Right = Array.Length - 1; while (Left <= Right) { for (int i = Left; i < Right; i++, CompareCount++) { if (Array[i] > Array[i + 1]) { DatabaseController.SetIteration(Array); Swap(ref Array[i], ref Array[i + 1]); } } Right--; CompareCount += Right - Left; for (int i = Right; i > Left; i--, CompareCount++) { if (Array[i - 1] > Array[i]) { DatabaseController.SetIteration(Array); Swap(ref Array[i - 1], ref Array[i]); } } Left++; } }
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="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); }