Skip to content

Commit b9af264

Browse files
committed
update 1.8
implemented quicksort, need to implement sorted elements turning cyan
1 parent 1de70ed commit b9af264

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

index.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ <h1>Sorting Visualizer</h1>
2525
</div>
2626

2727
<div class="slideContainer">
28-
<input type="range" min="5" max="50" value="5" id="myRange" class="slider">
28+
<input type="range" min="5" max="160" value="5" id="myRange" class="slider">
2929
</div>
3030

3131
<p id="boxcount">Items: <span id="value"></span></p>
3232

33-
<button>Go</button>
3433
<button onclick="BubbleSort()">BubbleSort</button>
3534
<button onclick="MergeSort()">MergeSort</button>
36-
<button onclick="QuickSort()">QuickSort</button>
35+
<button onclick="doQuickSort()">QuickSort</button>
3736

3837
<script src="script.js" async defer></script>
3938
</body>

script.js

+45-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var slider = document.getElementById("myRange");
22
var slidevalue = document.getElementById("value");
33
slidevalue.innerHTML = slider.value;
4+
var sortSelection;
45

56

67
for (i = 0; i < 5; i++) {
7-
var w = 810/5;
8+
var w = 200/5;
89
var num2 = addPx(w);
910
var n = Math.floor(Math.random() * 625) + 50;
1011
var num = addPx(n);
@@ -23,13 +24,13 @@ slider.oninput = function() {
2324
slidevalue.innerHTML = this.value;
2425
numBoxes = this.value;
2526
var obj = document.getElementsByClassName("box");
26-
27+
2728
while(obj[0]) {
2829
obj[0].parentNode.removeChild(obj[0]);
2930
}
3031

3132
for (i = 0; i < numBoxes; i++) {
32-
var w = 810/numBoxes;
33+
var w = 200/numBoxes;
3334
var num2 = addPx(w);
3435
var n = Math.floor(Math.random() * 600) + 30;
3536
var num = addPx(n);
@@ -68,7 +69,11 @@ function removePx (str) {
6869

6970

7071
async function Swap(x, y) {
72+
if (sortSelection = 0) {
7173
await sleep((-22)*(myCollection.length) + 1120);
74+
} else if (sortSelection = 1) {
75+
await sleep(25);
76+
}
7277
var temp = myCollection[x].style.height;
7378
myCollection[x].style.height = myCollection[y].style.height;
7479
myCollection[y].style.height = temp;
@@ -85,7 +90,8 @@ function sleep(ms) {
8590
async function BubbleSort() {
8691
var isSorted = 0;
8792
var lastUnsorted = myCollection.length - 1;
88-
93+
sortSelection = 0;
94+
8995
while (isSorted == 0) {
9096
isSorted = 1;
9197
for (i = 0; i < lastUnsorted; i++) {
@@ -116,14 +122,47 @@ async function BubbleSort() {
116122
}
117123
}
118124

125+
// QuickSort
126+
127+
async function Partition(start,end) {
128+
var pivot = removePx(myCollection[end].style.height);
129+
var partitionIndex = start;
130+
131+
for (i = start; i < end; i++) {
132+
if (removePx(myCollection[i].style.height) >= pivot) {
133+
myCollection[i].style.background = "red";
134+
myCollection[i].style.color = "red";
135+
myCollection[partitionIndex].style.background = "red";
136+
myCollection[partitionIndex].style.color = "red";
137+
await Swap(i, partitionIndex);
138+
myCollection[i].style.background = "bisque";
139+
myCollection[i].style.color = "bisque";
140+
myCollection[partitionIndex].style.background = "bisque";
141+
myCollection[partitionIndex].style.color = "bisque";
142+
partitionIndex++;
143+
}
144+
}
145+
await Swap(partitionIndex, end);
146+
return partitionIndex;
147+
}
119148

149+
async function QuickSort(start,end) {
150+
if (start < end) {
151+
var partitionIndex = await Partition(start, end);
152+
await QuickSort(start, partitionIndex - 1);
153+
await QuickSort(partitionIndex + 1, end);
154+
}
155+
}
120156

157+
function doQuickSort() {
158+
sortSelection = 1;
159+
QuickSort(0, myCollection.length - 1);
160+
}
121161

122-
// MergeSort
162+
// MergeSort
123163

124164

125165

126-
//QuickSort
127166

128167

129168

style.css

+22-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ h1 {
1919
}
2020

2121
.slider::after {
22-
content: '50';
22+
content: '160';
2323
color: black;
2424
font-size: large;
2525
position: absolute;
@@ -39,7 +39,8 @@ p {
3939
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
4040
}
4141

42-
#boxcount {
42+
#boxcount {
43+
position: relative;
4344
text-align: center;
4445
padding-top: 1%;
4546
}
@@ -53,8 +54,8 @@ p {
5354
}
5455

5556
.box {
56-
border: 3px solid black;
57-
padding: 5px;
57+
border: 1px solid black;
58+
padding: 2px;
5859
margin: 2px;
5960
display: inline-block;
6061
background: bisque;
@@ -68,9 +69,25 @@ p {
6869
width: 1840px;
6970
border: 5px solid darkgrey;
7071
position: relative;
71-
top: 40px;
72+
top: 50px;
7273
left: 30px;
7374
padding-bottom: 2%;
7475
transform: rotate(180deg);
7576
}
7677

78+
button {
79+
background-color: white; /* Green */
80+
border: 1px solid black;
81+
color: black;
82+
padding: 15px 32px;
83+
text-align: center;
84+
text-decoration: none;
85+
display: inline-block;
86+
font-size: 16px;
87+
padding: 15px 32px;
88+
position: relative;
89+
left: 5%;
90+
bottom: 20px;
91+
margin-left: 2%;
92+
}
93+

0 commit comments

Comments
 (0)