-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
186 lines (172 loc) · 5.6 KB
/
Copy pathscript.js
File metadata and controls
186 lines (172 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Declare constants to avoid use of "magic numbers"
const MIN = 1;
const MAX = 100;
const ARRAY_SIZE = 7;
const DELAY = 1000;
const CHECK = 1;
const SWAP = 2;
const SWAPPED = 3;
const SORTED_TO = 4;
const COMPLETE = 5;
const NEW_PASS = 6;
let unsortedArray = [];
// Get main element from DOM
const main = document.getElementsByTagName("main")[0];
randomizeArray(unsortedArray);
// display the unsorted array and the bubble sort pseudocode
// display the array in the DOM
showArray(unsortedArray);
function startSort() {
document.getElementById("start-button").remove();
const {sortedArray, sortingSteps} = bubbleSort(unsortedArray);
stepThroughSort(unsortedArray, sortingSteps);
}
// Functions:
function repeat() {
unsortedArray = [];
randomizeArray(unsortedArray);
startSort();
}
// populate array with random numbers
function randomizeArray(array) {
for(i = 0; i < ARRAY_SIZE; i++) {
array.push(randomInt(MIN, MAX));
}
}
// step through sort
function stepThroughSort(unsortedArray, steps) {
const localArray = [...unsortedArray];
let stepCounter = 0;
const interval = setInterval(showStep, DELAY);
function showStep() {
const currentStep = steps[stepCounter];
const {position, operation} = currentStep;
let i;
showArray(localArray);
switch(operation) {
case NEW_PASS:
showMessage(`Starting pass #${position + 1}.`);
break;
case CHECK:
document.getElementById(`array-element-${position}`).classList.add("checking");
document.getElementById(`array-element-${position + 1}`).classList.add("checking");
showMessage(`Checking position ${position} and position ${position + 1}.`);
break;
case SWAP:
document.getElementById(`array-element-${position}`).classList.add("swapping");
document.getElementById(`array-element-${position + 1}`).classList.add("swapping");
showMessage(`${localArray[position]} is greater than ${localArray[position + 1]}. Swapping positions ${position} and ${position + 1}.`);
// perform swap on local array
const temp = localArray[position];
localArray[position] = localArray[position + 1];
localArray[position + 1] = temp;
break;
case SWAPPED:
document.getElementById(`array-element-${position}`).classList.add("swapped");
document.getElementById(`array-element-${position + 1}`).classList.add("swapped");
showMessage(`${localArray[position + 1]} is greater than ${localArray[position]}. Swapping positions ${position} and ${position + 1}.`);
break;
case SORTED_TO:
for(i = position; i < localArray.length; i++) {
document.getElementById(`array-element-${i}`).classList.add("sorted");
document.getElementById(`array-element-${i}`).classList.add("sorted");
}
if(position == localArray.length - 1) {
showMessage(`Array is sorted for element ${position}. We no longer need to check this position.`);
} else {
showMessage(`Array is sorted for elements between position ${position} and the end of the array. We no longer need to check these positions.`);
}
break;
case COMPLETE:
for(i = 0; i < localArray.length; i++) {
document.getElementById(`array-element-${i}`).classList.add("sorted");
}
showMessage("Sorting Complete!");
break;
}
stepCounter++;
if(stepCounter >= steps.length) {
clearInterval(interval);
showRepeatButton();
}
}
}
// show message
function showMessage(message) {
const messageHTML = document.createElement("div");
messageHTML.id = "message";
messageHTML.innerHTML = message;
main.appendChild(messageHTML);
}
// bubble sort
function bubbleSort(numbers) {
const array = [...numbers];
const n = array.length;
const sortingSteps = [];
// perform a bubble sort on the array and store the steps performed on the sort
for (let i = 0; i < n-1; i++) {
sortingSteps.push({
operation: NEW_PASS,
position: i
});
for (let j = 0; j < n-i-1; j++) {
sortingSteps.push({
operation: CHECK,
position: j
});
if (array[j] > array[j+1]) {
// swap array[j+1] and array[j]
let temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
sortingSteps.push({
operation: SWAP,
position: j
});
sortingSteps.push({
operation: SWAPPED,
position: j
});
}
}
sortingSteps.push({
operation: SORTED_TO,
position: n - i - 1
});
sortingSteps.push({
operation: SORTED_TO,
position: n - i - 1
});
}
sortingSteps.push({
operation: COMPLETE,
position: 0
})
return {sortingSteps: sortingSteps, sortedArray: array};
}
function showRepeatButton() {
const button = document.createElement("button");
button.id = "start-button";
button.textContent = "Do It Again!";
button.onclick = repeat;
document.getElementsByTagName("body")[0].appendChild(button);
}
// generate random integers
function randomInt(min, max) {
return min + Math.floor(Math.random() * Math.floor(max));
}
// display the array in the DOM
function showArray(array) {
const arrayHTML = document.createElement("div");
arrayHTML.className = "numbers";
array.forEach((number, index) => {
const line = document.createElement("div");
line.id = `array-element-${index}`;
line.innerHTML = `${index} - ${number}`;
arrayHTML.appendChild(line);
});
main.innerHTML = "";
main.appendChild(arrayHTML);
}
// and explaining the step performed as each line of pseudocode is
// performed.