0% found this document useful (0 votes)
3 views

Assignment 03

The document outlines an assignment involving Java programming tasks related to data structures and algorithms. It includes creating a program to check for balanced parentheses using a stack, implementing a circular queue with specific operations, and developing sorting and searching algorithms. The assignment requires the implementation of various sorting methods and search algorithms, along with user interaction for input and output.

Uploaded by

Abdul Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 03

The document outlines an assignment involving Java programming tasks related to data structures and algorithms. It includes creating a program to check for balanced parentheses using a stack, implementing a circular queue with specific operations, and developing sorting and searching algorithms. The assignment requires the implementation of various sorting methods and search algorithms, along with user interaction for input and output.

Uploaded by

Abdul Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 03

Stacks and Queues


Question 1 - Part A
Write a Java program that checks if a given string containing parentheses (, ), {, }, [, and ] is
balanced. A string is considered balanced if every opening parenthesis has a corresponding
closing parenthesis in the correct order.

Requirements:

1. Use a stack to solve this problem.


2. The program should take a string as input and return true if the string is balanced,
otherwise false.

Sample Input and Output:

Input: {[()()]} → Output: true


Input: {[(])} → Output: false

Question 1 - Part B
Implement a circular queue in Java using an array. Your circular queue should support the
following operations:

1. enqueue(int value): Adds an element to the rear of the queue. If the queue is full, display
an appropriate message.
2. dequeue(): Removes an element from the front of the queue. If the queue is empty,
display an appropriate message.
3. peek(): Returns the front element without removing it.
4. isEmpty(): Checks if the queue is empty.
5. isFull(): Checks if the queue is full.

Requirements:

1. Use an array to implement the circular queue.


2. Use modular arithmetic to handle the wrap-around when adding/removing elements.
3. Demonstrate the queue by enqueuing and dequeuing elements in a sequence, showing
the circular behavior.

Sorting and searching algorithms


Question 2 - Part A
Write a Java program that takes an unsorted array of integers and sorts it using five different
sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, Shell sort and Quick sort. The
program should include separate methods for each sorting algorithm. After sorting the array with
each method, print the sorted array.

Requirements:

Implement five methods for sorting:


1. void bubbleSort(int[] arr): Sorts the array using the Bubble Sort algorithm.
2. void selectionSort(int[] arr): Sorts the array using the Selection Sort algorithm.
3. void insertionSort(int[] arr): Sorts the array using the Insertion Sort algorithm.
4. void shellSort(int[] arr): Sorts the array using the Shell Sort algorithm.
5. void QuickSort(int[] arr): Sorts the array using the Quick Sort algorithm.

In the main method:


1. Prompt the user to enter the array size and the elements.
2. Create a copy of the array for each sorting method to ensure all methods work on the
same initial data.
3. Print the sorted array from each algorithm.

Question 2 - Part B
Write a Java program that allows a user to search for an integer in an array using search
algorithms ‘Linear Search’ and ‘Binary Search’. The program should contain separate methods
for each search algorithm and print the position of the integer if it is found or indicate if it is not
found.
Requirements:

Implement two methods for searching:


1. int linearSearch(int[] arr, int target): Searches for the target element in the array using
Linear Search and returns its index if found, or -1 if it is not found.
2. int binarySearch(int[] arr, int target): Searches for the target element in a sorted array
using Binary Search and returns its index if found, or -1 if it is not found.

In the main method:


1. Prompt the user to enter the array size and its elements.
2. Prompt the user to enter the target element to search for.
3. Sort the array before performing Binary Search (You can use a sorting algorithm of your
choice).
4. Perform both Linear Search and Binary Search on the target element and display the
results.

You might also like