Skip to content

Commit e79b57f

Browse files
committed
Added bubble sort simulation to 09_Simulate
1 parent fe40603 commit e79b57f

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @name Bubble Sort
3+
* @description Sorts the randomly distributed bars
4+
* according to their height in ascending order
5+
* while simulating the whole sorting process.
6+
* Took references from Coding Challenge by The Coding Train.
7+
*/
8+
9+
let values = [];
10+
let i = 0;
11+
let j = 0;
12+
13+
// The statements in the setup() function
14+
// execute once when the program begins
15+
// The array is filled with random values in setup() function.
16+
function setup() {
17+
createCanvas(720, 400);
18+
for(let i = 0;i<width/8;i++){
19+
values.push(random(height));
20+
}
21+
}
22+
23+
// The statements in draw() function are executed until the
24+
// program is stopped. Each statement is executed in
25+
// sequence and after the last line is read, the first
26+
// line is executed again.
27+
function draw() {
28+
background(220);
29+
bubbleSort();
30+
simulateSorting();
31+
}
32+
33+
// The bubbleSort() function sorts taking 8 elements of the array
34+
// per frame. The algorithm behind this function is
35+
// bubble sort.
36+
function bubbleSort() {
37+
for(let k = 0;k<8;k++){
38+
if(i<values.length){
39+
let temp = values[j];
40+
if(values[j] > values[j+1]){
41+
values[j] = values[j+1];
42+
values[j+1] = temp;
43+
}
44+
j++;
45+
46+
if(j>=values.length-i-1){
47+
j = 0;
48+
i++;
49+
}
50+
}
51+
else{
52+
noLoop();
53+
}
54+
}
55+
}
56+
57+
// The simulateSorting() function helps in animating
58+
// the whole bubble sort algorithm
59+
// by drawing the rectangles using values
60+
// in the array as the length of the rectangle.
61+
function simulateSorting(){
62+
for(let i = 0;i<values.length;i++){
63+
stroke(100, 143, 143);
64+
fill(50);
65+
rect(i*8 , height, 8, -values[i],20);
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @name Bubble Sort
3+
* @description Sorts the randomly distributed bars
4+
* according to their height in ascending order
5+
* while simulating the whole sorting process.
6+
* Took references from Coding Challenge by The Coding Train.
7+
*/
8+
9+
let values = [];
10+
let i = 0;
11+
let j = 0;
12+
13+
// The statements in the setup() function
14+
// execute once when the program begins
15+
// The array is filled with random values in setup() function.
16+
function setup() {
17+
createCanvas(720, 400);
18+
for(let i = 0;i<width/8;i++){
19+
values.push(random(height));
20+
}
21+
}
22+
23+
// The statements in draw() function are executed until the
24+
// program is stopped. Each statement is executed in
25+
// sequence and after the last line is read, the first
26+
// line is executed again.
27+
function draw() {
28+
background(220);
29+
bubbleSort();
30+
simulateSorting();
31+
}
32+
33+
// The bubbleSort() function sorts taking 8 elements of the array
34+
// per frame. The algorithm behind this function is
35+
// bubble sort.
36+
function bubbleSort() {
37+
for(let k = 0;k<8;k++){
38+
if(i<values.length){
39+
let temp = values[j];
40+
if(values[j] > values[j+1]){
41+
values[j] = values[j+1];
42+
values[j+1] = temp;
43+
}
44+
j++;
45+
46+
if(j>=values.length-i-1){
47+
j = 0;
48+
i++;
49+
}
50+
}
51+
else{
52+
noLoop();
53+
}
54+
}
55+
}
56+
57+
// The simulateSorting() function helps in animating
58+
// the whole bubble sort algorithm
59+
// by drawing the rectangles using values
60+
// in the array as the length of the rectangle.
61+
function simulateSorting(){
62+
for(let i = 0;i<values.length;i++){
63+
stroke(100, 143, 143);
64+
fill(50);
65+
rect(i*8 , height, 8, -values[i],20);
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @name Bubble Sort
3+
* @description Sorts the randomly distributed bars
4+
* according to their height in ascending order
5+
* while simulating the whole sorting process.
6+
* Took references from Coding Challenge by The Coding Train.
7+
*/
8+
9+
let values = [];
10+
let i = 0;
11+
let j = 0;
12+
13+
// The statements in the setup() function
14+
// execute once when the program begins
15+
// The array is filled with random values in setup() function.
16+
function setup() {
17+
createCanvas(720, 400);
18+
for(let i = 0;i<width/8;i++){
19+
values.push(random(height));
20+
}
21+
}
22+
23+
// The statements in draw() function are executed until the
24+
// program is stopped. Each statement is executed in
25+
// sequence and after the last line is read, the first
26+
// line is executed again.
27+
function draw() {
28+
background(220);
29+
bubbleSort();
30+
simulateSorting();
31+
}
32+
33+
// The bubbleSort() function sorts taking 8 elements of the array
34+
// per frame. The algorithm behind this function is
35+
// bubble sort.
36+
function bubbleSort() {
37+
for(let k = 0;k<8;k++){
38+
if(i<values.length){
39+
let temp = values[j];
40+
if(values[j] > values[j+1]){
41+
values[j] = values[j+1];
42+
values[j+1] = temp;
43+
}
44+
j++;
45+
46+
if(j>=values.length-i-1){
47+
j = 0;
48+
i++;
49+
}
50+
}
51+
else{
52+
noLoop();
53+
}
54+
}
55+
}
56+
57+
// The simulateSorting() function helps in animating
58+
// the whole bubble sort algorithm
59+
// by drawing the rectangles using values
60+
// in the array as the length of the rectangle.
61+
function simulateSorting(){
62+
for(let i = 0;i<values.length;i++){
63+
stroke(100, 143, 143);
64+
fill(50);
65+
rect(i*8 , height, 8, -values[i],20);
66+
}
67+
}

0 commit comments

Comments
 (0)