CS303 Assignment 3: Algorithms and Data Structures, Assignments of Computer Science

An assignment for a computer science course, focusing on algorithms and data structures. The assignment includes instructions for writing algorithms for sequential search and binary search in a sorted array and binary search tree, analyzing their time complexities, and implementing merge sort and quicksort algorithms. Additionally, students are asked to extend a binary search tree to include a search function and write a program to detect palindromes using a stack.

Typology: Assignments

Pre 2010

Uploaded on 04/12/2010

koofers-user-gj6
koofers-user-gj6 🇺🇸

3

(2)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS303 - Assignment 3
Written Homework:
1. Write an algorithm for:
1. Sequential search in a sorted array: Given a sorted array and a search value, find the
number in the array.
2. Binary Search in a array sorted in ascending order: Given a sorted array and a search
value, find the number in the array.
3. Binary Search in a Binary Search Tree(BST): Given a BST and a search value, find the node
with the value.
2. What is the time complexity of the above algorithms?
3. Draw the BST that results when you insert items with keys A S S I G N M E N T in that order,
into an initially empty tree.
4. From the above generated tree, remove the I and draw the resulting tree.
pf3
pf4
pf5

Partial preview of the text

Download CS303 Assignment 3: Algorithms and Data Structures and more Assignments Computer Science in PDF only on Docsity!

CS303 - Assignment 3

Written Homework:

  1. Write an algorithm for:
    1. Sequential search in a sorted array: Given a sorted array and a search value, find the number in the array.
    2. Binary Search in a array sorted in ascending order: Given a sorted array and a search value, find the number in the array.
    3. Binary Search in a Binary Search Tree(BST): Given a BST and a search value, find the node with the value.
  2. What is the time complexity of the above algorithms?
  3. Draw the BST that results when you insert items with keys A S S I G N M E N T in that order, into an initially empty tree.
  4. From the above generated tree, remove the I and draw the resulting tree.

Programming Problem:

1. Sort

In this programming problem, you are going to implement two general sorting algorithms, Merge sort and Quicksort. The provided skeleton code contains the following files:

  • Sort.java : declares an interface implemented by all sorting algorithms.
  • SortDriver.java : driver program that reads numbers to be sorted from an input file, tests different sorting algorithms and prints some statistics on the screen (correctness, time, etc. ).
  • BubbleSort.java : a class that implements the Sort interface using the Bubble sort algorithm. It serves as a sample for you to implement the other two classes below.
  • MergeSort.java : an unfinished class that implements the Sort interface using the Merge sort algorithm.
  • QuickSort.java : an unfinished class that implements the Sort interface using the Quicksort algorithm. Your task is to implement the sort method for both MergeSort and QuickSort. With the sample input file is provided data50000.txt, and the sample output is: Bubble sort: Length of input: 50000 Correctly sorted: yes Time: 13.828 seconds Merge sort: Length of input: 50000 Correctly sorted: yes Time: 2.125 seconds Quicksort: Length of input: 50000 Correctly sorted: yes Time: 0.016 seconds Note that the actual run time varies on different machines at different time, but your results should not deviate too much from it.

Database size: 10 Query size: 10 Time of linear search: 0.016 seconds Time of binary search: 0.0 seconds Correctness of binary search: 10/ In this case the first 5 queries exist in the database ( true ) and the last 5 not ( false ). The correctness is verified in the following way: the driver program first runs all the queries using sequential search (which is assumed to be correctly implemented) and stores the query results as ground-truth. Then it runs all the queries using binary search, and compares the results from both algorithms. It prints on the console Correctness of binary search: X/Y meaning that for X out of Y queries binary search returns the same results as sequential search (and is therefore correct). Of course, if your binary search is correctly implemented, you should get the message with X = Y. As mentioned before, the advantage of binary search becomes more obvious as N (the size of the database) grows. Below is another run using a database file(database100000.txt) of 100000 integers and a query file (queries100000.txt) of 100000 integers: Database size: 100000 Query size: 100000 Time of linear search: 14.735 seconds Time of binary search: 0.125 seconds Correctness of binary search: 100000/ 2.2. Extend the Binary Search Tree you developed in your last assignment and add search feature: add a function in your class called: Search(int value) This function should return true if found else false. You can read the search value from console. Display the result back in console.

Bonus Problem:

Write a program that detects a palindrome using stack. A palindrome is a word, phrase, number or sequence of units that has the property of reading the same in either direction. For example, ABCBA never odd or even Read input from console and display yes if it is a palindrome, else no. Ignore the white spaces while reading the string in your program.

Submission and grading:

The solution should include:

1. Written Homework

2. Source code (soft copy will be collected in lab). You need to comment your code intensively

and document all the algorithm level decisions and analysis in a separate report (txt file is fine).

Your solution will be graded on both your program and your documentation. The code comment

should be in JavaDoc style: http://java.sun.com/j2se/javadoc/writingdoccomments/.

3. Writing Assignment which should be a free-standing essay describing how you approached the

programming assignment, what you implemented, and what you learned from it.