Skip to content

Commit 99f25da

Browse files
Added audio and end sorted pass through, highlighting every bar
1 parent a03cefa commit 99f25da

7 files changed

+172
-16
lines changed
File renamed without changes.

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BubbleSortVis.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3+
import pygame
4+
import time
5+
6+
pygame.mixer.init()
7+
8+
9+
def play_tone(frequency, duration):
10+
sample_rate = 44100 # Hertz
11+
n_samples = int(sample_rate * duration)
12+
13+
# Create a sine wave
14+
t = np.linspace(0, duration, n_samples, False)
15+
wave = 0.5 * np.sin(2 * np.pi * frequency * t)
16+
17+
# Convert to 16-bit audio data
18+
wave = (wave * 32767).astype(np.int16)
19+
20+
# Duplicate for stereo if needed
21+
wave = np.column_stack((wave, wave)) # Convert to 2 channels (stereo)
22+
23+
# Make and play sound
24+
sound = pygame.sndarray.make_sound(wave)
25+
sound.play(-1)
26+
time.sleep(duration)
27+
sound.stop()
328

429

530
class Vis:
@@ -19,14 +44,14 @@ def __init__(self, a=50, barcol='white'):
1944
self.lst = np.array(self.lst)
2045

2146
def display(self):
22-
if self.end:
23-
plt.close()
24-
return
2547
plt.clf()
2648
plt.gcf().set_facecolor('black')
2749
plt.bar(self.x, self.lst, color=self.highlights)
2850
plt.axis('off')
2951
plt.pause(0.0001)
52+
if self.end:
53+
plt.close()
54+
return
3055

3156

3257
def bubble_sort(current: Vis):
@@ -39,8 +64,14 @@ def bubble_sort(current: Vis):
3964
unsorted[j] = temp
4065
current.highlights[j] = 'green'
4166
current.display()
67+
play_tone(unsorted[j]*50+100, 0.01)
4268
current.highlights[j] = current.barcol
4369

70+
for i in range(len(unsorted)):
71+
current.highlights[i] = 'green'
72+
current.display()
73+
play_tone(unsorted[i]*50+100, 0.01)
74+
4475
current.end = True
4576
current.display()
4677

InsertionSortVis.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3+
import pygame
4+
import time
5+
6+
pygame.mixer.init()
7+
8+
9+
def play_tone(frequency, duration):
10+
sample_rate = 44100 # Hertz
11+
n_samples = int(sample_rate * duration)
12+
13+
# Create a sine wave
14+
t = np.linspace(0, duration, n_samples, False)
15+
wave = 0.5 * np.sin(2 * np.pi * frequency * t)
16+
17+
# Convert to 16-bit audio data
18+
wave = (wave * 32767).astype(np.int16)
19+
20+
# Duplicate for stereo if needed
21+
wave = np.column_stack((wave, wave)) # Convert to 2 channels (stereo)
22+
23+
# Make and play sound
24+
sound = pygame.sndarray.make_sound(wave)
25+
sound.play(-1)
26+
time.sleep(duration)
27+
sound.stop()
328

429

530
class Vis:
@@ -19,14 +44,14 @@ def __init__(self, a=50, barcol='white'):
1944
self.lst = np.array(self.lst)
2045

2146
def display(self):
22-
if self.end:
23-
plt.close()
24-
return
2547
plt.clf()
2648
plt.gcf().set_facecolor('black')
2749
plt.bar(self.x, self.lst, color=self.highlights)
2850
plt.axis('off')
2951
plt.pause(0.0001)
52+
if self.end:
53+
plt.close()
54+
return
3055

3156

3257
def insertion_sort(current_vis: Vis):
@@ -40,8 +65,14 @@ def insertion_sort(current_vis: Vis):
4065
j -= 1
4166
current_vis.highlights[j] = 'green'
4267
current_vis.display()
68+
play_tone(unsorted[j]*50+100, 0.01)
4369
current_vis.highlights[j] = current_vis.barcol
4470

