Iterative Algorithms Assignment 4: Bubble Sort (Parallel Arrays) Selection Sort Code: For time: (5:23 - 6:00) https://www.youtube.com/watch?v=WTRUBQoAbY8
Question:
Iterative Algorithms Assignment 4: Bubble Sort (Parallel Arrays)
Selection Sort Code:
For time: (5:23 - 6:00)
https://www.youtube.com/watch?v=WTRUBQoAbY8
Insertion Sort Code:
For time: (2:27 - 3:00)
https://youtu.be/_mFBTs-Pf3E
Set Up
1. Make a new main class called IA4_ParallelBubbleSort_LastName.java.
2. Copy an applicable sort method from a previous assignment and then alter it to take in a String Array for
sorting plus a double array and a second String array. It should then sort the first STRING array and
update the other 2 arrays to match.
3. Download the "PeriodicTableData" ZIP folder from D2L, extract it, then place the extracted folder, into a
"Data" folder, in your assignment project folder. (You can find the location of your assignment project
folder by either hovering your cursor over the project, or right-clicking -> Properties -> Sources -> Project
Folder:)
Now for 3 the zipfile (I am not sure how put the zipfile in the question so here, also there 3 different text documents)
How to make a text File: https://www.youtube.com/watch?v=BKvb0_hKELA
This video will help with using the text file(For time start at 10:00):
https://www.youtube.com/watch?v=GIjq9Y7qZeg&list=PLxmkTORwPF14R8E1w2R1g4el8ATG3LMT8&index=7&t=2118s
3 Different Text Files
elementNames
{
Hydrogen
Helium
Lithium
Beryllium
Boron
Carbon
Nitrogen
Oxygen
Fluorine
Neon
Sodium
Magnesium
Aluminum
Silicon
Phosphorus
Sulfur
Chlorine
Argon
Potassium
Calcium
Scandium
Titanium
Vanadium
Chromium
Manganese
Iron
Cobalt
Nickel
Copper
Zinc
Gallium
Germanium
Arsenic
Selenium
Bromine
Krypton
Rubidium
Strontium
Yttrium
Zirconium
Niobium
Molybdenum
Technetium
Ruthenium
Rhodium
Palladium
Silver
Cadmium
Indium
Tin
Antimony
Tellurium
Iodine
Xenon
Cesium
Barium
Lanthanum
Cerium
Praseodymium
Neodymium
Promethium
Samarium
Europium
Gadolinium
Terbium
Dysprosium
Holmium
Erbium
Thulium
Ytterbium
Lutetium
Hafnium
Tantalum
Wolfram
Rhenium
Osmium
Iridium
Platinum
Gold
Mercury
Thallium
Lead
Bismuth
Polonium
Astatine
Radon
Francium
Radium
Actinium
Thorium
Protactinium
Uranium
Neptunium
Plutonium
Americium
Curium
Berkelium
Californium
Einsteinium
Fermium
Mendelevium
Nobelium
Lawrencium
Rutherfordium
Dubnium
Seaborgium
Bohrium
Hassium
Meitnerium
Darmstadtium
Roentgenium
Copernicium
Nihonium
Flerovium
Moscovium
Livermorium
Tennessine
Oganesson
}
elementWeights
{
1.007
4.002
6.941
9.012
10.811
12.011
14.007
15.999
18.998
20.18
22.99
24.305
26.982
28.086
30.974
32.065
35.453
39.948
39.098
40.078
44.956
47.867
50.942
51.996
54.938
55.845
58.933
58.693
63.546
65.38
69.723
72.64
74.922
78.96
79.904
83.798
85.468
87.62
88.906
91.224
92.906
95.96
98
101.07
102.906
106.42
107.868
112.411
114.818
118.71
121.76
127.6
126.904
131.293
132.905
137.327
138.905
140.116
140.908
144.242
145
150.36
151.964
157.25
158.925
162.5
164.93
167.259
168.934
173.054
174.967
178.49
180.948
183.84
186.207
190.23
192.217
195.084
196.967
200.59
204.383
207.2
208.98
210
210
222
223
226
227
232.038
231.036
238.029
237
244
243
247
247
251
252
257
258
259
262
261
262
266
264
267
268
271
272
285
284
289
288
292
295
294
}
elementSymbols
{
H
He
Li
Be
B
C
N
O
F
Ne
Na
Mg
Al
Si
P
S
Cl
Ar
K
Ca
Sc
Ti
V
Cr
Mn
Fe
Co
Ni
Cu
Zn
Ga
Ge
As
Se
Br
Kr
Rb
Sr
Y
Zr
Nb
Mo
Tc
Ru
Rh
Pd
Ag
Cd
In
Sn
Sb
Te
I
Xe
Cs
Ba
La
Ce
Pr
Nd
Pm
Sm
Eu
Gd
Tb
Dy
Ho
Er
Tm
Yb
Lu
Hf
Ta
W
Re
Os
Ir
Pt
Au
Hg
Tl
Pb
Bi
Po
At
Rn
Fr
Ra
Ac
Th
Pa
U
Np
Pu
Am
Cm
Bk
Cf
Es
Fm
Md
No
Lr
Rf
Db
Sg
Bh
Hs
Mt
Ds
Rg
Cn
Nh
Fl
Mc
Lv
Ts
Og
}
Main Method a) Make and load the data for the following arrays, each of which has a corresponding file:
- Names (from the elementNames.txt file) Atomic weights (from the elementWeights.txt file) Symbols (from the elementSymbols.txt file)
You will need to use the loadArr() methods from the Files.java class we used in the Data Structures module from Comp. Sci. 20. You can find the Files class, along with Example 5: Countries in the "Old Examples and Video Lessons from the Comp Sci 20 Data Structures Module" folder on D2L. Feel free to copy the relevant arrays into your program, or just refer to them in the Files class. Finally, make sure your parameters for the methods look like "Data/PeriodicTableData/elementNames.txt". If you use something different, your code won't work properly when Ms. Wiebe marks it. Check with her if you are struggling with this.
b) Print all the data, one array per line, elements separated by commas. c) Sort the data by the names array. d) Print all the data again. e) Ask the user for an element name. f) Run a binary search on the names array and then print the atomic weight and symbol. g) Sort it again, this time by symbol. You should be able to use the same search method and just pass in the parameters in a different order. h) Repeat steps e and f but asking and searching for the symbol this time.
Parallel Bubble Sort Example code:
public class Ex1_BubbleSortParallelArrays {
public static void main(String[] args) {
String[] courses = {"Math", "Social", "Science", "English"};
String[] dates ={"19/06/19", "24/06/19", "21/06/19","20/06/19"};
boolean[] isMorning = {true, true, false, false};
int[] room = new int[4];
room[0] = 202;
room[1] = 134;
room[2] = 220;
room[3] = 142;
sortByRoom(courses,dates,isMorning,room);
for (int i = 0; i < courses.length; i++) {
System.out.println(room[i] +" " + courses[i] + " " + dates[i]);
}
}//end main
public static void sortByRoom(String[] courses,String[] dates,boolean[] isMorning, int[] room ){
boolean swap = true;
while (swap){
swap = false;
for (int i = 0; i < room.length-1; i++) {
if(room[i] > room[i+1]){
int temp = room[i];
room[i] = room[i+1];
room[i+1] = temp;
String temp2 = courses[i];
courses[i] = courses[i+1];
courses[i+1] = temp2;
temp2 = dates[i];
dates[i] = dates[i+1];
dates[i+1] = temp2;
boolean temp3 = isMorning[i];
isMorning[i] = isMorning[i+1];
isMorning[i+1] = temp3;
swap = true;
}
}
}
}//end sortbyRoom
}//end class
This is the code for the Files Class:
// This class has methods for saving and loading arrays from files.
import java.io.*; // imports all of the input/output classes, including PrintWriter,
// FileWriter, BufferedReader, FileReader, etc. They will help us work with the files.
public class Files {
//************** String Array Methods *********************//
public static void saveFile(String filename, String[] array) {
// This method takes the name of the file you will save to and the array
// you will be saving as parameters.
try { // try allows java to try to execute some code, and if an error occurs,
// instead of halting the entire program, you run whatever is in catch.
// The technical term for these errors is Java throwing an exception,
// so the 'catch' code catches the exception and does something with it.
PrintWriter file = new PrintWriter(new FileWriter(filename));
// This line creates a FileWriter object which, as the name suggests,
// allows you to write to a file. However, we use the PrintWriter
// class and the FileWriter object we just created to make a PrintWriter
// object called "file". Basically, this allows us to use regular print
// commands to format what we want to print to put in the file.
for(int i=0; i file.println(array[i]); // print each element to the file. // Note: We usually print to "System.out", the console, but // here we are printing to "file". } file.close(); // Once the data has been sent to the file, close it. } catch (IOException e) { // e is the exception System.out.println(e); // this line just prints the exception to the console } }//end saveFile public static String[] loadStringArr(String filename) { //Note: This is not a void method. We will be returning a String array. //Also, the filename of the file you will load into the array you are // going to return is required. String addLines=""; try { BufferedReader file = new BufferedReader(new FileReader(filename)); // As the name suggests, FileReader is a class that allows you to read // files. BufferedReader just speeds up the reading process. while (file.ready()) { // .ready() just checks to see if the file // has data ready to be read addLines += file.readLine()+","; // The lines from the file are //added to the addLines string, separated by commas. } } catch (IOException e) { System.out.println(e); } return addLines.split(","); // .split(",") takes a string and splits it into // a string array whereever a comma is present. We placed commas between // each line read from the file, so each line in the final is turned into // an element in the array this method returns. }//end loadStringArr //********************** Number Array Methods ******************// public static void saveFile(String filename, int[] array) { try { PrintWriter file = new PrintWriter( new FileWriter(filename) ); for(int i=0; i file.println("" + array[i]); // An empty string is added to each integer element in array // to convert it to a string. It isn't necessary for things to // run, but it probably isn't a bad idea to have. } file.close(); } catch (IOException e) { System.out.println(e); } }//end saveFile public static void saveFile(String filename, double[] array) { try { PrintWriter file = new PrintWriter(new FileWriter(filename)); for (int i = 0; i < array.length; i++) { file.println("" + array[i]); } file.close(); } catch (IOException e) { System.out.println(e); } }//end saveFile public static int[] loadIntArr(String filename) { String addLines = ""; try { BufferedReader file = new BufferedReader(new FileReader(filename)); while (file.ready()) { addLines += file.readLine() + ","; } } catch (IOException e) { System.out.println(e); } String[] tempStringArray = addLines.split(","); // .split() gives us a string array, but we have to convert it to // an integer array. int[] tempIntArray = new int[tempStringArray.length]; // make a temporary // integer array the same length as the temp string array for (int i = 0; i < tempIntArray.length; i++) { tempIntArray[i] = Integer.parseInt(tempStringArray[i]); // Go through each element in the string array, convert it to an // integer, then add it to the integer array. } return tempIntArray; // return your newly made integer array. }//end loadIntArr public static double[] loadDoubleArr(String filename) { String addLines = ""; try { BufferedReader file = new BufferedReader(new FileReader(filename)); while (file.ready()) { addLines += file.readLine() + ","; } } catch (IOException e) { System.out.println(e); } String[] tempStringArray = addLines.split(","); double[] tempDoubleArray = new double[tempStringArray.length]; for (int i = 0; i < tempDoubleArray.length; i++) { tempDoubleArray[i] = Double.parseDouble(tempStringArray[i]); } return tempDoubleArray; }//end loadIntArr }//end Files Class
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang