private void Quicksort(int[] array, int left, int right) { ArrayIndexesEventArgs arrayIndexes = new ArrayIndexesEventArgs(); int i = left, j = right; int pivot = array[left + (right - left) / 2]; while (i <= j) { while (array[i].CompareTo(pivot) < 0) { i++; } while (array[j].CompareTo(pivot) > 0) { j--; } if (i <= j) { // Swap int tmp = array[i]; array[i] = array[j]; array[j] = tmp; arrayIndexes.FirstIndex = i; arrayIndexes.SecondIndex = j; SwapCount++; OnSwapRaisedEvent(arrayIndexes); Thread.Sleep(SortingSpeedDelay); i++; j--; } } if (left < j) { Quicksort(array, left, j); } if (i < right) { Quicksort(array, i, right); } }
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); }
private void ArrayIndexesChange(object sender, ArrayIndexesEventArgs e) { string threadName = Thread.CurrentThread.Name; this.Invoke(new MethodInvoker(() => { CustomDataGridView dataGridView = GetMainDataGridViewFromTabPage(tcSorters.TabPages[threadName]); if (dataGridView.FirstCellToRedraw != null && dataGridView.SecondCellToRedraw != null) { dataGridView[dataGridView.FirstCellToRedraw.Item2, dataGridView.FirstCellToRedraw.Item1].Style.BackColor = Color.White; dataGridView[dataGridView.SecondCellToRedraw.Item2, dataGridView.SecondCellToRedraw.Item1].Style.BackColor = Color.White; } dataGridView.FirstCellToRedraw = Utils.ConvertIndexOfOneDimensionalArray(e.FirstIndex, _formModel.ArrayForSorting.GetLength(0)); dataGridView.SecondCellToRedraw = Utils.ConvertIndexOfOneDimensionalArray(e.SecondIndex, _formModel.ArrayForSorting.GetLength(0)); dataGridView[dataGridView.FirstCellToRedraw.Item2, dataGridView.FirstCellToRedraw.Item1].Style.BackColor = Color.PaleVioletRed; dataGridView[dataGridView.SecondCellToRedraw.Item2, dataGridView.SecondCellToRedraw.Item1].Style.BackColor = Color.PaleVioletRed; dataGridView.Refresh(); })); }