71+
for i in range(len(unsorted)):
72+
current_vis.highlights[i] = 'green'
73+
current_vis.display()
74+
play_tone(unsorted[i]*50+100, 0.01)
75+
4576
current_vis.end = True
4677
current_vis.display()
4778

MergeSortVis.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3+
import pygame
4+
import time
5+
6+
pygame.mixer.init()
7+
8+
9+
def play_tone(frequency, duration):
10+
sample_rate = 44100 # Hertz
11+
n_samples = int(sample_rate * duration)
12+
13+
# Create a sine wave
14+
t = np.linspace(0, duration, n_samples, False)
15+
wave = 0.5 * np.sin(2 * np.pi * frequency * t)
16+
17+
# Convert to 16-bit audio data
18+
wave = (wave * 32767).astype(np.int16)
19+
20+
# Duplicate for stereo if needed
21+
wave = np.column_stack((wave, wave)) # Convert to 2 channels (stereo)
22+
23+
# Make and play sound
24+
sound = pygame.sndarray.make_sound(wave)
25+
sound.play(-1)
26+
time.sleep(duration)
27+
sound.stop()
328

429

530
class Vis:
@@ -19,14 +44,14 @@ def __init__(self, a=100, barcol='white'):
1944
self.lst = np.array(self.lst)
2045

2146
def display(self):
22-
if self.end:
23-
plt.close()
24-
return
2547
plt.clf()
2648
plt.gcf().set_facecolor('black')
2749
plt.bar(self.x, self.lst, color=self.highlights)
2850
plt.axis('off')
2951
plt.pause(0.0001)
52+
if self.end:
53+
plt.close()
54+
return
3055

3156

3257
def _merge_sort(current: Vis, lo, hi):
@@ -45,28 +70,36 @@ def _merge_sort(current: Vis, lo, hi):
4570
l += 1
4671
current.highlights[lo+l+r] = 'green'
4772
current.display()
73+
play_tone(left[l-1]*50+100, 0.01)
4874
current.highlights[lo + l + r] = current.barcol
4975
else:
5076
current.lst[lo+l+r] = right[r]
5177
r += 1
5278
current.highlights[lo + l + r] = 'green'
5379
current.display()
80+
play_tone(right[r-1]*50+100, 0.01)
5481
current.highlights[lo + l + r] = current.barcol
5582

5683
if l < len(left):
5784
for i in range(len(left[l:])):
5885
current.lst[lo+l+r+i] = left[l+i]
5986
current.highlights[lo + l + r] = 'green'
6087
current.display()
88+
play_tone(left[l+i]*50+100, 0.01)
6189
current.highlights[lo + l + r] = current.barcol
6290
elif r < len(right):
6391
for i in range(len(right[r:])):
6492
current.lst[lo+l+r+i] = right[r+i]
6593
current.highlights[lo + l + r] = 'green'
6694
current.display()
95+
play_tone(right[r+i]*50+100, 0.01)
6796
current.highlights[lo + l + r] = current.barcol
6897

6998
if lo == 0 and hi == len(current.lst):
99+
for i in range(len(current.lst)):
100+
current.highlights[i] = 'green'
101+
current.display()
102+
play_tone(current.lst[i]*50+100, 0.01)
70103
current.end = True
71104
current.display()
72105

QuickSortVis.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3+
import pygame
4+
import time
5+
6+
pygame.mixer.init()
7+
8+
9+
def play_tone(frequency, duration):
10+
sample_rate = 44100 # Hertz
11+
n_samples = int(sample_rate * duration)
12+
13+
# Create a sine wave
14+
t = np.linspace(0, duration, n_samples, False)
15+
wave = 0.5 * np.sin(2 * np.pi * frequency * t)
16+
17+
# Convert to 16-bit audio data
18+
wave = (wave * 32767).astype(np.int16)
19+
20+
# Duplicate for stereo if needed
21+
wave = np.column_stack((wave, wave)) # Convert to 2 channels (stereo)
22+
23+
# Make and play sound
24+
sound = pygame.sndarray.make_sound(wave)
25+
sound.play(-1)
26+
time.sleep(duration)
27+
sound.stop()
328

