Skip to content

Commit 1af0c0b

Browse files
authored
Merge pull request #86 from jantabacki/master
2 parents 534b054 + 0534c14 commit 1af0c0b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

sorting/InsertionSort.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Generic Insertion Sort
3+
* @author Jan Tabacki
4+
*/
5+
6+
public class InsertionSort<T extends Comparable<? super T>> {
7+
8+
/**
9+
* Sort
10+
* @param T[] array of not sorted elements
11+
* @return T[] array of sorted element
12+
*/
13+
public T[] Sort(T[] values) {
14+
if (values.length > 0) {
15+
T value = values[0];
16+
for (int i = 1; i < values.length; ++i) {
17+
value = values[i];
18+
int j;
19+
for (j = i - 1; j >= 0 && values[j].compareTo(value) > 0; --j) {
20+
values[j + 1] = values[j];
21+
}
22+
values[j + 1] = value;
23+
}
24+
}
25+
return values;
26+
}
27+
}

0 commit comments

Comments
 (0)