Skip to content

Commit 7941164

Browse files
authored
Merge pull request #995 from Swapnil-2001/quicksort-new
Add Quicksort visualization to examples
2 parents fd318c1 + c0a033f commit 7941164

File tree

4 files changed

+512
-0
lines changed

4 files changed

+512
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* @name Quicksort
3+
* @frame 710,400
4+
* @description This is a simulation of the Quicksort
5+
* sorting algorithm. We start with an array of bars
6+
* and sort them according to their height in ascending
7+
* order. References taken from a coding challenge by
8+
* The Coding Train.<br><br>
9+
* Quicksort is a divide-and-conquer algorithm: it
10+
* performs sorting by dividing the original array into
11+
* smaller subarrays and solving them independently,
12+
* loosely speaking. It involves picking an element of
13+
* the array as the pivot element and partitioning the
14+
* given array around the picked pivot.<br>
15+
* Partitioning refers to arranging the given array(or
16+
* subarray) in such a way that all elements to the left
17+
* of the pivot element are smaller than it and all
18+
* elements to its right are larger than it. Thus, we have
19+
* a reference point from where we proceed to sort the
20+
* left and right 'halves' of the array, and eventually
21+
* arrive at an array sorted in ascending order.
22+
* <a href="https://www.geeksforgeeks.org/quick-sort/">
23+
* More</a><br>
24+
*/
25+
26+
// width of each bar is taken as 8.
27+
let values = [];
28+
// The array 'states' helps in identifying the pivot index
29+
// at every step, and also the subarray which is being sorted
30+
// at any given time.
31+
let states = [];
32+
33+
// The setup() function is called once when the program
34+
// starts. Here, we fill the array 'values' with random values
35+
// and the array 'states' with a value of -1 for each position.
36+
function setup() {
37+
createCanvas(710, 400);
38+
for(let i = 0; i < width/8; i++) {
39+
values.push(random(height));
40+
states.push(-1);
41+
}
42+
quickSort(0, values.length - 1);
43+
}
44+
45+
// The statements in draw() function are executed continuously
46+
// until the program is stopped. Each statement is executed
47+
// sequentially and after the last line is read, the first
48+
// line is executed again.
49+
function draw() {
50+
background(140);
51+
for(let i = 0; i < values.length; i++) {
52+
// color coding
53+
if (states[i] == 0) {
54+
// color for the bar at the pivot index
55+
fill('#E0777D');
56+
} else if (states[i] == 1) {
57+
// color for the bars being sorted currently
58+
fill('#D6FFB7');
59+
} else {
60+
fill(255);
61+
}
62+
rect(i * 8, height - values[i], 8, values[i]);
63+
}
64+
}
65+
66+
async function quickSort(start, end) {
67+
if (start > end) { // Nothing to sort!
68+
return;
69+
}
70+
// partition() returns the index of the pivot element.
71+
// Once partition() is executed, all elements to the
72+
// left of the pivot element are smaller than it and
73+
// all elements to its right are larger than it.
74+
let index = await partition(start, end);
75+
// restore original state
76+
states[index] = -1;
77+
await Promise.all(
78+
[quickSort(start, index - 1),
79+
quickSort(index + 1, end)
80+
]);
81+
}
82+
83+
// We have chosen the element at the last index as
84+
// the pivot element, but we could've made different
85+
// choices, e.g. take the first element as pivot.
86+
async function partition(start, end) {
87+
for (let i = start; i < end; i++) {
88+
// identify the elements being considered currently
89+
states[i] = 1;
90+
}
91+
// Quicksort algorithm
92+
let pivotIndex = start;
93+
// make pivot index distinct
94+
states[pivotIndex] = 0;
95+
let pivotElement = values[end];
96+
for (let i = start; i < end; i++) {
97+
if (values[i] < pivotElement) {
98+
await swap(i, pivotIndex);
99+
states[pivotIndex] = -1;
100+
pivotIndex++;
101+
states[pivotIndex] = 0;
102+
}
103+
}
104+
await swap(end, pivotIndex);
105+
for (let i = start; i < end; i++) {
106+
// restore original state
107+
if (i != pivotIndex) {
108+
states[i] = -1;
109+
}
110+
}
111+
return pivotIndex;
112+
}
113+
114+
// swaps elements of 'values' at indices 'i' and 'j'
115+
async function swap(i, j) {
116+
// adjust the pace of the simulation by changing the
117+
// value
118+
await sleep(25);
119+
let temp = values[i];
120+
values[i] = values[j];
121+
values[j] = temp;
122+
}
123+
124+
// custom helper function to deliberately slow down
125+
// the sorting process and make visualization easy
126+
function sleep(ms) {
127+
return new Promise(resolve => setTimeout(resolve, ms));
128+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* @name Quicksort
3+
* @frame 710,400
4+
* @description This is a simulation of the Quicksort
5+
* sorting algorithm. We start with an array of bars
6+
* and sort them according to their height in ascending
7+
* order. References taken from a coding challenge by
8+
* The Coding Train.<br><br>
9+
* Quicksort is a divide-and-conquer algorithm: it
10+
* performs sorting by dividing the original array into
11+
* smaller subarrays and solving them independently,
12+
* loosely speaking. It involves picking an element of
13+
* the array as the pivot element and partitioning the
14+
* given array around the picked pivot.<br>
15+
* Partitioning refers to arranging the given array(or
16+
* subarray) in such a way that all elements to the left
17+
* of the pivot element are smaller than it and all
18+
* elements to its right are larger than it. Thus, we have
19+
* a reference point from where we proceed to sort the
20+
* left and right 'halves' of the array, and eventually
21+
* arrive at an array sorted in ascending order.
22+
* <a href="https://www.geeksforgeeks.org/quick-sort/">
23+
* More</a><br>
24+
*/
25+
26+
// width of each bar is taken as 8.
27+
let values = [];
28+
// The array 'states' helps in identifying the pivot index
29+
// at every step, and also the subarray which is being sorted
30+
// at any given time.
31+
let states = [];
32+
33+
// The setup() function is called once when the program
34+
// starts. Here, we fill the array 'values' with random values
35+
// and the array 'states' with a value of -1 for each position.
36+
function setup() {
37+
createCanvas(710, 400);
38+
for(let i = 0; i < width/8; i++) {
39+
values.push(random(height));
40+
states.push(-1);
41+
}
42+
quickSort(0, values.length - 1);
43+
}
44+
45+
// The statements in draw() function are executed continuously
46+
// until the program is stopped. Each statement is executed
47+
// sequentially and after the last line is read, the first
48+
// line is executed again.
49+
function draw() {
50+
background(140);
51+
for(let i = 0; i < values.length; i++) {
52+
// color coding
53+
if (states[i] == 0) {
54+
// color for the bar at the pivot index
55+
fill('#E0777D');
56+
} else if (states[i] == 1) {
57+
// color for the bars being sorted currently
58+
fill('#D6FFB7');
59+
} else {
60+
fill(255);
61+
}
62+
rect(i * 8, height - values[i], 8, values[i]);
63+
}
64+
}
65+
66+
async function quickSort(start, end) {
67+
if (start > end) { // Nothing to sort!
68+
return;
69+
}
70+
// partition() returns the index of the pivot element.
71+
// Once partition() is executed, all elements to the
72+
// left of the pivot element are smaller than it and
73+
// all elements to its right are larger than it.
74+
let index = await partition(start, end);
75+
// restore original state
76+
states[index] = -1;
77+
await Promise.all(
78+
[quickSort(start, index - 1),
79+
quickSort(index + 1, end)
80+
]);
81+
}
82+
83+
// We have chosen the element at the last index as
84+
// the pivot element, but we could've made different
85+
// choices, e.g. take the first element as pivot.
86+
async function partition(start, end) {
87+
for (let i = start; i < end; i++) {
88+
// identify the elements being considered currently
89+
states[i] = 1;
90+
}
91+
// Quicksort algorithm
92+
let pivotIndex = start;
93+
// make pivot index distinct
94+
states[pivotIndex] = 0;
95+
let pivotElement = values[end];
96+
for (let i = start; i < end; i++) {
97+
if (values[i] < pivotElement) {
98+
await swap(i, pivotIndex);
99+
states[pivotIndex] = -1;
100+
pivotIndex++;
101+
states[pivotIndex] = 0;
102+
}
103+
}
104+
await swap(end, pivotIndex);
105+
for (let i = start; i < end; i++) {
106+
// restore original state
107+
if (i != pivotIndex) {
108+
states[i] = -1;
109+
}
110+
}
111+
return pivotIndex;
112+
}
113+
114+
// swaps elements of 'values' at indices 'i' and 'j'
115+
async function swap(i, j) {
116+
// adjust the pace of the simulation by changing the
117+
// value
118+
await sleep(25);
119+
let temp = values[i];
120+
values[i] = values[j];
121+
values[j] = temp;
122+
}
123+
124+
// custom helper function to deliberately slow down
125+
// the sorting process and make visualization easy
126+
function sleep(ms) {
127+
return new Promise(resolve => setTimeout(resolve, ms));
128+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* @name Quicksort
3+
* @frame 710,400
4+
* @description This is a simulation of the Quicksort
5+
* sorting algorithm. We start with an array of bars
6+
* and sort them according to their height in ascending
7+
* order. References taken from a coding challenge by
8+
* The Coding Train.<br><br>
9+
* Quicksort is a divide-and-conquer algorithm: it
10+
* performs sorting by dividing the original array into
11+
* smaller subarrays and solving them independently,
12+
* loosely speaking. It involves picking an element of
13+
* the array as the pivot element and partitioning the
14+
* given array around the picked pivot.<br>
15+
* Partitioning refers to arranging the given array(or
16+
* subarray) in such a way that all elements to the left
17+
* of the pivot element are smaller than it and all
18+
* elements to its right are larger than it. Thus, we have
19+
* a reference point from where we proceed to sort the
20+
* left and right 'halves' of the array, and eventually
21+
* arrive at an array sorted in ascending order.
22+
* <a href="https://www.geeksforgeeks.org/quick-sort/">
23+
* More</a><br>
24+
*/
25+
26+
// width of each bar is taken as 8.
27+
let values = [];
28+
// The array 'states' helps in identifying the pivot index
29+
// at every step, and also the subarray which is being sorted
30+
// at any given time.
31+
let states = [];
32+
33+
// The setup() function is called once when the program
34+
// starts. Here, we fill the array 'values' with random values
35+
// and the array 'states' with a value of -1 for each position.
36+
function setup() {
37+
createCanvas(710, 400);
38+
for(let i = 0; i < width/8; i++) {
39+
values.push(random(height));
40+
states.push(-1);
41+
}
42+
quickSort(0, values.length - 1);
43+
}
44+
45+
// The statements in draw() function are executed continuously
46+
// until the program is stopped. Each statement is executed
47+
// sequentially and after the last line is read, the first
48+
// line is executed again.
49+
function draw() {
50+
background(140);
51+
for(let i = 0; i < values.length; i++) {
52+
// color coding
53+
if (states[i] == 0) {
54+
// color for the bar at the pivot index
55+
fill('#E0777D');
56+
} else if (states[i] == 1) {
57+
// color for the bars being sorted currently
58+
fill('#D6FFB7');
59+
} else {
60+
fill(255);
61+
}
62+
rect(i * 8, height - values[i], 8, values[i]);
63+
}
64+
}
65+
66+
async function quickSort(start, end) {
67+
if (start > end) { // Nothing to sort!
68+
return;
69+
}
70+
// partition() returns the index of the pivot element.
71+
// Once partition() is executed, all elements to the
72+
// left of the pivot element are smaller than it and
73+
// all elements to its right are larger than it.
74+
let index = await partition(start, end);
75+
// restore original state
76+
states[index] = -1;
77+
await Promise.all(
78+
[quickSort(start, index - 1),
79+
quickSort(index + 1, end)
80+
]);
81+
}
82+
83+
// We have chosen the element at the last index as
84+
// the pivot element, but we could've made different
85+
// choices, e.g. take the first element as pivot.
86+
async function partition(start, end) {
87+
for (let i = start; i < end; i++) {
88+
// identify the elements being considered currently
89+
states[i] = 1;
90+
}
91+
// Quicksort algorithm
92+
let pivotIndex = start;
93+
// make pivot index distinct
94+
states[pivotIndex] = 0;
95+
let pivotElement = values[end];
96+
for (let i = start; i < end; i++) {
97+
if (values[i] < pivotElement) {
98+
await swap(i, pivotIndex);
99+
states[pivotIndex] = -1;
100+
pivotIndex++;
101+
states[pivotIndex] = 0;
102+
}
103+
}
104+
await swap(end, pivotIndex);
105+
for (let i = start; i < end; i++) {
106+
// restore original state
107+
if (i != pivotIndex) {
108+
states[i] = -1;
109+
}
110+
}
111+
return pivotIndex;
112+
}
113+
114+
// swaps elements of 'values' at indices 'i' and 'j'
115+
async function swap(i, j) {
116+
// adjust the pace of the simulation by changing the
117+
// value
118+
await sleep(25);
119+
let temp = values[i];
120+
values[i] = values[j];
121+
values[j] = temp;
122+
}
123+
124+
// custom helper function to deliberately slow down
125+
// the sorting process and make visualization easy
126+
function sleep(ms) {
127+
return new Promise(resolve => setTimeout(resolve, ms));
128+
}

0 commit comments

Comments
 (0)