Algorithm analysis and design and implementation in java

assignment_data_structure.docx

Unformatted Attachment Preview

Don't use plagiarized sources. Get Your Custom Essay on

Algorithm analysis and design and implementation in java, Computer Science Assignment Homework Help

Just from $10/Page

Q1: design, analyse and write code/s to implement sort algorithms:

(1) Bubble sort algorithm and analysis.

Bubble-sort is one of the simplest elementary array-sorting algorithms. The principle of it is as

below: For a given array of n (comparable) elements, the algorithm scans the array n-1 times,

where, in each scan, it compares the current element with the next one and swaps them if they are

not in the required order (e.g., in ascending order, as usual). Based on this principle, you are

required to – Write the bubble sort algorithm (in pseudo code), which takes an array as a parameter;

– Analyse your algorithm using O-notation, i.e., by counting the number of comparisons (and also by

counting the number copying operation); – Convert your algorithm to a Java code, and test your code

using an array of some 20 elements.

(2) BST-based sort algorithm and analysis

For a given array of n (comparable) elements, the BST-based sort algorithm consists of two steps: a)

Insert all array elements into an (initially) empty BST; b) Return the in-order traversal sequence (as

the sorting result).

You are required to

c) Write pseudo code to implement the BST-based sort algorithm, which takes an array a parameter;

– Analyse your algorithm using O-notation. – Referencing to the codes for BST related algorithms,

convert your algorithm to a Java code, and test your code using an array of some 20 elements.

Q2: Theoretical summary of sort algorithm complexities

Review all arrays-based sorting algorithms that you have learnt so far (including those you’ve done

in Q1), and fill in the missed complexity related data in Table 1, which has been partially completed

(note the complexity data shown in Table 1 is of Average case unless noted otherwise). While no

detailed algorithm analysis is required for this question, you are requested to fill in as detailed as you

can.

1

Q3: Algorithm analysis by experimental studies

Produce Java code/s to

(a) Randomly generate an array, A, of size n (for n = 200, 400, 800, 1000, 2000); and

(b) Sort the array A using array-based sorting algorithms/strategies – you are requested to modify

the related array sorting algorithms such that they not only sort the array, but also count the total

number of comparisons and, in addition, record the running time (say, in ms) for each run.

Collect data from the above running outputs and complete the following Table 2 and Table 3.

To assist you prepare and achieve the solution/s, the following steps are recommended:

(1) Write Java codes to implement the following array-based algorithms that can be used to sort an

array of n integers:

2

a) Bubble sort

b) Selection sort

c) Insertion sort

d) Merge sort

e) Quick sort

f) BST-based sort

(Note that most of those array-sorting algorithms have been implemented and tested in the lab

sessions or implemented in Q1).

(2) For each of the above codes, make necessary modification on it so that it not only sorts the

array, but also counts/outputs the number of comparisons, and records running time, for each run;

(3) Write Java code that, for a given number of n,

i). randomly generates an array A of n integers; and

ii). sorts the array A, using Bubble sort (Selection sort, Insertion sort, Merge sort, Quick sort, BSTbased sort, respectively), and outputs the required data (i.e., number of comparisons and running

time);

(4) For n = 200, run the Java codes completed in step (3), and collect the output data in this case.

(5) Repeat step (4) for 10 times (i.e., 10 runs), and calculate the average number of comparisons,

and average running time, over 10 runs. Fill the average number (of comparisons over 10 runs) in

the first column of Table 2, and fill the average running time (of sorting the array over 10 runs) in the

first column of Table 3;

(6) Repeat step (5) for n = 400, 800, 1000 and 2000, and fill in the rest columns of Table 2 and Table

3, respectively (Note: For comparison purpose, make sure you use the exact same array data for all 6

sorting algorithms/codes).

Q4: Sequencing array sorting algorithms based on their

complexities

Based on the tasks completed in Q2 and Q3,

(1) Compare data from Table 1, Table 2 and Table 3, and make a summary/comment/s on whatever

you find from the algorithm analysis activity (i.e., the relationship between theoretical complexity

analysis and that by experimental studies);

(2) Put these sorting algorithms (i.e., Bubble, Selection, Insertion, Merge, Quick, and BST-based sort)

in a sequence based on their complexities (e.g., the algorithm with the best complexity is put to the

first place and that with highest complexity to the last).

If you were given a task that needs to sort an array, which of the above algorithm/strategy may you

prefer to do the sorting? Explain your reason/s

Q5: Linked-list Programming: SLL deletion and reverse operations

A unit_list class is to be given in Workshop 08. The class uses an SLL to represent a list of student

assessment results of a unit. Each SLL node contains a record of one student’s assessment results.

3

That is, it stores a student ID followed by the marks of three assessment components (A1_mark,

A2_mark, and exam_mark), all are in integers. For convenience, student information stored in the

SLL is in ascending order by the student_ID field. The class given shows the technique of traversing

an SLL, searching an SLL and inserting a node into the SLL.

Your Task:

Starting from the unit_list class given, you are requested to expand the class by implementing the

following two Java methods:

(1) Deletion of a student’s record:

When being given a student ID (as input), the function/method searches the SLL to see if the student

with that ID existed in the SLL or not. If it is existed, the function/method deletes the student record

(i.e., the node in the SLL) and keeps all other student information in the SLL unchanged.

Note that the method requires no return (i.e., void return as in Java). As such, you should make sure

that the first node of the SLL is not physically deleted from the SLL, even if it might be logically

deleted (why?).

(2) Display/print the student information in descending order on the Student ID field:

This method displays/prints all student information(ID, individual mark items and total) in

descending order on student ID field. Please note that, to keep the unit_class work properly you

