0% found this document useful (0 votes)
101 views7 pages

TPL Assignment 1 New

The document describes bubble sort algorithms implemented in Python and Java. It discusses reading and writing files, data types used, runtimes of each program, and differences between the languages. The Python program took around 28 minutes to bubble sort 100,000 numbers while the Java program took approximately 7 minutes, showing Java had better performance. Overall, the document compares and contrasts implementing bubble sort in the two languages.

Uploaded by

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

TPL Assignment 1 New

The document describes bubble sort algorithms implemented in Python and Java. It discusses reading and writing files, data types used, runtimes of each program, and differences between the languages. The Python program took around 28 minutes to bubble sort 100,000 numbers while the Java program took approximately 7 minutes, showing Java had better performance. Overall, the document compares and contrasts implementing bubble sort in the two languages.

Uploaded by

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

SHORT QUESTIONS

1. How can user defined operator overloading harm the readability of a


program?
Ans: Operators can be used for various different operations; therefore,
overloading operators can cause confusing to the real user while writing a
program.

2. What are the advantages in implementing a language with a pure


interpreter?
Ans: Pure interpreter allows for implementation of many source level debugging
operations, because many run-time error messages can refer to source level
units.

3. What is one example of a lack of orthogonality in the design of C?


Ans: An array element can be any data type (except void).

4. Why is type checking the parameters of a subprogram important?


Ans: Type checking the parameters of a subprogram helps reduce program errors
and increases the readability of program. It is important especially in
mathematical applications where changing between int, float, and double
could cause errors.

5. What is the name of the category of programming languages whose


structure is dictated by von Neumann computer architecture?
Ans: Imperative

6. What does it mean for a program to be reliable?


Ans: If program performs according to its specifications under all conditions,
then it is said to be reliable.
7. Why is the von Neumann bottleneck important?
Ans: The von Neumann bottleneck is the primary limiting factor in the speed of
the von Neumann architecture machines.

8. What two programming language deficiencies were discovered as a


result of the research in software development in the 1970s?
Ans: Type checking and control statements

9. What are the three fundamental features of an object-oriented


programming language?
Ans: Abstraction, Inheritance and Polymorphism

10. What language was the first to support the three fundamental features
of object-oriented programming?
Ans: Smalltalk

11. What is the disadvantage of having too many features in a language?


Ans: Too many features reduce the readability, write-ability of the language and
increases complexity and cost of supporting.

12. What are three general methods of implementing a programming


language?
Ans: Compilation, Pure Interpretation and Hybrid Implementation System.

13. Which produces faster program execution, a compiler or a pure


interpreter?
Ans: Compiler
14. What role does the symbol table play in a compiler?
Ans: Symbol table is an important data structure created and maintained by
compilers in order to store information about the occurrence of various
entities such as variable names, function names, objects, classes, interfaces,
etc. Symbol table is used by both the analysis and the synthesis parts of a
compiler.

15. What does a linker do?


Ans: It converts one or more object files to single executable file, library file, or
another "object" file.

BUBBLE SORT PROGRAM

1. Python:
def bubblesort(mylist):
for i in range (0, len(mylist) - 1):
for j in range (0 , len(mylist) - 1 - i):
if mylist[j] > mylist[j+1]:
mylist[j], mylist[j+1] = mylist[j+1], mylist[j]
return mylist

def read():
#read from file
f = open("inputlakhNumbers.txt","r")
data = f.read().split()# convert string to list by split
f.close()
# for each element in list convert to int
for i in range(len(data)):
data[i] = int(data[i])
return data

def write(thelist):
#create edit the text file
file = open("numbers.txt", "wt")
# for each element in list write i value and add space between new value
for i in range(len(thelist)):
file.write(str(thelist[i]) + " ")
file.close()

thelist = read()
print(bubblesort(thelist))
write(thelist)

2. Java
package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Test {

public static int[] bubbleSort(int[] num ) {


int j;
boolean flag = true; // set flag to true to begin first pass
int temp; //holding variable

while ( flag ) {
flag= false; //set flag to false awaiting a possible swap
for( j=0; j < num.length -1; j++ ) {
if ( num[ j ] < num[j+1] ) {
temp = num[ j ]; //swap elements
num[ j ] = num[ j+1 ];
num[ j+1 ] = temp;
flag = true; //shows a swap occurred
}
}
}
}

public static void write (String filename, int[]x) throws IOException{


BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter(filename));
for (int i = 0; i < x.length; i++) {
outputWriter.write(x[i]+"");
}
outputWriter.flush();
outputWriter.close();
}
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("inputlakhNumbers.txt.txt"));
int [] numbers = new int [100000];
int [] Sortednumbers = new int [100000];
int i = 0;
while(scanner.hasNextInt()){
numbers[i++] = scanner.nextInt();
}

Sortednumbers = bubbleSort(numbers);

write("numbers.txt",Sortednumbers);

}
}
Report

Bubble sort algorithm is implemented in two languages, namely, Python and


Java. I do have the basic knowledge of both languages before implementation.
For me it was difficult to learn Python because I learned it on my own and never
any class for it. For Java, I learned in my graduate degree program and it was not
difficult for me to learn Java.
While writing bubble sort program, I do encountered errors but it did not take
much time to correct them as every problem’s solution was available on internet.
I learned my mistakes from internet and corrected my code accordingly.
I did not encounter any run time but while compiling code in Python, it took a lot
of time, so at first, I thought there was any issue but then it all went well.
Writing program in Python was much easier than in Java because of less line of
code in Python.
I used an integer datatype array to store numbers in both languages and a string
variable to store filename where sorted list of numbers was saved. Both languages
have built-in support for these datatypes.
I did not used static/heap dynamic or fixed heap dynamic array in both languages.
I only used static arrays whose size was known and whose storage was known at
compile time.
Python provides better facility for reading and writing contents on files. I found
it easier in Python as compared to Java.
While executing the program, Python took more time than Java. Total elapsed
time of Python for 100,000 numbers was approx. 28 mints but Java took around
7 mints to bubble sort same range of numbers.
Java is a statically typed and compiled language, and Python is a dynamically
typed and interpreted language. This single difference makes Java faster at
runtime and easier to debug, but Python is easier to use and easier to read.
Both programming languages are suitable for many people and have large
communities behind them. Learning one does not mean you can’t learn the other;
many programmers venture into multiple languages. And learning multiple can
reinforce the understanding of programming languages altogether.

You might also like