-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
113 lines (97 loc) · 3.19 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
function sortNumbers() {
const numbersInput = document.getElementById('numbers');
const sortingTechnique = document.getElementById('sortingTechnique').value;
const resultDiv = document.getElementById('result');
// Get the numbers from the input and convert them to an array
const numbersArray = numbersInput.value.split(',').map(Number);
// Check if the input is valid
if (!numbersArray.every(number => !isNaN(number))) {
resultDiv.innerHTML = '<p>Please enter valid numbers.</p>';
return;
}
// Choose the sorting technique
let sortedNumbers;
switch (sortingTechnique) {
case 'bubbleSort':
sortedNumbers = bubbleSort(numbersArray.slice());
break;
case 'selectionSort':
sortedNumbers = selectionSort(numbersArray.slice());
break;
case 'insertionSort':
sortedNumbers = selectionSort(numbersArray.slice());
break;
case 'mergeSort':
sortedNumbers = selectionSort(numbersArray.slice());
break;
default:
resultDiv.innerHTML = '<p>Invalid sorting technique selected.</p>';
return;
}
// Display the sorted numbers
resultDiv.innerHTML = `<p>Sorted numbers using ${sortingTechnique}:</p>
<p>${sortedNumbers.join(', ')}</p>`;
}
function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
function selectionSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
}
return arr;
}
function insertionSort(arr) {
const n = arr.length;
for (let i = 1; i < n; i++) {
let currentElement = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > currentElement) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = currentElement;
}
return arr;
}
function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
const middle = Math.floor(arr.length / 2);
const left = arr.slice(0, middle);
const right = arr.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
let result = [];
let leftIndex = 0;
let rightIndex = 0;
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}
}
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}