Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

FUNDAMENTAL OF DATA STRUCTURE CSC248

LAB 1 - SORTING & SEARCHING

QUESTION 1 – SORT
Based on the following program:
import java.util.*;

public class Lab1_Q1


{
//display content of array method
public static void displayArray(Object list[], int size)
{ for(int i=0; i<size; i++)
System.out.print(list[i] + " ");
}

//sorting - bubble sort method


public static void bubbleSort(Object list[], int size)
{ int k,j;
Object temp;

for(k = 0;k <size - 1; k++)


{
System.out.println("***** Round "+ (k+1)+ " *****");
for(j=0; j<size - 1-k; j++)
{

//compare & swap statement


// if(((String)list[j]).compareTo((String)list[j+1])>0)
if((Integer)list[j] > (Integer)list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
displayArray( list, size);//call to display
System.out.println();
}
System.out.println();
}
}

public static void main(String args[])


{
//Declare object scanner
Scanner scan = new Scanner(System.in);

//declare array with general type & variables


Object [] theList = new Object [6];
int i;
int SIZE = theList.length;

// enter value into array


FUNDAMENTAL OF DATA STRUCTURE CSC248

for(i=0; i<SIZE; i++)


{
System.out.print("\nEnter value "+(i+1)+" : ");
theList[i] = scan.nextInt();
}

// Display content of array before sort


System.out.print("\nArray Before Sort : ");
displayArray( theList, SIZE);
System.out.println("\n");

//call method bubbleSort()


bubbleSort(theList, SIZE);

// Display content of array after sort


System.out.print("\nArray After Sort : ");
displayArray( theList, SIZE);

}//end main
}//end class

a) Compile and run the above program. Find the output. Assume the values you are
enter: 19 30 4 8 12 9
b) Change the data type of array (refer to bold statement). Run the program using
the following input values: -
i. papaya banana apple prune orange grape
ii. 13.4 3.65 10.3 11.2 9.4 2.3

QUESTION 2 – SEARCH
Given the following algorithm for Binary Search:
FUNDAMENTAL OF DATA STRUCTURE CSC248

a) Re-type the following program:

b) Write the function definition for the binarySearch() method (refer to the above
algorithm) to find a certain value from user input. Display the index location if the
value is found, otherwise display “Value not in list”.
c) Compile and run the program. Snap the output screen. Assume the user enter
the following values:
i. 7
ii. 15
FUNDAMENTAL OF DATA STRUCTURE CSC248

QUESTION 3 – IO FILE & OBJECT ARRAY


Given the following Student class definition:

Insert the main method below into class Student and write the complete application
program for the following tasks:
a) Write the statement to display all information of the student to the screen output.
b) Write the statement to compare CGPA students and swap CGPA data between
the two elements if the CGPA for the first element is less than the CGPA for the
second element.
c) Write the statement to display student information after sorting (descending
order) and store it in an output file called output.txt.

• The following is the input file for student information: result.txt


FUNDAMENTAL OF DATA STRUCTURE CSC248

Main method

public static void main(String args[])


{
try
{
Student []stud = new Student [15]; //declare array
int i=0; //declare index of array

FileReader fr = new FileReader("result.txt"); //open input file


BufferedReader br = new BufferedReader(fr);

FileWriter wr = new FileWriter("output.txt");//open output file


PrintWriter pw = new PrintWriter(wr);

String name, matric, strLine;


double cgpa;
while ((strLine = br.readLine()) != null) { //read one line from file
StringTokenizer data= new StringTokenizer(strLine,"/");//split data
name = data.nextToken();
matric=data.nextToken();
cgpa = Double.parseDouble(data.nextToken());
stud[i] = new Student(name, matric, cgpa);//store into object array
i++;
}

System.out.println("Before Sort: ");


for(int a=0; a<15; a++)
//a) display student info to screen

System.out.println("\n");

int j, k, SIZE = 15;


Student temp = new Student(null,null,0.0); //declare temp object
//Sorting algorithm
for (k=0; k<SIZE-1; k++)
{
// after one pass, the data in num[SIZE-1-k] would be
// in it’s proper position
for (j=0; j<SIZE-1-k; j++)
{
//b) compare cgpa and swap data
}
}

System.out.println("After Sort: ");


for(int a=0; a<15; a++)
{System.out.println(stud[a] ); //display to screen
//c) store student data into an output file
}
br.close();
pw.close();

catch (Exception e)
{//Catch exception if any
System.err.println("\nError: " + e.getMessage());
}
FUNDAMENTAL OF DATA STRUCTURE CSC248

finally
{
System.out.println("\nThank you.");
}
}//end main

You might also like