1- using System ;
2- using System . Collections . Generic ;
3- using System . Text ;
1+ using System . Collections . Generic ;
42
53namespace Algorithms . NET . Sorting . MergeSort
64{
@@ -14,7 +12,7 @@ public class MergeSortAlgorithm
1412 public static List < double > SortAscending ( List < double > list )
1513 {
1614 List < double > sortedList = new List < double > ( list ) ;
17- return Sort ( sortedList , false ) ;
15+ return Sort ( sortedList , false ) ;
1816 }
1917
2018 /// <summary>
@@ -34,7 +32,7 @@ public static List<double> SortDescending(List<double> list)
3432 /// <param name="list">List of numbers to sort</param>
3533 /// <param name="sortDescending">Boolean value specifying whether sorting should be done in descending order</param>
3634 /// <returns>A sorted list</returns>
37- private static List < double > Sort ( List < double > list , bool sortDescending )
35+ private static List < double > Sort ( List < double > list , bool sortDescending )
3836 {
3937 //We stop recursion when size is 1, which means an array of one element is sorted.
4038 if ( list . Count < 2 )
@@ -47,15 +45,15 @@ private static List<double> Sort(List<double> list,bool sortDescending)
4745 List < double > right = new List < double > ( ) ;
4846
4947 //Fill each half
50- for ( int i = 0 ; i < middle ; i ++ )
48+ for ( int i = 0 ; i < middle ; i ++ )
5149 left . Add ( list [ i ] ) ;
5250
53- for ( int j = middle ; j < list . Count ; j ++ )
51+ for ( int j = middle ; j < list . Count ; j ++ )
5452 right . Add ( list [ j ] ) ;
5553
5654 //Sort each half.
57- Sort ( left , sortDescending ) ;
58- Sort ( right , sortDescending ) ;
55+ Sort ( left , sortDescending ) ;
56+ Sort ( right , sortDescending ) ;
5957
6058 //Merge the results
6159 Merge ( left , right , list , sortDescending ) ;
@@ -70,24 +68,24 @@ private static List<double> Sort(List<double> list,bool sortDescending)
7068 /// <param name="rightHalf">A list of the right half</param>
7169 /// <param name="list">Original list in which we want to insert items correctly</param>
7270 /// <param name="sortDescending">Boolean value of whether sorting should be done in descending order or not</param>
73- private static void Merge ( List < double > leftHalf , List < double > rightHalf , List < double > list , bool sortDescending )
71+ private static void Merge ( List < double > leftHalf , List < double > rightHalf , List < double > list , bool sortDescending )
7472 {
7573 int i = 0 , j = 0 , k = 0 ;
76-
74+
7775 //We insert the first items in the correct order by comparing them to each others
78- while ( i < leftHalf . Count && j < rightHalf . Count )
76+ while ( i < leftHalf . Count && j < rightHalf . Count )
7977 {
80- if ( ! sortDescending && leftHalf [ i ] < rightHalf [ j ] )
78+ if ( ! sortDescending && leftHalf [ i ] < rightHalf [ j ] )
8179 list [ k ++ ] = leftHalf [ i ++ ] ;
82- else
80+ else
8381 list [ k ++ ] = rightHalf [ j ++ ] ;
8482 }
8583
8684 //We insert the remaining items
87- while ( j < rightHalf . Count )
85+ while ( j < rightHalf . Count )
8886 list [ k ++ ] = rightHalf [ j ++ ] ;
8987
90- while ( i < leftHalf . Count )
88+ while ( i < leftHalf . Count )
9189 list [ k ++ ] = leftHalf [ i ++ ] ;
9290 }
9391 }
0 commit comments