src/components/algorithms.js   A
last analyzed

Complexity

Total Complexity 25
Complexity/F 3.57

Size

Lines of Code 167
Function Count 7

Duplication

Duplicated Lines 76
Ratio 45.51 %

Importance

Changes 0
Metric Value
wmc 25
eloc 97
mnd 18
bc 18
fnc 7
dl 76
loc 167
rs 10
bpm 2.5714
cpm 3.5714
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
B algorithms.js ➔ shellSort 0 40 6
A algorithms.js ➔ insertionSort 40 38 4
C algorithms.js ➔ quickSort 1 53 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
export function bubbleSort (arr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
  let state = []
3
4
  state.push({
5
    numbers: arr.slice(),
6
    active: [0, 0]
7
  })
8
9
  for (let i = 0; i < arr.length; i++) {
10
    for (let j = i + 1; j < arr.length; j++) {
11
      state.push({
12
        numbers: arr.slice(),
13
        active: [i, j]
14
      })
15
16
      if (arr[i] > arr[j]) {
17
        let temp = arr[i]
18
19
        arr[i] = arr[j]
20
        arr[j] = temp
21
      }
22
    }
23
  }
24
25
  for (let i = 0; i < arr.length; i++) {
26
    state.push({
27
      numbers: arr.slice(),
28
      active: [...Array(i).keys()]
29
    })
30
  }
31
32
  return state
33
}
34
35 View Code Duplication
export function insertionSort (arr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
36
  let state = []
37
38
  state.push({
39
    numbers: arr.slice(),
40
    active: [0, 0]
41
  })
42
43
  for (let i = 1; i < arr.length; i++) {
44
    let k = arr[i]
45
46
    let j = i - 1
47
48
    while (j >= 0 && arr[j] > k) {
49
      state.push({
50
        numbers: arr.slice(),
51
        active: [j, k]
52
      })
53
54
      arr[j + 1] = arr[j]
55
      j = j - 1
56
    }
57
    arr[j + 1] = k
58
  }
59
60
  for (let i = 0; i < arr.length; i++) {
61
    state.push({
62
      numbers: arr.slice(),
63
      active: [...Array(i).keys(), i]
64
    })
65
  }
66
  state.push({
67
    numbers: arr.slice(),
68
    active: [...Array(arr.length).keys()]
69
  })
70
71
  return state
72
}
73
74
export function quickSort (arr, less = (a, b) => a < b) {
75
  let state = []
76
77
  function swap (i, j) {
78
    var t = arr[i]
79
80
    arr[i] = arr[j]
81
    arr[j] = t
82
83
    state.push({
84
      numbers: arr.slice(),
85
      active: [i, j]
86
    })
87
  }
88
89
  function quicksort (left, right) {
90
    if (left < right) {
91
      var pivot = arr[left + Math.floor((right - left) / 2)]
92
93
      var leftNew = left
94
95
      var rightNew = right
96
97
      do {
98
        while (less(arr[leftNew], pivot)) {
99
          leftNew += 1
100
        }
101
        while (less(pivot, arr[rightNew])) {
102
          rightNew -= 1
103
        }
104
        if (leftNew <= rightNew) {
105
          swap(leftNew, rightNew)
106
          leftNew += 1
107
          rightNew -= 1
108
        }
109
      } while (leftNew <= rightNew)
110
111
      quicksort(left, rightNew)
112
      quicksort(leftNew, right)
113
    }
114
  }
115
116
  quicksort(0, arr.length - 1)
117
118
  for (let i = 0; i < arr.length; i++) {
119
    state.push({
120
      numbers: arr.slice(),
121
      active: [...Array(i).keys(), i]
122
    })
123
  }
124
125
  return state
126
}
127
128
export function shellSort (arr) {
129
  let state = []
130
131
  let increment = arr.length / 2
132
133
  while (increment > 0) {
134
    for (let i = increment; i < arr.length; i++) {
135
      let j = i
136
137
      let temp = arr[i]
138
139
      while (j >= increment && arr[j - increment] > temp) {
140
        state.push({
141
          numbers: arr.slice(),
142
          active: [j, j - increment]
143
        })
144
145
        arr[j] = arr[j - increment]
146
        j = j - increment
147
      }
148
149
      arr[j] = temp
150
    }
151
152
    if (increment === 2) {
153
      increment = 1
154
    } else {
155
      increment = parseInt((increment * 5) / 11)
156
    }
157
  }
158
159
  for (let i = 0; i < arr.length; i++) {
160
    state.push({
161
      numbers: arr.slice(),
162
      active: [...Array(i).keys(), i]
163
    })
164
  }
165
166
  return state
167
}
168