429

530
class Vis:
@@ -19,14 +44,14 @@ def __init__(self, a=100, barcol='white'):
1944
self.lst = np.array(self.lst)
2045

2146
def display(self):
22-
if self.end:
23-
plt.close()
24-
return
2547
plt.clf()
2648
plt.gcf().set_facecolor('black')
2749
plt.bar(self.x, self.lst, color=self.highlights)
2850
plt.axis('off')
2951
plt.pause(0.0001)
52+
if self.end:
53+
plt.close()
54+
return
3055

3156

3257
def _quick_sort(current: Vis, lo, hi):
@@ -63,11 +88,16 @@ def _quick_sort(current: Vis, lo, hi):
6388
unsorted[j] = temp
6489

6590
current.display()
91+
play_tone((unsorted[i] + unsorted[j]) * 50 / 2 + 100, 0.01)
6692
current.highlights[lo+i], current.highlights[lo+j], current.highlights[lo+pivot_index] = current.barcol, current.barcol, current.barcol
6793

6894
res = _quick_sort(current, lo, lo+i) + _quick_sort(current, lo+i, hi)
6995

7096
if lo == 0 and hi == len(current.lst):
97+
for i in range(len(current.lst)):
98+
current.highlights[i] = 'green'
99+
current.display()
100+
play_tone(current.lst[i]*50+100, 0.01)
71101
current.end = True
72102
current.display()
73103

SelectionSortVis.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3+
import pygame
4+
import time
5+
6+
pygame.mixer.init()
7+
8+
9+
def play_tone(frequency, duration):
10+
sample_rate = 44100 # Hertz
11+
n_samples = int(sample_rate * duration)
12+
13+
# Create a sine wave
14+
t = np.linspace(0, duration, n_samples, False)
15+
wave = 0.5 * np.sin(2 * np.pi * frequency * t)
16+
17+
# Convert to 16-bit audio data
18+
wave = (wave * 32767).astype(np.int16)
19+
20+
# Duplicate for stereo if needed
21+
wave = np.column_stack((wave, wave)) # Convert to 2 channels (stereo)
22+
23+
# Make and play sound
24+
sound = pygame.sndarray.make_sound(wave)
25+
sound.play(-1)
26+
time.sleep(duration)
27+
sound.stop()
328

429

530
class Vis:
@@ -19,14 +44,14 @@ def __init__(self, a=50, barcol='white'):
1944
self.lst = np.array(self.lst)
2045

2146
def display(self):
22-
if self.end:
23-
plt.close()
24-
return
2547
plt.clf()
2648
plt.gcf().set_facecolor('black')
2749
plt.bar(self.x, self.lst, color=self.highlights)
2850
plt.axis('off')
2951
plt.pause(0.0001)
52+
if self.end:
53+
plt.close()
54+
return
3055

3156

3257
def selection_sort(current: Vis):
@@ -39,13 +64,19 @@ def selection_sort(current: Vis):
3964
current.highlights[min_index] = 'red'
4065
current.highlights[j] = 'green'
4166
current.display()
67+
play_tone(unsorted[j]*50+100, 0.01)
4268
current.highlights[min_index], current.highlights[j] = current.barcol, current.barcol
4369

4470
if i != min_index:
4571
temp = unsorted[i]
4672
unsorted[i] = unsorted[min_index]
4773
unsorted[min_index] = temp
4874

75+
for i in range(len(unsorted)):
76+
current.highlights[i] = 'green'
77+
current.display()
78+
play_tone(unsorted[i]*50+100, 0.01)
79+
4980
current.end = True
5081
current.display()
5182

0 commit comments

Comments
 (0)