Skip to content

Commit 7ca85df

Browse files
committed
Added searching algorithm
1 parent e3d8194 commit 7ca85df

File tree

5 files changed

+74
-207
lines changed

5 files changed

+74
-207
lines changed

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ <h1>Sorting Algorithm Visualizer</h1>
3636
<input type="radio" name="algo-name" value="merge-sort" />Merge
3737
Sort</label
3838
> --->
39-
4039

4140
<br />
4241
<br />

searching-algorithm/base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// class for sorting
2-
31
//get selected algorithm
42
function getAlgo() {
53
var algo = document.getElementsByName("algo-name");
@@ -32,6 +30,8 @@ function randomData(max, range) {
3230
n++;
3331
}
3432
}
33+
data.sort((a, b) => a - b);
34+
console.log(data);
3535
return data;
3636
}
3737

searching-algorithm/function.js

Lines changed: 57 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ var svg,
99
traverseColor = "#ffcaa1",
1010
smallestColor = "#ab87ff",
1111
unsortedColor = "#add8e6",
12-
sortedColor = "#56b4d3",
12+
sortedColor = "green",
1313
isSorting = false,
1414
isSorted = false;
1515

16-
var swooshAudio = new Audio("./sound-effects/swoosh.mp3");
17-
var completeAudio = new Audio("./sound-effects/complete.mp3");
16+
var swooshAudio = new Audio("./../sound-effects/swoosh.mp3");
17+
var completeAudio = new Audio("./../sound-effects/complete.mp3");
1818
swooshAudio.volume = 0.3;
1919
completeAudio.volume = 0.3;
2020

@@ -32,249 +32,111 @@ var heightScale = d3
3232
// initialized a chart with random value
3333
createChart(data);
3434

