public override void Sort(int[] array) { int step = array.Length / 2; while (step > 0) { int i, j; for (i = step; i < array.Length; i++) { int current = array[i]; for (j = i - step; j >= 0 && array[j] > current; j -= step) { array[j + step] = array[j]; } array[j + step] = current; } step /= 2; } SortedArrayEventArgs sortedArray = new SortedArrayEventArgs(); sortedArray.SortedArray = array; OnSortingOverEvent(sortedArray); }
public override void Sort(int[] array) { Array.Sort(array); SortedArrayEventArgs sortedArray = new SortedArrayEventArgs(); sortedArray.SortedArray = array; OnSortingOverEvent(sortedArray); }
private void ArraySortingCompleted(object sender, SortedArrayEventArgs e) { _completedSortsCounter++; Sorter sorter = (Sorter)sender; tsslStatus.Text = sorter.ToString() + MessagesMainFormView.SorterFinishedSortingText; if (_completedSortsCounter == _formModel.SelectedSorters.Length) { _completedSortsCounter = 0; SortingIsStarted = false; tsslStatus.Text = MessagesMainFormView.SortingCompleteText; tbSortersSpeedRegulator.Invoke((Action) delegate { tbSortersSpeedRegulator.Value = tbSortersSpeedRegulator.Maximum; MessageBox.Show(MessagesMainFormView.SortingCompleteText, MessagesMainFormView.SortingProcessCaption, MessageBoxButtons.OK, MessageBoxIcon.Information); }); } if (!_arrayIdFormDatabase.HasValue) { return; } Database.SortedArrays sortedArray = new SortedArrays { iInputArrayId = (int)_arrayIdFormDatabase, fSortingTime = sorter.SortingTime.TotalSeconds, vcSorterName = sorter.ToString(), vcSortedArrayContent = string.Join(" ", e.SortedArray) }; try { _formController.AddSortedArrayToDatabase(sortedArray); } catch (DatabaseDoesntExistException exception) { MessageBox.Show(exception.Message, MessagesMainFormView.DatabaseConnection, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (DBConcurrencyException exception) { MessageBox.Show(exception.Message, MessagesMainFormView.DatabaseConnection, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public override void Sort(int[] array) { SortedArrayEventArgs sortedArrayArg = new SortedArrayEventArgs(); ArrayIndexesEventArgs arrayIndexes = new ArrayIndexesEventArgs(); int tmp; int smallestIndex; try { _sortingTime.Restart(); for (int index = 0; index < array.Length - 1; index++) { smallestIndex = index; for (int minIndex = index + 1; minIndex < array.Length; minIndex++) { if (array[minIndex] < array[smallestIndex]) { smallestIndex = minIndex; } } tmp = array[smallestIndex]; array[smallestIndex] = array[index]; array[index] = tmp; arrayIndexes.FirstIndex = smallestIndex; arrayIndexes.SecondIndex = index; SwapCount++; OnSwapRaisedEvent(arrayIndexes); Thread.Sleep(SortingSpeedDelay); } } finally { _sortingTime.Stop(); } sortedArrayArg.SortedArray = array; OnSortingOverEvent(sortedArrayArg); }
public override void Sort(int[] array) { ArrayIndexesEventArgs arrayIndexes = new ArrayIndexesEventArgs(); SortedArrayEventArgs sortedArrayArg = new SortedArrayEventArgs(); try { _sortingTime.Restart(); for (int counter = 0; counter < array.Length - 1; counter++) { int index = counter + 1; while (index > 0) { if (array[index - 1] > array[index]) { int temp = array[index - 1]; array[index - 1] = array[index]; array[index] = temp; arrayIndexes.FirstIndex = index - 1; arrayIndexes.SecondIndex = index; SwapCount++; OnSwapRaisedEvent(arrayIndexes); Thread.Sleep(SortingSpeedDelay); } index--; } } } finally { _sortingTime.Stop(); } sortedArrayArg.SortedArray = array; OnSortingOverEvent(sortedArrayArg); }
// with each iteration j moves a maximum or minimum element (depending on the sign > or < at if) to the end of the array public override void Sort(int[] array) { SortedArrayEventArgs sortedArrayArg = new SortedArrayEventArgs(); ArrayIndexesEventArgs arrayIndexes = new ArrayIndexesEventArgs(); bool wasSwapped = true; // nothing has changed, which means that the array is already sorted try { _sortingTime.Restart(); for (int i = 1; (i <= array.Length) && wasSwapped; i++) { wasSwapped = false; for (int j = 0; j < array.Length - i; j++) { if (array[j] > array[j + 1]) { int tmp = array[j]; array[j] = array[j + 1]; array[j + 1] = tmp; wasSwapped = true; arrayIndexes.FirstIndex = j; arrayIndexes.SecondIndex = j + 1; SwapCount++; OnSwapRaisedEvent(arrayIndexes); Thread.Sleep(SortingSpeedDelay); } } } } finally { _sortingTime.Stop(); } sortedArrayArg.SortedArray = array; OnSortingOverEvent(sortedArrayArg); }
public override void Sort(int[] array) { SortedArrayEventArgs sortedArrayArg = new SortedArrayEventArgs(); try { _sortingTime.Restart(); Quicksort(array, 0, array.Length - 1); } finally { _sortingTime.Stop(); } sortedArrayArg.SortedArray = array; OnSortingOverEvent(sortedArrayArg); }