This repository was archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
80 lines (74 loc) · 1.36 KB
/
InsertionSort.java
File metadata and controls
80 lines (74 loc) · 1.36 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class InsertionSort implements Sort
{
//private data members
private sortParams sParams;
//constructors
public InsertionSort()
{
sParams = null;
}
public void initialize(sortParams s)
{
sParams = s;
}
//getters
public sortParams getSortParams()
{
return sParams;
}
void TestPeakUsage(int memUsage)
{
if(memUsage > sParams.getMemUsage())
{
sParams.setMemUsage(memUsage);
}
}
/*
* Insertion Sort Methods for Ascending/Descending
* (Logic for the sorting)
*/
void InSort(int[] arr, int start, int end, Direction dir)
{
int memUsage = 0;
memUsage += 32;
TestPeakUsage(memUsage);
for(int i=start+1; i<end; ++i)
{
memUsage += 32;
TestPeakUsage(memUsage);
int val = arr[i];
memUsage += 32;
TestPeakUsage(memUsage);
int j = i - 1;
if(dir == Direction.ASCENDING)
{
while(j>=start&&arr[j]>val)
{
arr[j+1] = arr[j];
--j;
}
}
else
{
while(j>=start&&arr[j]<val)
{
arr[j+1] = arr[j];
--j;
}
}
arr[j+1] = val;
memUsage -= 64;
}
memUsage -= 32;
}
/*
* Sort Method Implementation
* sorts in order the input has using the private InSort
* methods above
*/
public void sort(sortParams sParams)
{
sParams.setMemUsage(0);
InSort(sParams.getArray(), sParams.getStartIndex(), sParams.getEndIndex(), sParams.getDirection());
}
}