-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
180 lines (141 loc) · 4.85 KB
/
script.js
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
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function swap(arr, i, j) {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
async function bubbleSort() {
const inputField = document.getElementById("arrayInput");
const arr = inputField.value.split(",").map(item => Number(item.trim()));
const len = arr.length;
let swapped;
do {
swapped = false;
for (let i = 0; i < len - 1; i++) {
if (arr[i] > arr[i + 1]) {
swap(arr, i, i + 1);
// Display the array in the animation
displayArray(arr);
await sleep(500); // Delay to show animation
swapped = true;
}
}
} while (swapped);
}
async function insertionSort() {
const inputField = document.getElementById("arrayInput");
const arr = inputField.value.split(",").map(item => Number(item.trim()));
const len = arr.length;
for (let i = 1; i < len; i++) {
let currentElement = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > currentElement) {
arr[j + 1] = arr[j];
j--;
// Display the array in the animation
displayArray(arr);
await sleep(500); // Delay to show animation
}
arr[j + 1] = currentElement;
// Display the array in the animation
displayArray(arr);
await sleep(500); // Delay to show animation
}
}
async function selectionSort() {
const inputField = document.getElementById("arrayInput");
const arr = inputField.value.split(",").map(item => Number(item.trim()));
const len = arr.length;
for (let i = 0; i < len - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex !== i) {
swap(arr, i, minIndex);
}
// Display the array in the animation
displayArray(arr);
await sleep(500); // Delay to show animation
}
}
async function mergeSort(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array.");
}
if (arr.length <= 1) {
return arr;
}
// Validate the array to ensure all elements are numbers
if (!arr.every((element) => typeof element === "number" && !isNaN(element))) {
throw new Error("Array must contain only numeric values.");
}
const middle = Math.floor(arr.length / 2);
const left = arr.slice(0, middle);
const right = arr.slice(middle);
return merge(await mergeSort(left), await mergeSort(right));
}
function merge(left, right) {
let mergedArray = [];
let leftIndex = 0;
let rightIndex = 0;
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
mergedArray.push(left[leftIndex]);
leftIndex++;
} else {
mergedArray.push(right[rightIndex]);
rightIndex++;
}
}
return mergedArray.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}
async function quickSort() {
const inputField = document.getElementById("arrayInput");
const arr = inputField.value.split(",").map(item => Number(item.trim()));
if (arr.length <= 1) {
return arr;
}
const pivot = arr[0];
const left = [];
const right = [];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
const sortedArr = await Promise.all([
quickSort(left),
quickSort(right)
]).then(result => result[0].concat(pivot, result[1]));
// Display the array in the animation
displayArray(sortedArr);
await sleep(500); // Delay to show animation
return sortedArr;
}
function displayArray(arr) {
const container = document.getElementById("container");
container.innerHTML = "";
const maxValue = Math.max(...arr);
const minSize = 25; // Minimum circle size
const maxSize = 80; // Maximum circle size
const sizeRange = maxSize - minSize;
for (let i = 0; i < arr.length; i++) {
const circle = document.createElement("div");
circle.classList.add("circle");
const circleSize = minSize + (arr[i] / maxValue) * sizeRange;
circle.style.width = circleSize + "px";
circle.style.height = circleSize + "px";
circle.textContent = arr[i];
container.appendChild(circle);
}
}
function startAnimation() {
const inputField = document.getElementById("arrayInput");
const inputArray = inputField.value.split(",").map(item => Number(item.trim()));
bubbleSort(inputArray);
}