Skip to content

Commit e6f0864

Browse files
author
Michael Kabatek
committed
Inital commit on data structures research
0 parents  commit e6f0864

File tree

141 files changed

+12844
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+12844
-0
lines changed

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.class
2+
3+
# Mobile Tools for Java (J2ME)
4+
.mtj.tmp/
5+
6+
# Package Files #
7+
*.jar
8+
*.war
9+
*.ear
10+
11+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12+
hs_err_pid*
13+
14+
#Notepad++ swap
15+
*.un~
16+
17+
#vim swap
18+
*~

ArrayTestRandom/ArrayTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
4+
public class ArrayTest {
5+
public static void main(String[ ] args) {
6+
int data[ ] = new int[10];
7+
Random rand = new Random( ); // a pseudo-random number generator
8+
rand.setSeed(System.currentTimeMillis( )); // use current time as a seed
9+
// fill the data array with pseudo-random numbers from 0 to 99, inclusive
10+
for (int i = 0; i < data.length; i++)
11+
data[i] = rand.nextInt(100); // the next pseudo-random number
12+
int[ ] orig = Arrays.copyOf(data, data.length); // make a copy of the data array
13+
System.out.println("arrays equal before sort: "+Arrays.equals(data, orig));
14+
Arrays.sort(data); // sorting the data array (orig is unchanged)
15+
System.out.println("arrays equal after sort: " + Arrays.equals(data, orig));
16+
System.out.println("orig = " + Arrays.toString(orig));
17+
System.out.println("data = " + Arrays.toString(data));
18+
}
19+
}

