This lab will focus on creating a better understanding of Selection Sort and Insertion Sort algorithms.
What you need to do
I have provided a driver and a Utility class with three methods. You must finish writing Selection Sort and Insertion Sort in the Utility class. Below is pseudocode for Selection and Insertion Sort, which you may use as a guide. Your selection sort will sort an array of Strings lexicographically, meaning A-Z. Your insertion sort will sort an array of Strings in reverse, meaning Z-A.
Selection Sort Pseudo Code
for i = 0 to length(A) min = i
for j = i to length(A) if A[j] < A[min]
min = j end for
swap A[i] and A[min]
end for
Insertion Sort Pseudo Code
for i = 1 to length(A) j=i
while j > 0 and A[j-1] < A[j] swap A[j] and A[j-1]
j=j-1 end while
end for
How do we compare strings to sort them correctly? Use the method compareTo provided in the string class. This method returns a int value and is used as follows:
<str1>.compareTo(<str2>)
If str1 comes before str2, compareTo will return a negative
number. If str1 comes after str2, compareTo will return a positive
number.
If str1 and str2 are the same, compareTo will return 0.
Driver Output
Unsorted array: Tom, Steve, Ann, Zoe, Bob, Moana, Naomi, Kevin, Ryan, Nina, Dora, Wanda, Eric
Selection Sorted array from A-Z:
Ann, Bob, Dora, Eric, Kevin, Moana, Naomi, Nina, Ryan, Steve, Tom,
Wanda, Zoe
Insertion Sorted array from Z-A:
Zoe, Wanda, Tom, Steve, Ryan, Nina, Naomi, Moana, Kevin, Eric,
Dora, Bob, Ann
Driver.java///////
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method
stub
String[] names = {"Tom",
"Steve","Ann","Zoe","Bob","Moana","Naomi","Kevin","Ryan","Nina","Dora","Wanda","Eric"};
System.out.println("Unsorted
array:");
Utility.printArray(names);
System.out.println();
Utility.selectionSort(names);
System.out.println("Selection
Sorted array from A-Z:");
Utility.printArray(names);
System.out.println();
Utility.insertionSort(names);
System.out.println("Insertion
Sorted array from Z-A:");
Utility.printArray(names);
}
}
Utility.java///////
public class Utility {
public static void selectionSort(String[] array)
{
//TODO - Sort array from A-Z
}
public static void insertionSort(String[] array)
{
//TODO - Sort array from Z-A
}
public static void printArray(String[] array) {
for(int i = 0; i < array.length;
i++) {
System.out.print(array[i]);
if(i !=
array.length -1) {
System.out.print(", ");
}
}
System.out.println();
}
}
In: Computer Science
What method does NOT cause the program to wait until a stream's buffer is empty? (a) flush (b) endl (c) clear (d) close (e) Multiple answers don't pause the program. (f) None of the above cause the program to pause.
I believe that clear is the answer.
flush pauses the program, endl (indirectly calls flush), close (calls flush).
In: Computer Science
General Questions about Network Footprinting
Guidelines
1. What is accomplished by network footprinting?
2. What are the countermeasures a network security architect can set in motion to diminish the threat from network footprinting?
3. What is the difference between network footprinting and
network reconnaissance?
4. In the context of network security, explain what is meant by
after reconnaissance comes penetration.
5. What is a denial of service (DOS) attack?
In: Computer Science
Write a function that will take a string containing only alphanumeric characters that are in lowercase (if you think your logic requires you to use more than one argument, please go ahead). Your task is to see if the string becomes a palindrome if only one character is removed from anywhere in the string.
In: Computer Science
Python:
You will modify the given code to create a guessing game that takes user input and allows you to make multiple guesses. There should also be a loop
Use the following attached Sample Code as a guide.
There are mistakes in the code!!!
#Guessing Game
import random
number=random.randint(1, another number)
print("Hello, CIS 101")
print("Let's play a guessing Game!called guess my number.")
print("The rules are: ")
print("I think of a number and you'll have to guess it.")
guess = random.randint(1, 5)
print("You will have " + str(guess) + " guesses.")
YourNumber = ???
unusedguesses=int(guess)
while unusedguesses < guess:
if int(YourNumber) == number:
print("YAY, You have guessed my number")
else:
unusedguesses=int(guess)-1
if unusedguesses == 0:
break
else:
print("Try again!")
print("The number was ", str(number))
In: Computer Science
Activity 10: Review
Comparison Operators
Comparison operators allow you to test whether values are
equivalent or whether values are identical.
Conditional Code
Sometimes you only want to run a block of code under certain
conditions. Flow control — via if and else blocks — lets you run
code only under certain conditions.
Switch Statement
Rather than using a series of if/else if/else blocks, sometimes it
can be useful to use a switch statement instead. [Definition:
Switch statements look at the value of a variable or expression,
and run different blocks of code depending on the value.]
Loops
Loops let you run a block of code a certain number of times. Note
that in Loops even though we use the keyword var before the
variable name i, this does not “scope” the variable i to the loop
block
For Loop
A for loop is made up of four statements and has the following
structure:
The initialisation statement is executed only once, before the loop
starts. It gives you an opportunity to prepare or declare any
variables.
The conditional statement is executed before each iteration, and
its return value decides whether or not the loop is to continue. If
the conditional statement evaluates to a falsey value then the loop
stops.
The iteration statement is executed at the end of each iteration
and gives you an opportunity to change the state of important
variables. Typically, this will involve incrementing or
decrementing a counter and thus bringing the loop ever closer to
its end.
The loopBody statement is what runs on every iteration. It can
contain anything you want. You’ll typically have multiple
statements that need to be executed and so will wrap them in a
block ( {...}).
While Loop
A while loop is similar to an if statement, except that its body
will keep executing until the condition evaluates to false.
An example for a while loop:
The do-while loop
This is almost exactly the same as the while loop, except for the
fact that the loop’s body is executed at least once before the
condition is tested.
An example for a do-while loop:
Functions
Functions contain blocks of code that need to be executed
repeatedly. Functions can take zero or more arguments, and can
optionally return a value.
Functions can be created in a variety of ways:
Function Declaration
Named Function Expression
A Simple Function
A Function Returns a Value
A Function that Returns another Function
A Self-executing Anonymous Function
A common pattern in JavaScript is the self-executing anonymous
function. This pattern creates a function expression and then
immediately executes the function. This pattern is extremely useful
for cases where you want to avoid polluting the global namespace
with your code — no variables declared inside of the function are
visible outside of it.
Function as Arguments
In JavaScript, functions are “first-class citizens” — they can be
assigned to variables or passed to other
functions as arguments. Passing functions as arguments is an
extremely common idiom in jQuery.
• Passing an anonymous function as an argument
• Passing a named function as an argument
Task 1 – Write web program to calculate a class average mark. It
should allow users to enter one mark at time for 3 times. See
Lesson 10 slides 33-35 for references.
Task 2 – Write web program to calculate a class average mark. It
should allow users to enter one mark at time for as many times as
they wish to do so. It also allows user to stop by entering
-1.
In: Computer Science
1. WRITE 3 ACTIVITIES AT EACH TIME MENTION (MORNING, AFTERNOON / EVENING, NIGHT) THAT YOU DO / SPEND TIME TOGETHER WITH YOUR FRIENDS.
2. DESCRIBE THE ACTIVITIES AT EACH TIME IN 7-10 SENTENCES IN A PARAGRAPH WITH RELEVANT PICTURES.
In: Computer Science
Hi!
I 'm writing a code for a doubly linked list and this is the header file
#include<iostream>
#include <string>
using namespace std;
struct node
{
int data;
node *next,*prev;
node(int d,node *p=0,node *n=0)
{
data=d; prev=p; next=n;
}
};
class list
{
node *head,*tail;
public:
list();
bool is_empty();
int size();
void print();
void search();
int search2(int el);
void add_last(int el);
void add_first(int el);
bool add_pos();
bool delete_first();
bool delete_last();
void delete_pos(int pos);
void delete_el();
void add_sorted();
};
i want to write a function that split the list into 2 list from a specific number
ex. 5 4 7 2 8
if I spilt it from number 4 it will be 5 in the first list and 7 2 8 in the second list and number 4 will go to the List that contains fewer element so first list will have 5 4 and second list 7 2 8 if the have the same number of element the number that you were separated from it goes to any list it does not matter
In: Computer Science
Create an ER diagram, a Relational Schema, and tables with Data, based on the following requirements:
The database will keep track of students and campus organizations.
- For each student, we will keep track of his or her unique student ID, and his or her name and gender.
- For each organization, we will keep track of its unique organization ID and the location.
- Each student in the database belongs to at least one organization and can belong to multiple organizations.
- Each organization in the database has at least one student belonging to it and can have multiple students.
- For every instance of a student belonging to an organization, we will record the student's function in the organization (e.g., president, vice president, treasurer, member, etc.).
In: Computer Science
unix operating system
create the scripts in your Fedora Server Virtual Machine
1- Open the file lab1 located in the labs directory and create a script that prompts the user to enter name, address and phone #. All entered details are directed to file "info.txt" in the same directory
When display info.txt, it should look like this example:
Name: John Smith
Address: 31-10 Thomson Ave.
Phone: 718-482-0000
2- Open the file lab2 located in the labs directory and create a script to display the logged in user name and user UID
When you execute lab2, the output should look like this example:
Username: root
User ID: 0
In: Computer Science
Research cyber kill chains. Is this a legit "game changer" in the IR space? How so?
Give a specific example
In: Computer Science
PROGRAM SIMULATION. Understand the given JAVA program and write the output.
1. public class Places
{
public static void main(String args[])
{
String place[]=new String[4];
place[0]="Salmaniya";
place[1]="Salmabad";
place[2]="Isa Town";
place[3] = “Manama”
System.out.println(place[3]);
System.out.println(place[1].toLowerCase());
System.out.println(place[2].substring(4,6);
System.out.println(place[3].charAt(4));
System.out.println(place[1].equals(place[2]));
}
}
b. public class ChangeIt
{
public void doIt( int[] z )
{
z[0] = 0;
}
}
public class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt(myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
In: Computer Science
In Java, what is the advantage of using ArrayList over arrays?
Write a program to perform following operations
a. Create a class named Rectangle with two
properties “height” and “width”. Create a parameterized
constructor to initialize “height” and “width” values.
b. Write method area() to calculate and return area of
Rectangle.
c. Create an ArrayList to store Rectangle objects.
d. Create three Rectangle objects of width and height set to
(2, 3), (3, 3) and (4, 5) and add them to ArrayList.
e. Calculate and print area of all the Rectangle objects in
ArrayList. (Hint: Area = height x width)
In: Computer Science
Activity 11: JS-Loops and Functions
Task 1:
Use JavaScript for loop to print the first 5 non-zero integers on
each line.
Task 2:
Use JavaScript while loop to print the first 5 non-zero integers on
each line.
Task 3:
Use JavaScript for loop to print the first 5 even integers on each
line.
Task 4:
Write a JavaScript function to multiple two numbers and return the
result.
Task 5:
Write a JavaScript factorial that asks users to enter a positive
and return the result.
i) Without using recursion ii) Using recursion Task 6:
Use a nested for or while loop to produce a table below.
1,1 1,2 1,3 1,4 1,5
2,1 2,2 2,3 2,4 2,5
3,1 3,2 3,3 3,4 3,5
4,1 4,2 4,3 4,4 4,5
5,1 5,2 5,3 5,4 5,5
In: Computer Science
In: Computer Science