Copyright  © – Sudharsan Iyengar

 

CS – 341         Lab 3               10/10/24 & 10/15/24

 

Sorting and Debugging Java programs

 

A bug in a Java program is a logical error. The term “bug” came about when a moth was found by Grace Hopper – embedded in a punch card – which in turn was causing program errors when the cards were passed through the card readers. This issue was presented at a conference and since then the term has gained place in the annals in the history of computers.

 

Logical errors are errors not so obvious. From the compilers perspective the program is syntactically and semantically correct. During execution though the program crashes or yields unexpected output.

 

Developers/programmers have to methodically go through a program and trace the execution for various input data cases. The identification of a software ‘bug’ is a tricky and time consuming process. Sometimes the logical error is a result of the effects of multiple program segments other than yours.

 

One method of identifying the cause of error is to place trace output statements that will output messages and relevant values at strategic locations. This allows the programmer to view the progression of computation as the program executes.

 

This process is cumbersome and relies on programmers’ ability to place proper trace outputs. In any event this process is repeated multiple times until the cause is identified. The result is a code littered with these statements.

 

Today’s IDEs (Integrated Development Environments) like Eclipse provides debuggers that allow dynamic control of the execution of the programs by the developer. Additionally they allow visualizing the dynamic data elements without the use of print statements.

 

When you want to debug the programs, the programs are executed under the debug mode. Prior to execution though you set controls at different points in your code. The code is not modified but execution is enabled through an interactive interface. Some of the simple controls are given below.

 

Elements of debugging:

 

Setting Breakpoints:                            Stopping execution during execution at certain points in the source code.

 

Starting a Debugging Session:            Program execution in the debug mode.

 

Controlling Program Execution:         Stepping over a statement

Stepping into a method

Stepping after a method

Resuming normal execution

Terminating Execution.

 

Examining:                                          Viewing data members of objects

Manipulating data members

Evaluating expressions

Viewing method invocations

 

Viewing output:                                  Viewing Console Output

 

Write a Java class Sorter that contains methods for (in-place) Selection sort, Bubble sort, and Insertion sort – given an array of integers. The methods return the number of comparisons done during the sort.

 

The main class creates an object of type Sorter and reads the input file “Input.dat” which contains one line of numbers (positive and negative). The sorting methods are called from the main. Output the array, the sorted array, and the number of comparisons done after each of the different sorts.

 

Modify the sorter methods so they will return the number of swaps. Again call the sorting methods are called from the main. Output the array, the sorted array, and the number of swaps done after each of the different sorts.

 

Is there a way you can return both - the number of comparisons and the number of swaps - from the sorting method?

 

Use the debugger features to set break points before and after reading the input file, within the sorting methods, and iterations in the methods, break point and after a swap, and as you see necessary.  Inspect the elements and observer the changes in the data array.

 

10/15/2024

 

Add methods to do linear search and binary search. The method should take in an array of integers and a value. It should return the position where the value is found in the array. If not found – then it should return -1.

 

Write an application that

 

·         Will generate 8192 random numbers in the range [1-500,000] and store it in an array

·         Copy these values to corresponding locations into another array and sort it (using any of the sorting methods we have).

·         Find the time needed to do linear search all these values; compare it to the time needed to do binary search for the same numbers.