public T[] Sort(T[] array, RichTextBox textBox, IComparableLab <T> comparator, bool direction, bool enableOutput) { Direction dir; if (direction) { dir = comparator.More; } else { dir = comparator.Less; } for (int j = 0; j < array.Length; j++) { for (int i = 0; i < array.Length - 1; i++) { if (dir(array[i], array[i + 1])) { T tmp = array[i]; array[i] = array[i + 1]; array[i + 1] = tmp; } } if (enableOutput) { Print(array, textBox, ("Step " + (j + 1) + ":")); } } return(array); }
public T[] Sort(T[] array, RichTextBox textBox, IComparableLab <T> comparator, bool direction, bool enableOutput) { Direction dir; if (direction) { dir = comparator.More; } else { dir = comparator.Less; } FindNeg(array, out int first, out int last, comparator); for (int i = first; i < last; i++) { int foundValId = FindElByDir(array, i, dir); if (foundValId != i) { T tmp = array[i]; array[i] = array[foundValId]; array[foundValId] = tmp; } if (enableOutput) { Print(array, textBox, ("Step " + (i + 1) + ":")); } } return(array); }
public bool ArrayCheck(T[] array, IComparableLab <T> comparator) { FindMax(array, comparator, out int ind); if (ind < array.Length - 1) { return(true); } return(false); }
private T[] AfterAction(T[] old, T[] sortedPart, IComparableLab <T> comparable) { FindNeg(old, out int first, out int last, comparable); int iterator = 0; for (int i = first; i < last; i++) { old[i] = sortedPart[iterator]; iterator++; } return(old); }
private T[] PreAction(T[] array, IComparableLab <T> comparable) { FindNeg(array, out int first, out int last, comparable); T[] result = new T[last - first]; int iterator = 0; for (int i = first; i < last; i++) { result[iterator] = array[i]; iterator++; } return(result); }
public T[] Sort(T[] array, RichTextBox textBox, IComparableLab <T> comparator, bool direction, bool enableOutput) { Direction dir; if (direction) { dir = comparator.More; } else { dir = comparator.Less; } RecursiveSort(ref array, 0, array.Length - 1, comparator, textBox, dir, enableOutput); return(array); }
private T FindMax(T[] array, IComparableLab <T> comparator, out int ind) { T max = array[0]; ind = 0; for (int i = 1; i < array.Length; i++) { if (comparator.Less(max, array[i])) { max = array[i]; ind = i; } } return(max); }
private void FindNeg(T[] array, out int first, out int last, IComparableLab <T> comparable) { first = 0; last = array.Length; for (int i = 0; i < array.Length; i++) { if (comparable.Less(array[i], 0)) { if (first == -1) { first = i; } last = i; } } }
public T[] Sort(T[] array, RichTextBox textBox, IComparableLab <T> comparator, bool direction, bool enableOutput) { Dictionary <T, int> count = new Dictionary <T, int>(); foreach (T el in array) { if (count.ContainsKey(el)) { count[el]++; } else { count.Add(el, 0); } } count = direction ? count.OrderBy((x) => x.Key).ToDictionary((x) => x.Key, x => x.Value) : count.OrderByDescending((x) => x.Key).ToDictionary((x) => x.Key, x => x.Value); int[] tmp = count.Values.ToArray(); for (int j = 0; j < tmp.Length; j++) { tmp[j]++; } if (enableOutput) { Print(count.Keys.ToArray(), textBox, "Дані: "); AbstractPrinter <int> printer = new CountSort <int>(); printer.Print(tmp, textBox, "Зустрічаються (разів): "); } int i = 0; foreach (KeyValuePair <T, int> el in count) { for (int j = 0; j <= count[el.Key]; j++) { array[i] = el.Key; i++; } } return(array); }
private void RecursiveSort(ref T[] array, int start, int end, IComparableLab <T> comparator, RichTextBox textBox, Direction direction, bool enableOutput) { if (start == end || start < 0) { return; } if (start < end) { T pivot = array[end]; T tmp; int currentInd = start; for (int i = start; i < end; i++) { if (direction(array[i], pivot)) { tmp = array[currentInd]; array[currentInd] = array[i]; array[i] = tmp; currentInd++; } } _step++; if (enableOutput) { string info = "Step: " + _step + "="; Print(array, textBox, info); } tmp = array[end]; array[end] = array[currentInd]; array[currentInd] = tmp; if (currentInd - 1 > start) { RecursiveSort(ref array, start, currentInd - 1, comparator, textBox, direction, enableOutput); } if (currentInd != end && currentInd < end) { RecursiveSort(ref array, currentInd + 1, end, comparator, textBox, direction, enableOutput); } } }
public bool ArrayCheck(int[] array, IComparableLab <int> comparator) { int negCount = 0; foreach (int el in array) { if (el < 0) { negCount++; } } if (negCount >= 2) { return(true); } return(false); }
public T[] Sort(T[] array, RichTextBox textBox, IComparableLab <T> comparator, bool direction, bool enableOutput) { Direction dir; if (direction) { dir = comparator.More; } else { dir = comparator.Less; } bool flag; T tmp; for (int j = 0; j < array.Length; j++) { flag = false; for (int i = 0; i < array.Length - 1; i++) { if (dir(array[i], array[i + 1])) { tmp = array[i]; array[i] = array[i + 1]; array[i + 1] = tmp; flag = true; } } if (enableOutput) { Print(array, textBox, ("Step " + (j + 1) + "flag: " + flag.ToString() + ":")); } if (!flag) { break; } } return(array); }
public T[] Sort(T[] array, RichTextBox textBox, IComparableLab <T> comparator, bool direction, bool enableOutput) { Direction dir; if (direction) { dir = comparator.More; } else { dir = comparator.Less; } FindMax(array, comparator, out int ind); T[] tmp = new T[array.Length - ind]; for (int i = ind; i < array.Length; i++) { tmp[i - ind] = array[i]; } return(RecMerge(tmp, dir, textBox, enableOutput)); //TODO: Change here before 7 Lab return RecMerge(array, dir, textBox) }
public T[] Sort(T[] array, RichTextBox textBox, IComparableLab <T> comparator, bool direction, bool enableOutput) { Direction dir; if (direction) { dir = comparator.More; } else { dir = comparator.Less; } int step = array.Length / 2; int stepInfo = 1; while (step > 0) { for (int i = step; i < array.Length; i++) { for (int j = i - step; j >= 0 && dir(array[j], array[j + step]); j -= step) { T tmp = array[j]; array[j] = array[j + step]; array[j + step] = tmp; } } if (enableOutput) { Print(array, textBox, ("Step: " + stepInfo + "= ")); } stepInfo++; step /= 2; } return(array); }
public bool ArrayCheck(T[] array, IComparableLab <T> comparator) { return(true); }
private void buttonSort_Click(object sender, EventArgs e) { this.tmp.CopyTo(array, 0); int tmp = _radioButtonList.FindIndex(button => button.Checked); switch (tmp) { case 0: { _comparator = new NumberComparator(); _sortable = new BubbleSorter <double>(); } break; case 1: { _comparator = new NumberComparator(); _sortable = new FlagBubble <double>(); } break; case 2: { _comparator = new NumberComparator(); _sortable = new SimpleSelection <double>(); } break; case 3: { _comparator = new NumberComparator(); _sortable = new QuickSort <double>(); } break; case 4: { IComparableLab <CustomDoubleArray> comparator = new CustomDoubleArrayComparator(); ISortable <CustomDoubleArray> sortable = new ShellSort <CustomDoubleArray>(); if (sortable.ArrayCheck(CustomDoubleArray, comparator)) { tmpArrayHolder.Text = ""; sortedArrayHolder.Text = ""; CustomDoubleArray = sortable.Sort(CustomDoubleArray, tmpArrayHolder, comparator, true, true); sortable.Print(CustomDoubleArray, sortedArrayHolder, ""); } else { tmpLabel.Text = "Please reenter array, because it does not fit the condition!!"; } } return; case 5: { _comparator = new NumberComparator(); _sortable = new MergeSort <double>(); _direction = false; } break; case 6: { int [] intArray = new int[array.Length]; for (int i = 0; i < intArray.Length; i++) { intArray[i] = (int)array[i]; } intArray = Lab6ArrayHandler.Execute(intArray); IComparableLab <int> comparable = new NumberComparator(); ISortable <int> sortable = new CountSort <int>(); _direction = false; if (sortable.ArrayCheck(intArray, comparable)) { tmpArrayHolder.Text = ""; sortedArrayHolder.Text = ""; sortable.Print(intArray, tmpArrayHolder, "Масив після виконання індивідуального завдання: \n"); intArray = sortable.Sort(intArray, tmpArrayHolder, comparable, _direction, true); sortable.Print(intArray, sortedArrayHolder, ""); } else { tmpLabel.Text = "Please reenter array, because it does not fit the condition!!"; } } return; default: { throw new IndexOutOfRangeException(); } } if (_sortable.ArrayCheck(array, _comparator)) { tmpArrayHolder.Text = ""; sortedArrayHolder.Text = ""; array = _sortable.Sort(array, tmpArrayHolder, _comparator, _direction, true); _sortable.Print(array, sortedArrayHolder, ""); } else { tmpLabel.Text = "Please reenter array, because it does not fit the condition!!"; } }