Vous êtes sur la page 1sur 3

CALIFORNIA STATE POLYTECHNIC UNIVERSITY

Department of Computer Science

CS 240
N.D.
Lab 1

Before you Begin:


Starting now you need to save all of the projects and homework from this class in your
GITHUB account that you created in HW 1. This will be a requirement for this class as
we will reference HW and LAB assignments in future assignments. I would also
recommend you save your work from other classes to have for future reference.

Implement Algorithms:
In class we covered a few sorting algorithms. Algorithms are a step by step set of
instructions to solve a problem or task. Sorting algorithms are step by step instructions to
arrange data in a particular order. For example in regards to decimal numbers one can
arrange the numbers in ascending, smallest to largest, or descending, largest to smallest.
Hence the two main things to understand are the data in a given data structure will be
compared and if necessary moved to arrange them in a particular order. The number of
times these operations are performed will vary depending on the algorithm and data you
use.

1. Your first task is to implement the algorithms that we learned in class using a
fixed size integer array, size of 10. (THIS IS YOUR DATA STRUCTURE)
a. Using an iterative approach
b. Using a recursive approach

ALGORITHMS:
Selective Sort
Insertion Sort
Shell Sort Note: Use Hibbard Sequence = 1, 3, 7, , (2*previous + 1)]
Merge Sort
Quick Sort
Radix Sort

2. Writing the best algorithm for a particular problem will only be achieved if you
have a good sense of how the algorithms work and scale. Above you worked on
how the algorithm works. Now you will work on becoming more intuitive with
how they scale by graphing the algorithms based on average, best and worst case
scenarios given varying amounts of data.
To help get you started look on page 287 Figure 9-10 and Figure 9-11
You will make 6 separate graphs for each of the sorting algorithms. Each
graph will contain three plots for the Average, Best and Worst Case.
Use three different colors(example Red,Green,Blue) and plot out the data
values, n, listed in Figure 9-11 for each of the Algorithms:

NOTE: Some textbooks, such as ours, will use Big O and state the
case for various cases rather than use the symbol listed below.

a) Average Case (n)


b) Best Case (n)
c) Worst Case O(n).

3. (ITERATIVE APPROACH ONLY)Go back to your algorithms from part 1,


iterative approach only, and create two counters:

int countMove Start at 0. Increment if any data is ever moved.


int countCompare Start at 0. Increment if any data is ever compared.

Add the counters to your implementation and increment when a data item is
moved, countMove, and when data items are compared, countCompare, for
the following cases:

Note: Use data values between 0-999. To help populate your array with
integer values you should use a random number generator such as defined in
class Random.

Example:
import java.util.Random;

public class RandomExample {


public static void main( String args[] ){

// Create a Random object


Random randomObj = new Random();

// x will be a random number between 0-999


// nextInt creates a random number between 0 to (input-1)
int x = randObj.nextInt(1000));
}
}

a) Use Array size 10, run your program and print out the counters
b) Increase Array size to 100, run your program and print out the counters
c) Increase Array size to 1000, run your program and print out the
counters
d) Create 6 Graphs for each of the algorithms and plot countMove and
countCompare with the data values you obtained.
What to Submit:
Part 1) Create a word document with your Name and GITHUB link.
Additionally you should include ONLY the code for part (a) and (b) for
each of the Algorithms

Part 2) In the same word document. Include the graphs for all 6 Algorithms

Part 3) In the same word document. Include the graphs for all 6 Algorithms

Vous aimerez peut-être aussi