Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Sorting and Searching Assignment for Computer Science I (CIS 201), Assignments of Computer Science

Instructions for assignment 5 of computer science i (cis 201) at a university level. Students are required to examine various aspects of sorting and searching by modifying the given java code. The assignment covers topics such as parameter names, inline code, global arrays, and sorting arraylists. Students are also expected to implement insertion sort and search methods. Instructions for each task, as well as examples and explanations.

Typology: Assignments

Pre 2010

Uploaded on 08/09/2009

koofers-user-xld-1
koofers-user-xld-1 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CIS 201 Computer Science I
Assignment 5: Sorting and Searching
Due Tuesday, April 21
Introduction
In this assignment you will examine various aspects of sorting and searching.
Parameter names
Copy the file
/home/student/Classes/201/Assignments/05/Sort.java
into a working directory for this assignment. You will be making modifications to this file and
submitting the modifications as described below.
First, compile and run this file to be sure that you understand its purpose and how it behaves.
Make a copy of the
Sort.java
file named
Sort1.java
. You can do this using the following
command:
cp Sort.java Sort1.java
You can also do this inside
emacs
by editing
Sort.java
and then using the
Save Buffer As...
menu choice. Edit your
Sort1.java
file so that the names of the class is
Sort1
instead of
Sort
.
Examine the
Sort1.java
file in your favorite editor, paying particular attention to the names of
the formal parameters in the methods
findIndexOfSmallest
,
swap
,
sort
, and
display
.
As you know, formal parameter names can be anything you like, but they should be descriptive, if
possible so a formal parameter name
a
is a reasonable candidate for a parameter that refers to an
array.
However, in this assignment we will break this rule by using different formal parameter names for
the methods. In particular, your task is to replace the method signatures in the file
Sort1.java
to
read as follows:
public static int findIndexOfSmallest(int [] j, int a)
1
pf3
pf4
pf5

Partial preview of the text

Download Sorting and Searching Assignment for Computer Science I (CIS 201) and more Assignments Computer Science in PDF only on Docsity!

CIS 201 – Computer Science I

Assignment 5: Sorting and Searching

Due Tuesday, April 21

Introduction

In this assignment you will examine various aspects of sorting and searching.

Parameter names

Copy the file

/home/student/Classes/201/Assignments/05/Sort.java

into a working directory for this assignment. You will be making modifications to this file and submitting the modifications as described below.

First, compile and run this file to be sure that you understand its purpose and how it behaves.

Make a copy of the Sort.java file named Sort1.java. You can do this using the following command:

cp Sort.java Sort1.java

You can also do this inside emacs by editing Sort.java and then using the Save Buffer As... menu choice. Edit your Sort1.java file so that the names of the class is Sort1 instead of Sort.

Examine the Sort1.java file in your favorite editor, paying particular attention to the names of the formal parameters in the methods findIndexOfSmallest, swap, sort, and display.

As you know, formal parameter names can be anything you like, but they should be descriptive, if possible – so a formal parameter name a is a reasonable candidate for a parameter that refers to an array.

However, in this assignment we will break this rule by using different formal parameter names for the methods. In particular, your task is to replace the method signatures in the file Sort1.java to read as follows:

public static int findIndexOfSmallest(int [] j, int a)

public static void swap(int [] tmp, int a, int i) public static void sort(int [] j) public static void display(int [] i)

and to modify the corresponding method bodies to use these formal parameter names in place of the ones originally in the file. Of course, if the formal parameter names clash with any of the local variables in the methods, you may need to changes those names as well.

Compile and run your program to be sure that it behaves exactly the same as the original Sort.java program.

Submit your Sort1.java program into moodle.

Inline code

Make a copy of your Sort.java file named Sort2.java and examine this file using your favorite editor. Modify the class name to Sort2.

Notice that the sort method makes calls to the methods findIndexOfSmallest and swap. Your task is to replace each of these method calls by replacing the method call with the actual code from the method. For example, here is the original code for the sort method from Sort.java:

public static void sort(int [] a) { int len = a.length; for (int i=0 ; i<len ; i++) { int j = findIndexOfSmallest(a, i); swap(a, i, j); } }

By replacing the call to the method swap(a, i, j) with the actual code for swap, we get the following:

