-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
633ea07
commit 0f89a7f
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Given a list whose elements are 2-tuples that represent inclusive intervals, | ||
return the intersection of a pivot interval and the 2-tuples if one exists. | ||
*/ | ||
|
||
export const findIntersection = (intervals, l, r) => { | ||
const pivot = intervals[r]; | ||
let pivotL = pivot[0]; | ||
let pivotR = pivot[1]; | ||
for(let i = 0; i < r; i++){ | ||
if(intervals[i][0] <= pivotR && intervals[i][1] >= pivotL) { | ||
if(intervals[i][0] > pivotL) | ||
pivotL = intervals[i][0]; | ||
if(intervals[i][1] < pivotR) | ||
pivotR = intervals[i][1]; | ||
} | ||
} | ||
return [pivotL, pivotR]; | ||
|
||
} | ||
|
||
export const partitionRight = (intervals, intersectionIntervalLeft, l, r) => { | ||
let i = l - 1; | ||
for(let j = l; j < r; j++) { | ||
if(intervals[j][0] <= intersectionIntervalLeft) { | ||
i++; | ||
[intervals[j], intervals[i]] = [intervals[i], intervals[j]]; | ||
} | ||
} | ||
[intervals[i+1], intervals[r]] = [intervals[r], intervals[i+1]]; | ||
|
||
return i + 1; | ||
} | ||
|
||
|
||
const partitionLeft = (intervals, intersectionIntervalRight, l, r) => { | ||
let i = l - 1; | ||
for(let j = l; j < r; j++) { | ||
if(intervals[j][1] < intersectionIntervalRight) { | ||
i++; | ||
[intervals[j], intervals[i]] = [intervals[i], intervals[j]]; | ||
} | ||
} | ||
[intervals[i+1], intervals[r]] = [intervals[r], intervals[i+1]]; | ||
|
||
return i + 1; | ||
} | ||
|
||
export const fuzzyQuickSort = (intervals, l, r) => { | ||
if (l < r) { | ||
const [intersectionIntervalLeft, intersectionIntervalRight] = findIntersection(intervals, l, r); | ||
const t = partitionRight(intervals, intersectionIntervalLeft, l, r); | ||
const q = partitionLeft(intervals, intersectionIntervalRight, l, t); | ||
fuzzyQuickSort(intervals, l, q - 1); | ||
fuzzyQuickSort(intervals, t + 1, r); | ||
return intervals; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {findIntersection, fuzzyQuickSort} from "../index"; | ||
|
||
describe('Fuzzy sort intervals', () => { | ||
|
||
describe('Find Intersection', () => { | ||
|
||
it('should return indices correctly - 1', () => { | ||
expect(findIntersection([[12, 14], [6, 8], [7, 10], [1, 4]], 0, 3)).toEqual([1, 4]); | ||
}); | ||
|
||
it('should return indices correctly - 2', () => { | ||
expect(findIntersection([[8, 14], [1, 5], [4, 9], [8, 12]], 0, 3)).toEqual([8, 9]); | ||
}); | ||
|
||
it('should return indices correctly - 3', () => { | ||
const input = [[13, 20], [19, 21], [9, 11], [5, 7], [12, 16], [8, 10], [7, 9], [4, 6], [20, 24], [2, 2], [6, 8], [11, 15]]; | ||
|
||
expect(findIntersection(input, 0, 11)).toEqual([13, 15]); | ||
}); | ||
|
||
}); | ||
|
||
describe('Quick Sort Intervals', () => { | ||
|
||
it('should fuzzy sort intervals - 1', () => { | ||
const input = [[12, 14], [6, 8], [7, 10], [1, 4]]; | ||
const expectedOutput = [[1, 4], [7, 10], [6, 8], [12, 14]]; | ||
|
||
expect(fuzzyQuickSort(input, 0, input.length - 1)).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should fuzzy sort intervals - 2', () => { | ||
const input = [[8, 14], [1, 5], [4, 9], [8, 12]]; | ||
const expectedOutput = [[1,5], [8, 12], [4, 9], [8, 14]]; | ||
|
||
expect(fuzzyQuickSort(input, 0, input.length - 1)).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should fuzzy sort intervals - 3', () => { | ||
const input = [[13, 20], [19, 21], [9, 11], [5, 7], [12, 16], [8, 10], [7, 9], [4, 6], [20, 24], [2, 2], [6, 8], [11, 15]]; | ||
const expectedOutput = [[2, 2], [4, 6], [6, 8], [7, 9], [5, 7], [8, 10], [9, 11], [11, 15], [13, 20], [12, 16], [19, 21], [20, 24]] | ||
|
||
expect(fuzzyQuickSort(input, 0, input.length - 1)).toEqual(expectedOutput); | ||
}); | ||
}); | ||
|
||
}); |