Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions Sorts/CycleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
* Wikipedia: https://en.wikipedia.org/wiki/Cycle_sort
*/

/**
* cycleSort takes an input array of numbers and returns the array sorted in increasing order.
*
* @param {number[]} list An array of numbers to be sorted.
* @return {number[]} An array of numbers sorted in increasing order.
*/
function cycleSort (list) {
let writes = 0
for (let cycleStart = 0; cycleStart < list.length; cycleStart++) {
let value = list[cycleStart]
let position = cycleStart
Expand All @@ -20,19 +25,17 @@ function cycleSort (list) {
position++
}
}
// if its the same continue
// if it is the same, continue
if (position === cycleStart) {
continue
}

while (value === list[position]) {
position++
}

const oldValue = list[position]
list[position] = value
value = oldValue
writes++

// rotate the rest
while (position !== cycleStart) {
Expand All @@ -48,20 +51,9 @@ function cycleSort (list) {
const oldValueCycle = list[position]
list[position] = value
value = oldValueCycle
writes++
}
}
return writes
return list
}

/**
* Implementation of Cycle Sort
*/
const array = [5, 6, 7, 8, 1, 2, 12, 14]
// Before Sort
console.log('\n- Before Sort | Implementation of Cycle Sort -')
console.log(array)
// After Sort
console.log('- After Sort | Implementation of Cycle Sort -')
console.log(cycleSort(array))
console.log('\n')
export { cycleSort }
69 changes: 69 additions & 0 deletions Sorts/test/CycleSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { cycleSort } from '../CycleSort'

describe('cycleSort function', () => {
it('should correctly sort an input list that is sorted backwards', () => {
const array = [5, 4, 3, 2, 1]
expect(cycleSort(array)).toEqual([1, 2, 3, 4, 5])
})

it('should correctly sort an input list that is unsorted', () => {
const array = [15, 24, 3, 2224, 1]
expect(cycleSort(array)).toEqual([1, 3, 15, 24, 2224])
})

describe('Variations of input array lengths', () => {
it('should return an empty list with the input list is an empty list', () => {
expect(cycleSort([])).toEqual([])
})

it('should correctly sort an input list of length 1', () => {
expect(cycleSort([100])).toEqual([100])
})

it('should correctly sort an input list of an odd length', () => {
expect(cycleSort([101, -10, 321])).toEqual([-10, 101, 321])
})

it('should correctly sort an input list of an even length', () => {
expect(cycleSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56])
})
})

describe('Variations of input array elements', () => {
it('should correctly sort an input list that contains only positive numbers', () => {
expect(cycleSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50])
})

it('should correctly sort an input list that contains only negative numbers', () => {
expect(cycleSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1])
})

it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => {
expect(cycleSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56])
})

it('should correctly sort an input list that contains only whole numbers', () => {
expect(cycleSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12])
})

it('should correctly sort an input list that contains only decimal numbers', () => {
expect(cycleSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45])
})

it('should correctly sort an input list that contains only a mix of whole and decimal', () => {
expect(cycleSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56])
})

it('should correctly sort an input list that contains only fractional numbers', () => {
expect(cycleSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98])
})

it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => {
expect(cycleSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12])
})

it('should correctly sort an input list that contains duplicates', () => {
expect(cycleSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4])
})
})
})