You are on page 1of 6

Name: Rohit Adhari

Roll No:MC-I-101
Class: FY-MCA
Title: Comparative Analysis of C++ and java Bubble sort Algorithm
Abstract: This comparative analysis explores the Bubble sort Algorithm
implemented in Python and Java, two widely used programming languages with
distinct characteristics. The study examines the execution speed, memory
access, and language features of both Python and Java, shedding light on their
suitability for different application domains.

Introduction: Bubble Sort is a simple and straightforward sorting algorithm.


It is a comparison-based sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order. The
pass through the list is repeated until no swaps are needed, indicating that the
list is sorted. It is named "Bubble Sort" because smaller elements "bubble" to
the top of the list as the larger elements "sink" to the bottom

Java: Java is a widely-used, statically-typed, and compiled programming


language initially developed by James Gosling and released by Sun
Microsystems in 1995. It is celebrated for its portability and cross-platform
compatibility. Java's strong type system and robust memory management make
it a popular choice for building enterprise-level software, including large-scale
web applications, mobile apps (Android), and embedded systems. Its execution
on the Java Virtual Machine (JVM) ensures high performance and security. Java
boasts a vast ecosystem, with a wealth of libraries and frameworks for various
development needs.

C++: C++ is a high-performance, cross-platform programming language that


was developed by Bjarne Stroustrup as an extension to the C language . It is
widely used in operating systems, graphical user interfaces, and embedded
systems . It supports procedural, functional, and generic programming styles .
C++ gives programmers a high level of control over system resources and
memory .
Java

Program: Bubble sort

Code:
import java.io.*;

class GFG {
static void bubbleSort(int arr[], int n) {

int i, j, temp;
boolean swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}

if (swapped == false)
break;
}
}
static void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String args[]) {


long startTime = System.currentTimeMillis();
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = arr.length;
bubbleSort(arr, n);
System.out.println("Sorted array: ");s
printArray(arr, n);
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.o.println("Execution time: " + executionTime + " milliseconds");
}

Output:
C++
Program: Bubble sort
Code:
}#include <bits/std++.ch>
using namespace std;

void bubbleSort(int arr[], int n)


{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (swapped == false)
break;
}
}

void printArray(int arr[], int size)


{
int i;
for (i = 0; i < size; i++)
cout << " " << arr[i];
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
auto end_time = std::chrono::high_resolution_clock::now();
auto execution_time =
std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
start_time).count();
cout<<endl;
std::cout << "Program execution time: " << execution_time << "
milliseconds" << std::endl;
return 0;
}
Output:

Result:
Platform Line of File File Speed
code Capicity Capicity Accees
text
Java 36 4 kb 882 bytes 1 ms
C++ 34 4 kb 645 bytes 0 ms

Conclusion:
In conclusion, the comparison between Java and C++ for solving the Bubble
sort Algorithm problem reveals distinct characteristics of both programming
languages.
If execution time is a critical factor, C++ is likely to perform better than Java
for Bubble Sort and many other algorithms due to its lower-level control,
efficient memory management, and direct access to machine resources.
However, the exact performance difference can vary based on the specific
implementations, compiler settings, and hardware, so it's advisable to conduct
benchmarks for your specific use case to get a more accurate measurement of
the performance difference.

You might also like