public static void sort(int [] a) { int len = a.length; for (int i=0 ; i<len ; i++) { int j = findIndexOfSmallest(a, i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } }

Compile and run this program to be sure that it works as before. In this case, the only difference is that the ia array is a class variable known to the entire class rather than a local variable known only to the main method – but the program will still run correctly.

Now, instead of passing an array as an actual parameter to each of the methods findIndexOfSmallest, swap, sort, and display, these three methods can simply refer to the class variable ia instead of the formal array parameter. So make the changes to these four methods by removing the array formal parameter and referring instead in the body of each of these methods to the class variable ia. These changes will alter the method signatures, so where these methods are called – for example, in sort and main – be sure to call these methods with the proper actual parameters.

Compile and run this program to be sure that it works the same as the original Sort program.

Submit Sort3.java in moodle.

Sorting ArrayList s

Make a copy of Sort.java named Sort4.java, and change the name of the class in this file to Sort4. Then replace all of the integer arrays in this program to be of the form ArrayList, and make all modifications necessary for the program to sort this type of aggregation.

Since an ArrayList is created empty in your main method in Sort4.java, you will need to add random elements to it to make its size equal to n. This is different from the integer array used in Sort.java where the length of the array was defined to be n.

Compile and run this program to be sure that it works the same as the original Sort program.

Submit Sort4.java in moodle.

Insertion sort

The selection sort – as implemented in Sort.java – works fine if you have all of the items to be sorted already in the array. But what if you have n elements in an ArrayList that are already sorted, and you want to add a new element to the array? Since the new element might now be the smallest element, it will need to be moved to the beginning of the array – if you are stuck with selection sort, you would need to re-sort the entire array to do so.

This is where insertion sort comes in handy. The idea behind insertion sort is to assume that the first n elements of the array are already sorted. (Clearly this would be the case when n is zero or 1!) If you add an n + 1st element to the array, you find the location in the first n slots where this new

element should go to keep it sorted, and move all of the elements up in the array to make room for the new element. The resulting array will have the first n +1 elements sorted. This process is dandy if you are adding elements to an aggregate such as an ArrayList which can grow dynamically.

Here’s how a single insertion works. Assume that you have an array a of n + 1 integers in locations a [ 1 ], a [ 2 ], · · · , a [ n + 1 ], with the integers a [ 1 ], a [ 2 ], · · · , a [ n ] in sorted order, and assume that you want to insert element a [ n + 1 ] in its proper position in the array a [ 1 ], a [ 2 ], · · · , a [ n ], a [ n + 1 ] so that this n + 1 element array is sorted:

  1. Set t = a [ n + 1 ].
  2. While 0 < n and a [ n ] > t , assign a [ n + 1 ] = a [ n ] and then n = n − 1.
  3. Set a [ n + 1 ] = t.

Basically, the variable n moves down from the top of the array, and items larger than t (the element to be inserted) move upwards in the array until a [ n ] <= t , in which case we know that t must follow a [ n ] in the array – in other words, a [ n + 1 ] must be set equal to t.

For an ArrayList, here is a method signature that would serve to capture the idea suggested above:

// Precondition: a is an Integer ArrayList, with elements // in positions 0 through n-1 in (nondecreasing) sorted order // and with some Integer value in position n // Postcondition: the elements in positions 0 through n are // in (nondecreasing) sorted order public static void insert(ArrayList a, int n)

Make a copy of Sort4.java named Sort5.java. Change the name of this class to Sort5.

Add the method insert to your class as documented above, and fill in the body of this method as described in the single insertion algorithm. Since the ArrayList elements are numbered starting at zero instead of 1, you will need to make appropriate changes to accommodate this. Also, since you are dealing with ArrayLists instead of arrays, you will need to change expressions such as a [ p ] into a.get(p) and assignments such as a [ p ] = q into a.set(p, q).

Now re-write your sort method as follows:

public static void sort(ArrayList a) { int len = a.size(); for (int n=1 ; n<len ; n++) insert(a, n); }

break; int t = in.nextInt(); System.out.print(t); int p = search(ia, t); if (p < 0) System.out.println(" is not present"); else System.out.println(" is located at position " + p); }

You will need to import java.util.Scanner to include the Scanner class.

To Complete this assignment, implement the search method, which will have the following doc- umentation and signature:

// Precondition: a is an ArrayList of Integers, t is a target integer // Returns: the position in a where t occurs, or - // if t does not occur in the array public static int search(ArrayList a, int t)

You can use either sequential search (easiest) or binary search. If you use binary search, you will get full credit; for sequential search, you will get 95% credit.

Compile and run this program. Be sure that the program reports the correct position in the array when a target integer is found, and that the program reports that any other integer is not present.

Submit your Sort7.java program in moodle.