public void DoAlgorithm(MyMass <T> data, bool flagOfSort) { bool fl = flagOfSort; List <T> mass = data.Mass; for (int j = 0; j < mass.Count; j++) { for (int i = 0; i < mass.Count - 1; i++) { if (fl) { if (_comparer.Compare(_selector(mass[i]), _selector(mass[i + 1])) == 1) { var temp = mass[i + 1]; mass[i + 1] = mass[i]; mass[i] = temp; } } else { if (_comparer.Compare(_selector(mass[i]), _selector(mass[i + 1])) == -1) { var temp = mass[i + 1]; mass[i + 1] = mass[i]; mass[i] = temp; } } } } data.Mass = mass; }
public void DoAlgorithm(MyMass <T> data, bool flagOfSort) { var array = data.Mass; for (int i = 1; i < array.Count; i++) { int j; var buf = array[i]; for (j = i - 1; j >= 0; j--) { if (flagOfSort) { if ((_comparer.Compare(_selector(array[j]), _selector(buf)) == -1)) { break; } array[j + 1] = array[j]; } else { if ((_comparer.Compare(_selector(array[j]), _selector(buf)) == 1)) { break; } array[j + 1] = array[j]; } } array[j + 1] = buf; } }
public void DoAlgorithm(MyMass <T> data, bool flagOfSort) { var arr = data.Mass; int start = 0; int end = arr.Count - 1; quicksort(arr, start, end, flagOfSort); }
public void DoAlgorithm(MyMass <T> data, bool flagOfSort) { flag = flagOfSort; var arr = data.Mass.ToArray(); arr = Pyramid_Sort(arr, arr.Length); data.Mass = new List <T>(arr); }
public void DoAlgorithm(MyMass <T> data, bool flagOfSort) { var mass = data.Mass; flag = flagOfSort; var newMass = Merge_Sort(mass.ToArray()); mass = new List <T>(newMass); data.Mass = mass; }
public object Clone() { List <T> mass = new List <T>(); foreach (var element in _mass) { mass.Add(new T()); mass[mass.Count - 1] = element; } var res = new MyMass <T>(mass); return(res); }
public void DoAlgorithm(MyMass <T> data, bool flagOfSort) { int min; T temp; var arr = data.Mass; int length = arr.Count; for (int i = 0; i < length - 1; i++) { min = i; for (int j = i + 1; j < length; j++) { if (flagOfSort) { if (_comparer.Compare(_selector(arr[j]), _selector(arr[min])) == -1) { min = j; } } else { if (_comparer.Compare(_selector(arr[j]), _selector(arr[min])) == 1) { min = j; } } } if (min != i) { temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } } }
static void Main(string[] args) { ShowInfo(); MyStruct[] arr = new[] { new MyStruct(4), new MyStruct(40), new MyStruct(87), new MyStruct(8), new MyStruct(4), new MyStruct(2), new MyStruct(78), new MyStruct(18), new MyStruct(5), new MyStruct(13), new MyStruct(6), new MyStruct(1) }; List <MyStruct> mas = new List <MyStruct>(arr); MyMass <MyStruct> m = new MyMass <MyStruct>(mas); var mc = m.Clone(); //var strat = new Context(); Console.WriteLine("Не отсортированный массив " + m.ToString()); //strat.SetStrategy(new BubleSort()); //strat.DoSomeLogic(m, new IntComparep()); var strat = new BubleSort <MyStruct>(new IntComparep(), GetInt); strat.DoAlgorithm(m, true); Console.WriteLine("Buble sort " + m.ToString()); strat.DoAlgorithm(m, false); Console.WriteLine("Reverse buble sort " + m.ToString()); var strat1 = new MergeSort <MyStruct>(new IntComparep(), GetInt); strat1.DoAlgorithm(m, true); Console.WriteLine("Merge sort " + m.ToString()); strat1.DoAlgorithm(m, false); Console.WriteLine("Reverse merge sort " + m.ToString()); var strat2 = new QuickSort <MyStruct>(new IntComparep(), GetInt); strat2.DoAlgorithm(m, true); Console.WriteLine("Quick sort " + m.ToString()); strat2.DoAlgorithm(m, false); Console.WriteLine("Quick buble sort " + m.ToString()); var strat3 = new HeapSort <MyStruct>(new IntComparep(), GetInt); strat3.DoAlgorithm(m, true); Console.WriteLine("heap sort " + m.ToString()); strat3.DoAlgorithm(m, false); Console.WriteLine("Reverse heap sort " + m.ToString()); var strat5 = new SelectionSort <MyStruct>(new IntComparep(), GetInt); strat5.DoAlgorithm(m, true); Console.WriteLine("Selection sort " + m.ToString()); strat5.DoAlgorithm(m, false); Console.WriteLine("Reverse selection sort " + m.ToString()); var strat4 = new IsertionSort <MyStruct>(new IntComparep(), GetInt); strat4.DoAlgorithm(m, true); Console.WriteLine("Insertion sort " + m.ToString()); strat4.DoAlgorithm(m, false); Console.WriteLine("Reverse insertion sort " + m.ToString()); //strat.SetStrategy(new SelectionSort()); //strat.DoSomeLogic(m, new IntComparep()); //Console.WriteLine("Selection sort " + m.ToString()); //strat.DoSomeLogic(m, new IntComparep(), false); //Console.WriteLine("Reverse Selection sort " + m.ToString()); //strat.SetStrategy(new IsertionSort()); //strat.DoSomeLogic(m, new IntComparep()); //Console.WriteLine("Insertion sort " + m.ToString()); //strat.DoSomeLogic(m, new IntComparep(), false); //Console.WriteLine("Reverse insertion sort " + m.ToString()); //strat.SetStrategy(new QuickSort()); //strat.DoSomeLogic(m, new IntComparep()); //Console.WriteLine("Quick sort " + m.ToString()); //strat.DoSomeLogic(m, new IntComparep(), false); //Console.WriteLine("Reverse quick sort " + m.ToString()); //strat.SetStrategy(new MergeSort()); //strat.DoSomeLogic(m, new IntComparep()); //Console.WriteLine("Merge sort " + m.ToString()); //strat.DoSomeLogic(m, new IntComparep(), false); //Console.WriteLine("Reverse merge sort " + m.ToString()); //strat.SetStrategy(new HeapSort()); //strat.DoSomeLogic(m, new IntComparep()); //Console.WriteLine("Heap sort " + m.ToString()); //strat.DoSomeLogic(m, new IntComparep(), false); //Console.WriteLine("Reverse heap sort " + m.ToString()); }