From 7c92c360770268fb029f28d56f8fcd9005ce0a15 Mon Sep 17 00:00:00 2001 From: offspring131313 Date: Tue, 24 May 2011 02:12:30 +0000 Subject: [PATCH] Added Mergesort for Playmovie column sorting for when the number of elements is over 15. ListView currently does not update when the code uses the mergesort portion and not just the insertion sort portion. --- BizHawk.MultiClient/PlayMovie.cs | 42 +++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/BizHawk.MultiClient/PlayMovie.cs b/BizHawk.MultiClient/PlayMovie.cs index 9e49565324..5a99b5785b 100644 --- a/BizHawk.MultiClient/PlayMovie.cs +++ b/BizHawk.MultiClient/PlayMovie.cs @@ -202,17 +202,57 @@ namespace BizHawk.MultiClient string columnName = MovieView.Columns[columnToOrder].Text; if (sortedCol.CompareTo(columnName) != 0) sortReverse = false; - InsertionSort(MovieList, columnName); + MergeSort(MovieList, 0, MovieList.Count, columnName); sortedCol = columnName; sortReverse = !(sortReverse); MovieView.Refresh(); } + private void MergeSort(List movieList, int low, int high, string parameter) + { + int sort = 1; + if (sortReverse) + sort = -1; + + if (high - low < 15) + InsertionSort(movieList, parameter); + else + { + int mid = Convert.ToInt32(((high - low) / 2) + low); + MergeSort(movieList, low, mid, parameter); + MergeSort(movieList, mid + 1, high, parameter); + + int trackLow = low; + int trackHigh = mid+1; + List sortedList = new List(); + for (int i = low; i <= high ; i++) + { + + if ((trackHigh > high) || (movieList[trackLow].CompareTo(movieList[trackHigh], parameter) == sort)) + { + sortedList.Add(movieList[trackLow]); + trackLow++; + } + else + { + sortedList.Add(movieList[trackHigh]); + trackHigh++; + } + } + + for (int i = low; i <= high; i++) + { + movieList[i] = sortedList[i]; + } + } + } + private void InsertionSort(List smallList, string parameter) { int sort = 1; if (sortReverse) sort = -1; + for (int i = 0; i < smallList.Count; i++) { int smallest = i;