Skip to content

Commit dde1e27

Browse files
DANIEL DRORIDANIEL DRORI
authored andcommitted
Add Tournament Sort implementation
1 parent 5c39e87 commit dde1e27

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Sorts/TournamentSort.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Tournament Sort
3+
*
4+
* Tournament Sort improves upon naive Selection Sort by using a min-heap
5+
* (priority queue) to find the minimum element in O(log n) instead of O(n),
6+
* giving an overall time complexity of O(n log n).
7+
*
8+
* The name comes from its resemblance to a single-elimination sports tournament
9+
* where the winner (minimum element) is found each round.
10+
*
11+
* Reference: https://en.wikipedia.org/wiki/Tournament_sort
12+
*/
13+
14+
/**
15+
* Restores the min-heap property by sifting down from index i.
16+
* @param {number[]} heap - The heap array
17+
* @param {number} n - Size of the heap
18+
* @param {number} i - Index to sift down from
19+
*/
20+
const heapify = (heap, n, i) => {
21+
let smallest = i
22+
const left = 2 * i + 1
23+
const right = 2 * i + 2
24+
25+
if (left < n && heap[left] < heap[smallest]) smallest = left
26+
if (right < n && heap[right] < heap[smallest]) smallest = right
27+
28+
if (smallest !== i) {
29+
;[heap[i], heap[smallest]] = [heap[smallest], heap[i]]
30+
heapify(heap, n, smallest)
31+
}
32+
}
33+
34+
/**
35+
* Sorts an array using Tournament Sort.
36+
* @param {number[]} arr - The array to sort
37+
* @returns {number[]} The sorted array
38+
*/
39+
const tournamentSort = (arr) => {
40+
if (arr.length <= 1) return arr
41+
42+
const heap = [...arr]
43+
const result = []
44+
45+
// Build min-heap
46+
for (let i = Math.floor(heap.length / 2) - 1; i >= 0; i--) {
47+
heapify(heap, heap.length, i)
48+
}
49+
50+
// Extract minimum elements one by one
51+
let size = heap.length
52+
while (size > 0) {
53+
result.push(heap[0])
54+
heap[0] = heap[size - 1]
55+
size--
56+
heapify(heap, size, 0)
57+
}
58+
59+
return result
60+
}
61+
62+
export { tournamentSort }

Sorts/TournamentSort.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { tournamentSort } from './TournamentSort.js'
2+
3+
describe('Tournament Sort', () => {
4+
it('should sort an unsorted array', () => {
5+
expect(tournamentSort([5, 3, 1, 4, 2])).toEqual([1, 2, 3, 4, 5])
6+
})
7+
8+
it('should handle an already sorted array', () => {
9+
expect(tournamentSort([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5])
10+
})
11+
12+
it('should handle a reverse sorted array', () => {
13+
expect(tournamentSort([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5])
14+
})
15+
16+
it('should handle a single element array', () => {
17+
expect(tournamentSort([42])).toEqual([42])
18+
})
19+
20+
it('should handle an empty array', () => {
21+
expect(tournamentSort([])).toEqual([])
22+
})
23+
24+
it('should handle duplicates', () => {
25+
expect(tournamentSort([3, 1, 2, 1, 3])).toEqual([1, 1, 2, 3, 3])
26+
})
27+
28+
it('should handle negative numbers', () => {
29+
expect(tournamentSort([-3, 0, -1, 5, 2])).toEqual([-3, -1, 0, 2, 5])
30+
})
31+
})

0 commit comments

Comments
 (0)