CaesarCipher/CaesarCipher.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/** Class for doing encryption and decryption using the Caesar Cipher. */
2+
public class CaesarCipher {
3+
protected char[ ] encoder = new char[26]; // Encryption array
4+
protected char[ ] decoder = new char[26]; // Decryption array
5+
/** Constructor that initializes the encryption and decryption arrays */
6+
public CaesarCipher(int rotation) {
7+
for (int k=0; k < 26; k++) {
8+
encoder[k] = (char) ('A' + (k + rotation) % 26);
9+
decoder[k] = (char) ('A' + (k - rotation + 26) % 26);
10+
}
11+
}
12+
/** Returns String representing encrypted message. */
13+
public String encrypt(String message) {
14+
return transform(message, encoder); // use encoder array
15+
}
16+
/** Returns decrypted message given encrypted secret. */
17+
public String decrypt(String secret) {
18+
return transform(secret, decoder); // use decoder array
19+
}
20+
/** Returns transformation of original String using given code. */
21+
private String transform(String original, char[ ] code) {
22+
char[ ] msg = original.toCharArray( );
23+
for (int k=0; k < msg.length; k++)
24+
if (Character.isUpperCase(msg[k])) { // we have a letter to change
25+
int j = msg[k] - 'A'; // will be value from 0 to 25
26+
msg[k] = code[j]; // replace the character
27+
}
28+
return new String(msg);
29+
}
30+
/** Simple main method for testing the Caesar cipher */
31+
public static void main(String[ ] args) {
32+
CaesarCipher cipher = new CaesarCipher(3);
33+
System.out.println("Encryption code = " + new String(cipher.encoder));
34+
System.out.println("Decryption code = " + new String(cipher.decoder));
35+
String message = "THE EAGLE IS IN PLAY; MEET AT JOE'S.";
36+
String coded = cipher.encrypt(message);
37+
System.out.println("Secret: " + coded);
38+
String answer = cipher.decrypt(coded);
39+
System.out.println("Message: " + answer); // should be plaintext again
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
3+
*
4+
* Developed for use with the book:
5+
*
6+
* Data Structures and Algorithms in Java, Sixth Edition
7+
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
8+
* John Wiley & Sons, 2014
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Created-By: 1.7.0_25 (Oracle Corporation)
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
3+
*
4+
* Developed for use with the book:
5+
*
6+
* Data Structures and Algorithms in Java, Sixth Edition
7+
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
8+
* John Wiley & Sons, 2014
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
package dsaj.arrays;
24+
25+
import java.util.Arrays;
26+
27+
/** Class for doing encryption and decryption using the Caesar Cipher. */
28+
public class CaesarCipher {
29+
protected char[] encoder = new char[26]; // Encryption array
30+
protected char[] decoder = new char[26]; // Decryption array
31+
32+
/** Constructor that initializes the encryption and decryption arrays */
33+
public CaesarCipher(int rotation) {
34+
for (int k=0; k < 26; k++) {
35+
encoder[k] = (char) ('A' + (k + rotation) % 26);
36+
decoder[k] = (char) ('A' + (k - rotation + 26) % 26);
37+
}
38+
}
39+
40+
/** Returns String representing encrypted message. */
41+
public String encrypt(String message) {
42+
return transform(message, encoder); // use encoder array
43+
}
44+
45+
/** Returns decrypted message given encrypted secret. */
46+
public String decrypt(String secret) {
47+
return transform(secret, decoder); // use decoder array
48+
}
49+
50+
/** Returns transformation of original String using given code. */
51+
private String transform(String original, char[] code) {
52+
char[] msg = original.toCharArray();
53+
for (int k=0; k < msg.length; k++)
54+
if (Character.isUpperCase(msg[k])) { // we have a letter to change
55+
int j = msg[k] - 'A'; // will be value from 0 to 25
56+
msg[k] = code[j]; // replace the character
57+
}
58+
return new String(msg);
59+
}
60+
61+
/** Simple main method for testing the Caesar cipher */
62+
public static void main(String[] args) {
63+
CaesarCipher cipher = new CaesarCipher(3);
64+
System.out.println("Encryption code = " + new String(cipher.encoder));
65+
System.out.println("Decryption code = " + new String(cipher.decoder));
66+
String message = "THE EAGLE IS IN PLAY; MEET AT JOE'S.";
67+
String coded = cipher.encrypt(message);
68+
System.out.println("Secret: " + coded);
69+
String answer = cipher.decrypt(coded);
70+
System.out.println("Message: " + answer); // should be plaintext again
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
3+
*
4+
* Developed for use with the book:
5+
*
6+
* Data Structures and Algorithms in Java, Sixth Edition
7+
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
8+
* John Wiley & Sons, 2014
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
package dsaj.arrays;
24+
25+
import java.util.Arrays;
26+
27+
public class DeepClone {
28+
29+
public static int[][] deepClone(int[][] original) {
30+
int[][] backup = new int[original.length][]; // create top-level array of arrays
31+
for (int k=0; k < original.length; k++)
32+
backup[k] = original[k].clone(); // copy row k
33+
return backup;
34+
}
35+
36+
public static void main(String[] args) {
37+
int[][] sample = { {1, 2, 3}, {4, 5, 6} };
38+
int[][] backup = deepClone(sample);
39+
40+
System.out.println("Sample:");
41+
System.out.println(Arrays.deepToString(sample));
42+
System.out.println("Backup:");
43+
System.out.println(Arrays.deepToString(backup));
44+
45+
for (int j=0; j < sample.length; j++)
46+
for (int k=0; k < sample[j].length; k++)
47+
sample[j][k] *= 10;
48+
49+
System.out.println("Sample:");
50+
System.out.println(Arrays.deepToString(sample));
51+
System.out.println("Backup:");
52+
System.out.println(Arrays.deepToString(backup));
53+
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
3+
*
4+
* Developed for use with the book:
5+
*
6+
* Data Structures and Algorithms in Java, Sixth Edition
7+
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
8+
* John Wiley & Sons, 2014
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
package dsaj.arrays;
24+
25+
public class GameEntry {
26+
private String name; // name of the person earning this score
27+
private int score; // the score value
28+
/** Constructs a game entry with given parameters.. */
29+
public GameEntry(String n, int s) {
30+
name = n;
31+
score = s;
32+
}
33+
/** Returns the name field. */
34+
public String getName() { return name; }
35+
/** Returns the score field. */
36+
public int getScore() { return score; }
37+
/** Returns a string representation of this entry. */
38+
public String toString() {
39+
return "(" + name + ", " + score + ")";
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
3+
*
4+
* Developed for use with the book:
5+
*
6+
* Data Structures and Algorithms in Java, Sixth Edition
7+
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
8+
* John Wiley & Sons, 2014
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
package dsaj.arrays;
24+
25+
public class InsertionSort {
26+
27+
/** Insertion-sort of an array of characters into nondecreasing order */
28+
public static void insertionSort(char[] data) {
29+
int n = data.length;
30+
for (int k = 1; k < n; k++) { // begin with second character
31+
char cur = data[k]; // time to insert cur=data[k]
32+
int j = k; // find correct index j for cur
33+
while (j > 0 && data[j-1] > cur) { // thus, data[j-1] must go after cur
34+
data[j] = data[j-1]; // slide data[j-1] rightward
35+
j--; // and consider previous j for cur
36+
}
37+
data[j] = cur; // this is the proper place for cur
38+
}
39+
}
40+
41+
public static void main(String[] args) {
42+
char[] a = {'C', 'E', 'B', 'D', 'A', 'I', 'J', 'L', 'K', 'H', 'G', 'F'};
43+
System.out.println(java.util.Arrays.toString(a));
44+
insertionSort(a);
45+
System.out.println(java.util.Arrays.toString(a));
46+
}
47+
}

0 commit comments

Comments
 (0)