-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsertionSort.java
49 lines (42 loc) · 1.33 KB
/
InsertionSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.ArrayList;
// Implementation for Sorting Results using Insertion Sort algorithm
public class InsertionSort extends Sort {
// Constructor
// _result : result list to be sorted
public InsertionSort( ArrayList<Data[]> result, DataStore dataStore ) {
super( result, dataStore );
}
// Method to sort an result list
// keys : columns to sort
public ArrayList<Data[]> Sort( String[] keys )
throws IllegalArgumentException
{
dataStore.checkKeyArgs( keys );
// Sort for each key
for ( String key : keys ) {
Data[] temp; // holds the interchanged element row
// Use first row (header) in result to get order of keys in result
int ncol = -1;
for ( int i = 0; i < result.get( 0 ).length; i++ ) {
if ( ((String)result.get( 0 )[ i ].Get()).compareTo( key ) == 0 ) {
ncol = i;
break;
}
}
// Check if key is in the result
if ( -1 == ncol )
throw new IllegalArgumentException( "Sort key not in result: " + key );
int length = result.size();
for (int i = 2; i < length; i++) { // start at row 2 (1 is the heading)
for(int j = i ; j > 1 ; j--){
if ( result.get( j )[ ncol ].LT( result.get( j - 1 )[ ncol ] ) ) {
temp = result.get( j );
result.set( j, result.get( j - 1 ) );
result.set( j - 1, temp );
}
}
}
}
return result;
}
}