22using System . Collections . Generic ;
33
44static class Sorting < T > where T : IComparable < T >
5+ {
6+
7+ // Helper method
8+ private static void Swap ( List < T > listP , int lhs , int rhs )
59 {
6-
7- // Insertion Algorithm
8- public static void InsertionSort ( List < T > listP )
10+ T temp = listP [ lhs ] ;
11+ listP [ lhs ] = listP [ rhs ] ;
12+ listP [ rhs ] = temp ;
13+ }
14+ public static bool IsSorted ( List < T > listP )
15+ {
16+ bool isSorted = true ;
17+ for ( int i = 0 ; i < listP . Count - 1 ; i ++ )
918 {
10- int swapOperations = 0 ;
11- // Can be ignored, is simply here
12- // to count number of time we
13- // swap values.
14- Console . WriteLine ( "----------- Insertion Sort -------" ) ;
15- Displaying < T > . DisplayHeader ( listP , 0 , listP . Count ) ;
19+ if ( listP [ i ] . CompareTo ( listP [ i + 1 ] ) > 0 ) {
20+ isSorted = false ;
21+ }
22+ }
23+ return isSorted ;
24+ }
25+
26+ // Done with helper method.
1627
28+ // Insertion Algorithm
29+ public static void InsertionSort ( List < T > listP )
30+ {
1731 T current ;
1832 int slot ;
1933 for ( int bar = 1 ; bar < listP . Count ; bar ++ )
2034 {
2135 current = listP [ bar ] ;
2236 for ( slot = bar ; slot > 0 && current . CompareTo ( listP [ slot - 1 ] ) < 0 ; slot -- )
2337 {
24- swapOperations ++ ;
2538 listP [ slot ] = listP [ slot - 1 ] ;
2639 }
2740 listP [ slot ] = current ;
2841 }
29-
30- Displaying < T > . Display ( listP ) ;
31- Console . WriteLine ( "Count = {0}" , swapOperations ) ;
3242 }
3343 // Done with insertion Algorithm
3444
3545 // Helper methods for Heapsort
36- private static void Swap ( List < T > listP , int lhs , int rhs )
37- {
38- T temp = listP [ lhs ] ;
39- listP [ lhs ] = listP [ rhs ] ;
40- listP [ rhs ] = temp ;
41- }
42-
4346 private static int LeftChild ( int i )
4447 {
4548 return 2 * i + 1 ;
@@ -49,32 +52,24 @@ private static int LeftChild(int i)
4952 // Heapsort Algorithm
5053 public static void Heapsort ( List < T > listP )
5154 {
52- Console . WriteLine ( " --- Starting HeapSort ----" ) ;
5355 Heapsort ( listP , listP . Count ) ;
5456 }
5557
5658 private static void Heapsort ( List < T > listP , int N )
5759 {
58- Displaying < T > . DisplayHeader ( listP , 0 , listP . Count ) ;
59- Displaying < T > . Display ( listP ) ;
60-
6160 for ( int i = N / 2 ; i >= 0 ; i -- ) /* BuildHeap */
6261 PercDown ( listP , i , N ) ;
63- Console . WriteLine ( "-- Max Heap is built --" ) ;
64- Displaying < T > . Display ( listP ) ;
6562 for ( int i = N - 1 ; i > 0 ; i -- )
6663 {
6764 Swap ( listP , 0 , i ) ; /* DeleteMax */
6865 PercDown ( listP , 0 , i ) ;
69- Displaying < T > . Display ( listP ) ;
7066 }
7167 }
7268
7369 private static void PercDown ( List < T > listP , int i , int N )
7470 {
7571 int Child ;
7672 T current ;
77-
7873 for ( current = listP [ i ] ; LeftChild ( i ) < N ; i = Child )
7974 {
8075 Child = LeftChild ( i ) ;
@@ -92,132 +87,42 @@ private static void PercDown(List<T> listP, int i, int N)
9287
9388 // Done with heapsort Algorithm
9489
95-
96-
97-
98-
99- // ================================================================================
100- //
101- // Bubble Algorithm
102- //
103- // ================================================================================
90+ // Bubble Algorithm
10491 public static void BubbleSort ( List < T > listP )
10592 {
106- int count = 0 ;
107- Console . WriteLine ( "----------- Bubble Sort --- Green - bubbled to correct spot starting at end" ) ;
108- Displaying < T > . DisplayHeader ( listP , 0 , listP . Count ) ;
109- Displaying < T > . Display ( listP ) ;
110-
111- for ( int i = listP . Count - 1 ; i >= 0 ; i -- )
93+ for ( int i = listP . Count - 1 ; i >= 0 ; i -- )
11294 {
11395 for ( int j = 0 ; j < listP . Count - 1 ; j ++ )
11496 {
11597 if ( listP [ j ] . CompareTo ( listP [ j + 1 ] ) > 0 )
11698 Swap ( listP , j , j + 1 ) ;
117- count ++ ;
11899 }
119- Displaying < T > . DisplayOne ( listP , i , 0 , i + 1 ) ;
120100 }
121-
122- Displaying < T > . Display ( listP ) ;
123- Console . WriteLine ( "Count = {0}" , count ) ;
124101 }
102+ // Done with bubble algorithm.
103+
104+ // ShellSort Algorithm
125105
126- // ================================================================================
127- //
128- // ShellSort Algorithm
129- //
130- // ================================================================================ public static void ShellSort(List<T> listP)
131106 public static void ShellSort ( List < T > listP )
132107 {
133- int swapNum = 0 ;
134- Console . WriteLine ( ) ;
135- Console . Write ( " " ) ;
136- Displaying < T > . DisplayHeader ( listP , 0 , listP . Count ) ;
137-
138108 int slot ;
139- T tmp ;
140- for ( int gap = listP . Count / 3 + 1 ; gap > 0 ; gap /= 2 ) // determines increment sequence
109+ T current ;
110+ for ( int gap = listP . Count / 3 + 1 ; gap > 0 ; gap /= 2 )
141111 {
142- Console . Write ( "{0} " , gap ) ;
143- Displaying < T > . Display2 ( listP , 0 , listP . Count ) ;
144- for ( int next = gap ; next < listP . Count ; next ++ )
145- { // goes thru array by steps
146- tmp = listP [ next ] ;
147- for ( slot = next ; slot >= gap && tmp . CompareTo ( listP [ slot - gap ] ) < 0 ; slot -= gap ) // slides tmp until in place
148- {
149- ++ swapNum ;
150- listP [ slot ] = listP [ slot - gap ] ;
151-
152- }
153- listP [ slot ] = tmp ;
154- }
155- }
156- Console . WriteLine ( "Count = {0}" , swapNum ) ;
157- }
158112
159- // gaps must be sorted larger to smaller
160- public static void ShellSort ( List < T > listP , int [ ] gaps )
161- {
162- Console . Write ( "---- ShellSort -----" ) ;
163- T tmp ;
164- int slot ;
165- int count = 0 ;
166- Console . WriteLine ( ) ;
167- Console . Write ( " " ) ;
168- Displaying < T > . DisplayHeader ( listP , 0 , listP . Count ) ;
169- Console . Write ( " " ) ;
170- Displaying < T > . Display ( listP ) ;
171- foreach ( int gap in gaps ) // determines increment sequence
172- {
173- Console . Write ( "{0} " , gap ) ;
174113 for ( int next = gap ; next < listP . Count ; next ++ )
175- { // goes thru array by steps
176- tmp = listP [ next ] ;
177- for ( slot = next ; slot >= gap && tmp . CompareTo ( listP [ slot - gap ] ) < 0 ; slot -= gap ) // slides tmp until in place
114+ {
115+ // goes thru array by steps
116+ current = listP [ next ] ;
117+ for ( slot = next ; slot >= gap && current . CompareTo ( listP [ slot - gap ] ) < 0 ; slot -= gap ) // slides current until in place
178118 {
179- ++ count ;
180119 listP [ slot ] = listP [ slot - gap ] ;
181-
182120 }
183- listP [ slot ] = tmp ;
121+ listP [ slot ] = current ;
184122 }
185- Displaying < T > . DisplayEvery ( listP , 0 , listP . Count , gap ) ;
186123 }
187- Console . WriteLine ( "Number of shifts = {0}" , count ) ;
188124 }
189125
190- public static void ShellSort ( List < T > listP , List < int > IncrementList )
191- {
192- T tmp ;
193- int slot ;
194- int count = 0 ;
195-
196- Console . Write ( " " ) ;
197- Displaying < T > . DisplayHeader ( listP , 0 , listP . Count ) ;
198- Console . Write ( " " ) ;
199- Displaying < T > . Display2 ( listP , 0 , listP . Count ) ;
200-
201- for ( int incNumber = IncrementList . Count - 1 ; incNumber >= 0 ; incNumber -- )
202- {
203- int gap = IncrementList [ incNumber ] ;
204-
205- for ( int next = gap ; next < listP . Count ; next ++ )
206- { // goes thru array by steps
207- tmp = listP [ next ] ;
208- for ( slot = next ; slot >= gap && tmp . CompareTo ( listP [ slot - gap ] ) < 0 ; slot -= gap ) // slides tmp until in place
209- {
210- ++ count ;
211- listP [ slot ] = listP [ slot - gap ] ;
212- }
213- listP [ slot ] = tmp ;
214- // Console.WriteLine("Count = {0}", count);
215- }
216- Console . Write ( "{0,3} " , gap ) ;
217- Displaying < T > . Display2 ( listP , 0 , listP . Count ) ;
218- }
219- Console . WriteLine ( "Count = {0}" , count ) ;
220- }
221126
222127
223128
0 commit comments