File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change 1- // сортировка вставками
1+
22public class InsertionSort {
3- public static insertionSort (int [] arr ) {
3+ /**
4+ * сортировка вставками по убыванию
5+ */
6+ public static void insertionSort (int [] arr ) {
47 for (int i = 1 ; i < arr .length ; i ++) {
58 int x = arr [i ];
69 int j = i - 1 ;
7- while (j > 0 && arr [j ] < x ) {
10+ while (j >= 0 && arr [j ] < x ) {
811 arr [j + 1 ] = arr [j ];
912 j --;
1013 }
1114 arr [j + 1 ] = x ;
1215 }
1316 }
17+
18+ /**
19+ * Сортировка вставками по возрастанию
20+ * @param list принимает список, элементы которого реализуют интерфейс {@code Comparable}
21+ * @throws UnsupportedOperationException если список неизменяемый (unmodifiable)
22+ */
23+ public static <T extends Comparable <? super T >> void insertionSort (List <T > list ) {
24+ for (int i = 1 ; i < list .size (); i ++) {
25+ T value = list .get (i );
26+ int j = i - 1 ;
27+ while (j >= 0 && list .get (j ).compareTo (value ) > 0 ) {
28+ list .set (j + 1 , list .get (j ));
29+ j --;
30+ }
31+ list .set (j + 1 , value );
32+ }
33+ }
1434}
You can’t perform that action at this time.
0 commit comments