35-
// javascript objects for performing different sorting algorithm
36-
const SortAlgo = {
37-
// bubble sort methods to perform bubble sort algorithm
38-
bubbleSort() {
35+
const SearchAlgo = {
36+
liearSearch() {
3937
// promise for async bubble sort with delay
4038

4139
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
4240
// async function for bubble sort
4341

44-
async function sort(self) {
42+
async function search(self) {
4543
var temp;
4644
for (let i = 0; i < data.length - 1; i++) {
4745
// If user click on stop button then this function will stop performing here.
48-
if (self.abort) {
49-
self.abort = false;
50-
return;
51-
}
46+
5247
// changing initial smallest bar color
53-
changeBarColor(data[0], smallestColor);
5448
await timer(time);
55-
for (j = 0; j < data.length - i - 1; j++) {
56-
// If user click on stop button then this function will stop performing here.
57-
if (self.abort) {
58-
self.abort = false;
59-
changeBarColor(data[j], unsortedColor);
60-
return;
61-
}
62-
await timer(time);
63-
changeBarColor(data[j + 1], traverseColor);
49+
changeBarColor(data[i], traverseColor);
50+
console.log(data[i]);
51+
await timer(time);
52+
console.log("Searching");
53+
54+
if (data[i] == target) {
55+
changeBarColor(data[i], sortedColor);
56+
console.log("found");
6457
await timer(time);
65-
if (data[j] > data[j + 1]) {
66-
temp = data[j];
67-
data[j] = data[j + 1];
68-
data[j + 1] = temp;
69-
changeBarColor(data[j + 1], smallestColor);
70-
swooshAudio.play();
71-
swapBar(data);
72-
await timer(time);
73-
} else {
74-
changeBarColor(data[j + 1], smallestColor);
75-
}
76-
changeBarColor(data[j], unsortedColor);
58+
break;
7759
}
78-
changeBarColor(data[j], sortedColor);
7960
}
8061

8162
// after complete sorting complete making all the bar green and playing complete sound effects
82-
svg.selectAll("rect").style("fill", "#56b4d3");
8363

8464
completeAudio.play();
8565
isSorting = false;
8666
isSorted = true;
87-
togglePlay();
8867
}
8968

9069
// calling async function here
91-
sort(this);
70+
search(this);
9271
},
9372

94-
// selection sort methods
95-
selectionSort() {
96-
// promise for async selection sort with delay
73+
binarySearch() {
74+
// promise for async bubble sort with delay
75+
9776
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
77+
// async function for bubble sort
9878

99-
// async function for selection sort algorithm
100-
async function sort(self) {
101-
for (let i = 0; i < data.length; i++) {
102-
// Stoping execution here if users wants to stop.
103-
if (self.abort) {
104-
self.abort = false;
105-
return;
106-
}
107-
smallest = data[i];
108-
pos = i;
109-
changeBarColor(smallest, smallestColor);
79+
async function search(self) {
80+
console.log(target);
81+
let l = 0,
82+
r = data.length - 1,
83+
mid;
84+
while (l <= r) {
85+
// If user click on stop button then this function will stop performing here.
86+
mid = (l + r) / 2;
11087
await timer(time);
111-
for (var j = i + 1; j < data.length; j++) {
112-
if (self.abort) {
113-
self.abort = false;
114-
return;
115-
}
116-
changeBarColor(data[j], traverseColor);
117-
if (smallest > data[j]) {
118-
await timer(time);
119-
changeBarColor(smallest, unsortedColor);
120-
smallest = data[j];
121-
pos = j;
122-
}
123-
124-
changeBarColor(smallest, smallestColor);
88+
changeBarColor(data[mid], traverseColor);
89+
if (data[mid] == target) {
90+
changeBarColor(data[mid], sortedColor);
91+
console.log("found");
12592
await timer(time);
126-
changeBarColor(data[j], unsortedColor);
127-
}
128-
if (data[i] != smallest) {
129-
temp = data[i];
130-
data[i] = smallest;
131-
data[pos] = temp;
132-
// playing swapping sound
133-
swooshAudio.play();
134-
}
135-
// swapping bar and changing smallest color
136-
changeBarColor(smallest, sortedColor);
137-
swapBar(data);
138-
await timer(time); // then the created Promise can be awaited
139-
}
140-
141-
// After complete sorting algorithm making all the bar green.
142-
svg.selectAll("rect").style("fill", "#56b4d3");
143-
144-
completeAudio.play();
145-
isSorting = false;
146-
isSorted = true;
147-
togglePlay();
148-
}
149-
// calling sort function here
150-
sort(this);
151-
},
152-
153-
//Merge Sort methods to perform merge sort algorithm
154-
mergeSort() {
155-
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
156-
157-
// async function for selection sort algorithm
158-
async function sort(self, arr, l, r) {
159-
// l is for left index and r is
160-
// right index of the sub-array
161-
// of arr to be sorted */
162-
if (r > l) {
163-
var m = l + parseInt((r - l) / 2);
164-
165-
sort(this, arr, l, m);
166-
167-
sort(this, arr, m + 1, r);
168-
169-
var n1 = m - l + 1;
170-
var n2 = r - m;
171-
172-
// Create temp arrays
173-
var L = new Array(n1);
174-
var R = new Array(n2);
175-
176-
// Copy data to temp arrays L[] and R[]
177-
for (var i = 0; i < n1; i++) L[i] = arr[l + i];
178-
for (var j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
179-
180-
// Merge the temp arrays back into arr[l..r]
181-
182-
// Initial index of first subarray
183-
var i = 0;
184-
185-
// Initial index of second subarray
186-
var j = 0;
187-
188-
// Initial index of merged subarray
189-
var k = l;
190-
191-
while (i < n1 && j < n2) {
192-
if (L[i] <= R[j]) {
193-
arr[k] = L[i];
194-
i++;
195-
} else {
196-
arr[k] = R[j];
197-
j++;
198-
}
199-
k++;
200-
}
201-
202-
// Copy the remaining elements of
203-
// L[], if there are any
204-
while (i < n1) {
205-
arr[k] = L[i];
206-
i++;
207-
k++;
93+
break;
94+
} else if (data[mid] < target) {
95+
l = mid + 1;
96+
} else {
97+
r = mid - 1;
20898
}
99+
// changing initial smallest bar color
209100

210-
// Copy the remaining elements of
211-
// R[], if there are any
212-
while (j < n2) {
213-
arr[k] = R[j];
214-
j++;
215-
k++;
216-
}
217-
swapBar(data);
101+
await timer(time);
218102
}
219103

220-
console.log(data);
221-
svg.selectAll("rect").style("fill", "#56b4d3");
104+
// after complete sorting complete making all the bar green and playing complete sound effects
105+
222106
completeAudio.play();
223107
isSorting = false;
224108
isSorted = true;
225-
togglePlay();
226109
}
227110

228-
// calling sort function here
229-
sort(this, data, 0, data.length - 1);
230-
},
231-
232-
// If user wants to stop the sorting process then this function will be called and sorting algorithm will be stopped immediately.
233-
sortStop() {
234-
this.abort = true;
235-
isSorting = false;
111+
// calling async function here
112+
search(this);
236113
},
237114
};
238115

239-
function stopSorting() {
240-
const stopSorting = SortAlgo.sortStop.bind(SortAlgo);
241-
stopSorting();
242-
}
243-
function startSorting() {
244-
if (getAlgo() == "bubble-sort") {
245-
const bubbleSortStarted = SortAlgo.bubbleSort.bind(SortAlgo);
246-
bubbleSortStarted();
247-
} else if (getAlgo() == "selection-sort") {
248-
const selectionSortStarted = SortAlgo.selectionSort.bind(SortAlgo);
249-
selectionSortStarted();
116+
function startSearching() {
117+
if (getAlgo() == "linear-search") {
118+
const linearSearchStarted = SearchAlgo.liearSearch.bind(SearchAlgo);
119+
linearSearchStarted();
120+
} else if (getAlgo() == "binary-search") {
121+
const binarySearchStarted = SearchAlgo.binarySearch.bind(SearchAlgo);
122+
binarySearchStarted();
250123
} else if (getAlgo() == "merge-sort") {
251124
const mergeSortStarted = SortAlgo.mergeSort.bind(SortAlgo);
252125
mergeSortStarted();
253126
}
254127
}
255128

256-
document.getElementById("sort").addEventListener("click", function () {
257-
isSorting = true;
258-
startSorting();
259-
togglePlay();
260-
});
129+
document.getElementById("search").addEventListener("click", function () {
130+
target = parseInt(document.getElementById("targetValue").value);
261131

262-
document.getElementById("stop").addEventListener("click", function () {
263-
if (isSorting) {
264-
stopSorting();
265-
togglePlay();
132+
if (isNaN(target)) {
133+
alert("Please enter a valid number");
134+
} else {
135+
startSearching();
266136
}
267137
});
268138

269139
document.getElementById("random-data").addEventListener("click", function () {
270-
if (isSorting) {
271-
stopSorting();
272-
togglePlay();
273-
}
274-
if (isSorted) {
275-
isSorted = false;
276-
document.getElementById("sort").classList.remove("none");
277-
}
278140
svg.remove();
279141
var data = randomData(maxElement, dataRange);
280142
createChart(data);

searching-algorithm/index.html

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ <h1>Sorting Algorithm Visualizer</h1>
2121
><input
2222
type="radio"
2323
name="algo-name"
24-
value="bubble-sort"
24+
value="binary-search"
2525
checked
26-
/>Bubble Sort</label
26+
/>Binary Search</label
2727
>
2828
<label>
29-
<input
30-
type="radio"
31-
name="algo-name"
32-
value="selection-sort"
33-
/>Selection Sort</label
29+
<input type="radio" name="algo-name" value="linear-search" />Linear
30+
Search</label
3431
>
3532
<!--- <label>
3633
<input type="radio" name="algo-name" value="merge-sort" />Merge
@@ -51,16 +48,22 @@ <h1>Sorting Algorithm Visualizer</h1>
5148
onchange="setSpeed()"
5249
/>
5350
</label>
51+
<br /><br />
52+
<label for="">
53+
Search For
54+
<input class="input-element" id="targetValue" type="text" />
55+
</label>
5456
</form>
55-
<button id="sort">Sort</button>
56-
<button id="stop" class="none stop">Stop</button>
57+
<button id="search">Search</button>
5758
</div>
5859
<div class="container">
5960
<div class="bar-chart">
6061
<div id="chart"></div>
6162
<hr />
6263
</div>
6364
</div>
65+
<div class="container foundNotice"></div>
66+
6467
<footer>
6568
<div class="footer-text">
6669
<p>

searching-algorithm/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ button {
3939
padding: 2px 30px;
4040
border: none;
4141
}
42+
.input-element {
43+
height: 25px;
44+
}
4245

4346
button {
4447
background-image: linear-gradient(

0 commit comments

Comments
 (0)