should not destroy the original SLL (why?). To do this, you may create a new SLL which is the reverse

of the original SLL (or if you have to destroy the original SLL, make sure your recover it afterwards).

(3) Analyse the above methods using O-notation.

The code is :

/**

* It demonstrates how to build an SLL, which in this example represents a list of semester

* results of a unit. Each SLL node contains a record of one student’s results of the semester,

* that is, it stores a student ID followed by marks of three assessment components

* (A1_result, A2_result, and exam_result). For simplicity, all marks are integers. It also

* shows the technique of traversing an SLL, searching an SLL and inserting a node into the SLL.

* Hope this helps understanding of the linked list data structure and concepts and operations,

etc..

* (Code tested on BlueJ)

*

*

* @version v2

*/

public class unit_list

4

{

private int student_ID;

//of 4 digits

private int A1_result;

//0<= a1_mark<=20
private int A2_result;
//0<= a2_mark<=30
private int exam_result; //0<= a3_mark<=50
private unit_list next =null;
private unit_list(int ID, int mark1, int mark2, int mark3)
{ if ((ID<999)||(ID>9999)) return;

if ((mark1 <0.0)||(mark1>20.0)) return;

if ((mark2 <0.0)||(mark2>30.0)) return;

if ((mark3 <0.0)||(mark1>50.0)) return;

this.student_ID=ID;

this.A1_result=mark1;

this.A2_result=mark2;

this.exam_result=mark3;

}

private static void highest_result(unit_list u_list)

{ // serach student with hishest overall result, of mark1+mark2+mark3

if (u_list== null) return;

unit_list highest_mark = u_list;

for (unit_list curr= u_list.next; curr!= null; curr = curr.next)

if (curr.A1_result + curr.A2_result+curr.exam_result >

highest_mark.A1_result + highest_mark.A2_result+highest_mark.exam_result)

highest_mark = curr;

System.out.println(“nStudent with highest overall results is the one with Student_ID.:

“+highest_mark.student_ID);

}

private static void print_unit_result(unit_list u_list)

{if (u_list == null) return;

System.out.println(“Current linked list contains:”);

5

for (unit_list curr= u_list; curr!= null; curr = curr.next)

System.out.println(” Student_ID.: “+curr.student_ID +

“t A1: “+curr.A1_result +

“t A2: “+curr.A2_result +

“t Exam: “+curr.exam_result);

System.out.println();

}

/**************************INSERT*****************/

private static void insert_unit_result(unit_list u_list, int ID, int mark1, int mark2, int mark3)

{

unit_list new_node = new unit_list(ID, mark1, mark2, mark3);

// if empty list, insert as the only node

if (u_list == null)

return;

// cannot insert anyway due to Void return – so we assume unit_list != null

// For convenience,student records are listed in ascending order by the student_ID field.

unit_list previous = null;

unit_list curr = u_list;

while (curr!=null)

//traverse the SLL

{ if (curr.student_ID >=ID) break;

//insert here??

previous=curr;

curr=curr.next;

};

if (curr==null) /* insert as the last */

{previous.next=new_node;

return;

}

6

if (curr.student_ID ==ID)

// ID match, replace the unit marks

{curr.A1_result = mark1;

curr.A2_result = mark2;

curr.exam_result = mark3;

return;

}

if (previous==null) /*the new node to be inserted at the beginning */

{new_node.next=u_list;

// due to void return, changing unit_list link would not work

unit_list temp = new unit_list (0, 0, 0,0);

temp.student_ID =u_list.student_ID;

temp.A1_result = u_list.A1_result;

temp.A2_result = u_list.A2_result;

temp.exam_result = u_list.exam_result;

temp.next=u_list.next;

u_list.student_ID =ID;

u_list.A1_result = mark1;

u_list.A2_result = mark2;

u_list.exam_result = mark3;

u_list.next = temp;

return;

}

// Otherwise i.e., curr.ID >ID and Previous!=null

new_node.next=curr;

previous.next=new_node;

return;

}

7

public static void main(String[] args)

{int[] unit1 = {1111, 17, 22, 30,

1112, 10, 6, 50,

1114, 14, 21, 30,

1116, 8, 16, 35,

1122, 11, 19, 40,

1145, 9, 16, 20,

1189, 20, 30, 50};

//build a link of a unit result

//first unit node

unit_list u_list = new unit_list( unit1[0], unit1[1], unit1[2], unit1[3]);

unit_list curr = u_list;

for (int i=1; i<=6; i++) // to build the rest of the list
{unit_list one_node = new unit_list(unit1[i*4], //student_ID
unit1[i*4+1], //a1_mark
unit1[i*4+2], //a2_mark
unit1[i*4+3] //exam_mark
);
curr.next = one_node;
curr= curr.next;
}
//print out the student results of unit 1
print_unit_result (u_list);
//fing hishest performance student
highest_result(u_list);
System.out.println("nInsert a new record: ID: 1225, A1:17, A2 20, Exam: 20 n");
8
insert_unit_result(u_list, 1225, 17, 20, 20);
print_unit_result (u_list);
}
}
9
...
Purchase answer to see full
attachment

The price is based on these factors:

Academic level

Number of pages

Urgency

Basic features

- Free title page and bibliography
- Unlimited revisions
- Plagiarism-free guarantee
- Money-back guarantee
- 24/7 support

On-demand options

- Writer’s samples
- Part-by-part delivery
- Overnight delivery
- Copies of used sources
- Expert Proofreading

Paper format

- 275 words per page
- 12 pt Arial/Times New Roman
- Double line spacing
- Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Delivering a high-quality product at a reasonable price is not enough anymore.

That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more