Lab03 DAA BubbleSort
Lab03 DAA BubbleSort
Faculty of Technology
Department of Information and Communication Technology
Subject: DAA (01CT0512) AIM: Bubble Sort
Experiment No: 03 Date: 01-08-2023 Enrolment No:92100133078
Theory:
Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in the wrong order. This algorithm
is not suitable for large data sets as its average and worst-case time complexity
is quite high.
Algorithm:
• traverse from left and compare adjacent elements and the higher one is
placed at right side.
• In this way, the largest element is moved to the rightmost end at first.
• This process is then continued to find the second largest and place it and
so on until the data is sorted.
Programming Language: Java
Code:
import java.io.*;
class BubbleSort {
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])
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: DAA (01CT0512) AIM: Bubble Sort
Experiment No: 03 Date: 01-08-2023 Enrolment No:92100133078
{
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[])
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: DAA (01CT0512) AIM: Bubble Sort
Experiment No: 03 Date: 01-08-2023 Enrolment No:92100133078
{
int arr[] = { 3,34,54,67,90,0,1,9 };
int n = arr.length;
bubbleSort(arr, n);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}
Output:
Time complexity:
Best case time complexity: __________
Justification:______________________________________________________
________________________________________________________________