File tree Expand file tree Collapse file tree
src/algorithms/sorting/comb-sort Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import Sort from '../Sort' ;
2+
3+ export default class CombSort extends Sort {
4+ sort ( originalArray ) {
5+ // Clone original array to prevent its modification.
6+ const array = [ ...originalArray ] ;
7+
8+ // Gap sequence shrink factor (empirically found to be ~1.3)
9+ const shrinkFactor = 1.3 ;
10+
11+ // Initialize gap as array length
12+ let gap = array . length ;
13+
14+ // Initialize swapped as true to enter the loop
15+ let swapped = true ;
16+
17+ // Keep iterating while gap is greater than 1 or there was a swap
18+ while ( gap > 1 || swapped ) {
19+ // Update gap using shrink factor
20+ gap = Math . floor ( gap / shrinkFactor ) ;
21+
22+ // Ensure gap is at least 1
23+ if ( gap < 1 ) {
24+ gap = 1 ;
25+ }
26+
27+ // Reset swapped flag
28+ swapped = false ;
29+
30+ // Compare elements with current gap
31+ for ( let i = 0 ; i + gap < array . length ; i += 1 ) {
32+ // Call visiting callback.
33+ this . callbacks . visitingCallback ( array [ i ] ) ;
34+
35+ // Compare elements at distance of gap
36+ if ( this . comparator . lessThan ( array [ i + gap ] , array [ i ] ) ) {
37+ // Swap elements
38+ [ array [ i ] , array [ i + gap ] ] = [ array [ i + gap ] , array [ i ] ] ;
39+
40+ // Mark that a swap occurred
41+ swapped = true ;
42+ }
43+ }
44+ }
45+
46+ return array ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ # Comb Sort
2+
3+ _ Read this in other languages:_
4+ [ _ português_ ] ( README.pt-BR.md )
5+
6+ ## Description
7+
8+ Comb Sort improves on Bubble Sort by using a gap larger than 1.
9+ The gap starts at the length of the list and shrinks by a factor
10+ of 1.3 each iteration until it reaches 1. At that point, Comb Sort
11+ becomes equivalent to Bubble Sort, but by that time most of the
12+ work has been done and the list is nearly sorted.
13+
14+ The shrink factor of 1.3 has been empirically shown to be the best
15+ value for the shrink factor for Comb Sort.
16+
17+ ## Complexity
18+
19+ | Name | Best | Average | Worst | Memory | Stable |
20+ | ----------------- | --------- | -------------- | --------------- | ------- | ------ |
21+ | Comb sort | n log(n) | n²/2ᵈ (n log²n) | n² | 1 | No |
22+
23+ ## References
24+
25+ - [ Wikipedia] ( https://en.wikipedia.org/wiki/Comb_sort )
26+ - [ GeeksForGeeks] ( https://www.geeksforgeeks.org/comb-sort/ )
27+ - [ YouTube] ( https://www.youtube.com/watch?v=J4_1A6lxfFQ )
Original file line number Diff line number Diff line change 1+ import CombSort from '../CombSort' ;
2+ import {
3+ equalArr ,
4+ notSortedArr ,
5+ reverseArr ,
6+ sortedArr ,
7+ SortTester ,
8+ } from '../../SortTester' ;
9+
10+ describe ( 'CombSort' , ( ) => {
11+ it ( 'should sort array' , ( ) => {
12+ SortTester . testSort ( CombSort ) ;
13+ } ) ;
14+
15+ it ( 'should sort array with custom comparator' , ( ) => {
16+ SortTester . testSortWithCustomComparator ( CombSort ) ;
17+ } ) ;
18+
19+ it ( 'should sort negative numbers' , ( ) => {
20+ SortTester . testNegativeNumbersSort ( CombSort ) ;
21+ } ) ;
22+
23+ it ( 'should visit SORTED array element specified number of times' , ( ) => {
24+ SortTester . testAlgorithmTimeComplexity (
25+ CombSort ,
26+ sortedArr ,
27+ 10 ,
28+ ) ;
29+ } ) ;
30+
31+ it ( 'should visit NOT SORTED array element specified number of times' , ( ) => {
32+ SortTester . testAlgorithmTimeComplexity (
33+ CombSort ,
34+ notSortedArr ,
35+ 30 ,
36+ ) ;
37+ } ) ;
38+ } ) ;
You can’t perform that action at this time.
0 commit comments