Skip to content

Commit f8545ef

Browse files
committed
quick sort
1 parent 6fc7624 commit f8545ef

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

BigO-algoritmos/quick-sort.html

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE html>
2+
<html lang="pt-br">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Quick Sort</title>
8+
</head>
9+
10+
<body>
11+
12+
<h1>Quick Sort</h1>
13+
14+
<script>
15+
16+
class QuickSort {
17+
constructor(array) {
18+
this.array = array
19+
}
20+
21+
sort() {
22+
this.quickSort(0, this.array.length - 1)
23+
return this.array
24+
}
25+
26+
quickSort(start, end) {
27+
if (start < end) {
28+
let pivotIndex = this.partition(start, end)
29+
this.quickSort(start, pivotIndex - 1)
30+
this.quickSort(pivotIndex + 1, end)
31+
}
32+
}
33+
34+
partition(start, end) {
35+
const midIndex = parseInt((start + end) / 2)
36+
const pivotValue = this.array[midIndex]
37+
38+
let i = start
39+
let j = end
40+
41+
while (i <= j) {
42+
while (this.array[i] < pivotValue) {
43+
i++
44+
}
45+
46+
while (this.array[j] > pivotValue) {
47+
j--
48+
}
49+
50+
if (i <= j) {
51+
this.swap(i, j)
52+
i++
53+
j--
54+
}
55+
}
56+
console.log("i: ", i, "j: ", j)
57+
return i - 1
58+
}
59+
60+
61+
swap(i, j) {
62+
const temp = this.array[i]
63+
this.array[i] = this.array[j]
64+
this.array[j] = temp
65+
}
66+
67+
68+
}
69+
70+
const numeros = [34, 7, 23, 32, 5, 62, 78, 43]
71+
const sorter = new QuickSort(numeros)
72+
console.log("Array Ordenado:", sorter.sort())
73+
74+
75+
</script>
76+
77+
</body>
78+
79+
</html>

0 commit comments

Comments
 (0)