Skip to content

Commit 6bc05ff

Browse files
committed
(hybrid) zipsort use single template function
1 parent 78a7d5b commit 6bc05ff

3 files changed

Lines changed: 19 additions & 38 deletions

File tree

src/sorts/HybridZipSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public HybridZipSort(Delays delayOps, Highlights markOps, Reads readOps, Writes
2727

2828
@Override
2929
public void runSort(int[] array, int length, int bucketCount) {
30-
this.hybridZipSort(array, 0, length, 1, true, false);
30+
this.zipSort(array, 0, length, true, 1, true, false);
3131
}
3232
}

src/sorts/ZipSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public ZipSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeO
2727

2828
@Override
2929
public void runSort(int[] array, int length, int bucketCount) {
30-
this.zipSort(array, 0, length, 1, true, false);
30+
this.zipSort(array, 0, length, false, 1, true, false);
3131
}
3232
}

src/templates/ZipSorting.java

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -116,60 +116,41 @@ protected void zipMerge(int[] arr, int left, int right, int end, double sleep, b
116116
}
117117
}
118118

119-
protected void zipSort(int[] arr, int beg, int end, double sleep, boolean mark, boolean temp) {
119+
protected void zipSort(int[] arr, int beg, int end, boolean hybrid, double sleep, boolean mark, boolean temp) {
120+
insertSorter = new InsertionSort(this.Delays, this.Highlights, this.Reads, this.Writes);
120121
int sze = end - beg;
121122
if(sze <= 1)
122123
return;
123124

124-
//go through all of the lengths starting at 2 doubling
125125
int len = 1;
126-
while(len < sze) {
127-
int pos = 0;
128-
//go through all of the sorted sublists, zip them together
129-
while(pos + len < sze) {
130-
//make the two halves
131-
int cleft = beg + pos;
132-
int cright = cleft + len;
133-
int cend = cleft + (len * 2);
134-
if(pos + (len * 2) > sze) cend = end;
135126

136-
//do zip merge
137-
zipMerge(arr, cleft, cright, cend, sleep, mark, temp);
138-
pos += (len * 2);
127+
if(hybrid){
128+
//sort small runs with insertion sort before doing merge
129+
int insert_count = 16;
130+
{
131+
int len = insert_count;
132+
int count = 0;
133+
for(int bg = beg; bg != end; count+=len) {
134+
int ed = (count + len > sze ? end : bg + len);
135+
insertSorter.customInsertSort(arr, bg, ed, sleep, temp);
136+
bg = ed;
137+
}
139138
}
140-
len *= 2;
141-
}
142-
}
139+
if(sze <= insert_count)
140+
return;
143141

144-
protected void hybridZipSort(int[] arr, int beg, int end, double sleep, boolean mark, boolean temp) {
145-
insertSorter = new InsertionSort(this.Delays, this.Highlights, this.Reads, this.Writes);
146-
int sze = end - beg;
147-
if(sze <= 1)
148-
return;
149-
//sort small runs with insertion sort before doing merge
150-
int insert_count = 16;
151-
{
152-
int len = insert_count;
153-
int count = 0;
154-
for(int bg = beg; bg != end; count+=len) {
155-
int ed = (count + len > sze ? end : bg + len);
156-
insertSorter.customInsertSort(arr, bg, ed, sleep, temp);
157-
bg = ed;
158-
}
142+
len = insert_count;
159143
}
160-
if(sze <= insert_count)
161-
return;
162144

163145
//go through all of the lengths starting at 1 doubling
164-
int len = insert_count;
165146
while(len < sze) {
166147
int pos = 0;
167148
//go through all of the sorted sublists, zip them together
168149
while(pos + len < sze) {
169150
//make the two halves
170151
int cleft = beg + pos;
171152
int cright = cleft + len;
172-
int cend = (pos + (len * 2) > sze ? end : cleft + (len * 2));
153+
int cend = (pos + (len * 2) > sze ? end : cright + len);
173154

174155
//do zip merge
175156
zipMerge(arr, cleft, cright, cend, sleep, mark, temp);

0 commit comments

Comments
 (0)