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 pathShellSort.java
More file actions
92 lines (86 loc) · 1.68 KB
/
ShellSort.java
File metadata and controls
92 lines (86 loc) · 1.68 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
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.ArrayList;
public class ShellSort implements Sort
{
//private data members
private sortParams sParams;
//constructors
public ShellSort()
{
sParams = null;
}
//always initialize the memory usage to 0
public void initialize(sortParams s)
{
sParams = s;
sParams.setMemUsage(0);
}
//getters
public sortParams getSortParams()
{
return sParams;
}
void TestPeakUsage(int memUsage)
{
if(memUsage > sParams.getMemUsage())
{
sParams.setMemUsage(memUsage);
}
}
/*
* Shell Short Methods
*
*/
void ShSort(int[] arr, int start, int end, ArrayList<Integer> gapSeq, Direction dir)
{
int memUsage = 0;
memUsage += 32;
TestPeakUsage(memUsage);
for(int k=0; k<gapSeq.size(); ++k)
{
memUsage += gapSeq.size()*32;
TestPeakUsage(memUsage);
int gap = gapSeq.get(k);
memUsage += 32;
TestPeakUsage(memUsage);
for(int i=gap; i<end; ++i)
{
memUsage += 32;
TestPeakUsage(memUsage);
int val = arr[i];
memUsage += 32;
TestPeakUsage(memUsage);
int j = i - gap;
if(dir == Direction.ASCENDING)
{
while(j>=start && arr[j]>val)
{
arr[j+gap] = arr[j];
j -= gap;
}
}
else
{
while(j>=start && arr[j]<val)
{
arr[j+gap] = arr[j];
j -= gap;
}
}
arr[j+gap] = val;
memUsage -= 64;
}
memUsage -= 64;
}
memUsage -= 32;
}
/*
* Sort Method Implementation
* sorts in order the input has using the private ShSort
* methods above
*/
public void sort(sortParams sParams)
{
sParams.setMemUsage(0);
ShSort(sParams.getArray(), sParams.getStartIndex(), sParams.getEndIndex(), sParams.getGapSeq(), sParams.getDirection());
}
}