Selectionsort

public override IEnumerable < T > Sort < T > (IComparer < T > sortFunction, IEnumerable < T > collection) {
	List < T > objectList = collection.ToList();

	for (int currentIndex = 0; currentIndex < objectList.Count; currentIndex++) {
		//Always select the current index at first.
		int currentMinIndex = currentIndex;

		//Determine the min object in the list.
		for (int minIndex = currentIndex + 1; minIndex < objectList.Count; minIndex++) {
			if (sortFunction.Compare(objectList[currentMinIndex], objectList[minIndex]) > 0) currentMinIndex = minIndex;
		}

		//Swap the min object with the current position.
		//Swap with the same position won't have an effect.
		objectList.Swap(currentIndex, currentMinIndex);
	}

	return objectList;
}
public static void Swap(this IList list, int x, int y) {
	object buffer = list[y];
	list[y] = list[x];
	list[x] = buffer;
}
0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments