Skip to content

Commit a82741a

Browse files
author
Kevin Nadro
committed
Merge remote-tracking branch 'parkjs814/master' into auto-generate-tracers
2 parents c948a50 + b0c446c commit a82741a

File tree

7 files changed

+134
-3
lines changed

7 files changed

+134
-3
lines changed

algorithm/category.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@
7979
"bucket": "Bucket Sort",
8080
"bubble": "Bubble Sort",
8181
"comb": "Comb Sort",
82-
"counting": "Counting Sort",
82+
"counting": "Counting Sort",
8383
"cycle": "Cycle Sort",
8484
"heap": "Heapsort",
8585
"insertion": "Insertion Sort",
8686
"merge": "Merge Sort",
87+
"pigeonhole": "Pigeonhole Sort",
8788
"quick": "Quicksort",
8889
"radix": "Radix Sort",
8990
"selection": "Selection Sort",
@@ -112,8 +113,9 @@
112113
"etc": {
113114
"list": {
114115
"flood_fill": "Flood Fill",
115-
"cellular_automata": "Cellular Automata",
116-
"create_maze": "Create Maze"
116+
"cellular_automata": "Cellular Automata",
117+
"create_maze": "Create Maze",
118+
"magic_square": "Magic Square"
117119
},
118120
"name": "Uncategorized"
119121
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var i = Math.floor (n/2);
2+
var j = n-1;
3+
4+
for ( var num = 1; num <= n*n; ) {
5+
logTracer._print ( 'i = ' + i );
6+
logTracer._print ( 'j = ' + j );
7+
8+
if( i == -1 && j == n ) {
9+
j = n - 2;
10+
i = 0;
11+
12+
logTracer._print ( 'Changing : ' );
13+
logTracer._print ( 'i = ' + i );
14+
logTracer._print ( 'j = ' + j );
15+
} else {
16+
if ( j == n ) {
17+
j = 0;
18+
logTracer._print ( 'Changing : ' + 'j = ' + j);
19+
}
20+
if ( i < 0 ) {
21+
i = n-1;
22+
logTracer._print ( 'Changing : ' + 'i = ' + i );
23+
}
24+
}
25+
26+
if ( A[i][j] > 0 ) {
27+
logTracer._print ( ' Cell already filled : Changing ' + ' i = ' + i + ' j = ' + j );
28+
j -= 2;
29+
i++;
30+
continue;
31+
} else {
32+
A[i][j] = num++;
33+
tracer._notify( i, j, A[i][j] )._wait ();
34+
tracer._denotify ( i, j );
35+
tracer._select ( i, j )._wait ();
36+
}
37+
j++;
38+
i--;
39+
}
40+
41+
logTracer._print ( 'Magic Constant is ' + n*(n*n+1)/2 );
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var n = 7;
2+
var A = new Array (n);
3+
for (var i = n - 1; i >= 0; i--) {
4+
A[i] = new Array (n);
5+
}
6+
7+
for ( var i = n -1; i >= 0; i-- ) {
8+
for ( var j = n - 1; j >= 0; j-- ) {
9+
A[i][j] = 0;
10+
}
11+
}
12+
13+
var tracer = new Array2DTracer ('Magic Square')._setData(A);
14+
var logTracer = new LogTracer ( 'Console' );

algorithm/etc/magic_square/desc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Magic Square": "In recreational mathematics, a magic square is an arrangement of distinct numbers (i.e., each number is used once), usually integers, in a square grid, where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number, called the magic constant. A magic square has the same number of rows as it has columns, and in conventional math notation, 'n' stands for the number of rows (and columns) it has. Thus, a magic square always contains n2 numbers, and its size (the number of rows [and columns] it has) is described as being of order n. A magic square that contains the integers from 1 to n2 is called a normal magic square. (The term magic square is also sometimes used to refer to any of various types of word squares.)",
3+
"Complexity": {
4+
"time": " O(N<sup>2</sup>)",
5+
"space": "O(N<sup>2</sup>)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Magic_square'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Magic Square"
12+
}
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var min = A[0];
2+
var max = A[0];
3+
4+
for( var i = 1; i < N; i++ ) {
5+
if( A[i] < min ) {
6+
min = A[i];
7+
}
8+
if( A[i] > max ) {
9+
max = A[i];
10+
}
11+
}
12+
var range = max - min + 1;
13+
14+
var holes = new Array ( range );
15+
for ( var i = 0; i < range; i++ ) {
16+
holes[i] = [];
17+
}
18+
tracer2._setData( holes );
19+
20+
logTracer._print ( 'Filling up holes' );
21+
for ( var i = 0; i < N ; i++ ) {
22+
tracer1._select ( i )._wait ();
23+
24+
holes[ A[i] - min ].push( A[i] );
25+
26+
tracer2._setData( holes );
27+
tracer1._deselect ( i );
28+
}
29+
30+
logTracer._print ( 'Building sorted array' );
31+
var k = 0;
32+
for ( var i = 0; i < range ; i++ ) {
33+
for (var j = 0; j < holes[i].length; j++ ) {
34+
tracer2._select ( i, j )._wait ();
35+
A[k++] = holes[i][j];
36+
tracer1._notify ( k-1, A[k-1] )._wait ();
37+
tracer2._deselect ( i, j );
38+
tracer1._denotify ( k-1 );
39+
}
40+
}
41+
42+
logTracer._print ( 'Sorted array is ' + A );
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var A = Array1D.random(7);
2+
var N = A.length;
3+
4+
var tracer1 = new Array1DTracer ( 'Array' )._setData ( A );
5+
var tracer2 = new Array2DTracer ( 'Holes' );
6+
var logTracer = new LogTracer ( 'Console' );
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Pigeonhole Sort": "Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements (n) and the number of possible key values (N) are approximately the same.",
3+
"Complexity": {
4+
"time": " O(n + N)",
5+
"space": "O(n)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Pigeonhole_sort'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Pigeonhole Sort"
12+
}
13+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy