Odd-even sort is a relatively simple sorting algorithm. It is a comparison sort based on bubble sort with which it shares many characteristics. It functions by comparing all (odd, even)-indexed pairs of adjacent elements in the list and, if a pair is in the wrong order (the first is larger than the second) the elements are switched. The next step repeats this for (even, odd)-indexed pairs (of adjacent elements). Then it alternates between (odd, even) and (even, odd) steps until the list is sorted. It can be thought of as using parallel processors, each using bubblesort but starting at different points in the list (all odd indices for the first step). This sorting algorithm is only marginally more difficult than bubble sort to implement.

public IList OddEvenSort(IList arrayToSort) { bool sorted = false; while (!sorted) { sorted = true; for (var i = 1; i < arrayToSort.Count - 1; i += 2) { if (((IComparable)arrayToSort[i]).CompareTo(arrayToSort[i + 1]) > 0) { object temp = arrayToSort[i]; arrayToSort[i] = arrayToSort[i + 1]; RedrawItem(i); arrayToSort[i + 1] = temp; RedrawItem(i+1); pnlSamples.Refresh(); if (chkCreateAnimation.Checked) SavePicture(); sorted = false; } } for (var i = 0; i < arrayToSort.Count - 1; i += 2) { if (((IComparable)arrayToSort[i]).CompareTo(arrayToSort[i + 1]) > 0) { object temp = arrayToSort[i]; arrayToSort[i] = arrayToSort[i + 1]; arrayToSort[i + 1] = temp; RedrawItem(i); RedrawItem(i+1); pnlSamples.Refresh(); if (chkCreateAnimation.Checked) SavePicture(); sorted = false; } } } return arrayToSort; }

## No comments:

## Post a Comment