0% found this document useful (0 votes)
15 views

Lab03 DAA BubbleSort

The document describes the bubble sort algorithm. It provides the theory, algorithm, code sample in Java to implement bubble sort, and analyzes the time and space complexity. The code takes an integer array as input, sorts it using the bubble sort approach, and prints the sorted output array. The space complexity is O(1) as it only uses constant extra space. The best case time complexity is O(n) when the array is already sorted. The worst case time complexity is O(n^2) when the array is reverse sorted.

Uploaded by

farq.hai123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab03 DAA BubbleSort

The document describes the bubble sort algorithm. It provides the theory, algorithm, code sample in Java to implement bubble sort, and analyzes the time and space complexity. The code takes an integer array as input, sorts it using the bubble sort approach, and prints the sorted output array. The space complexity is O(1) as it only uses constant extra space. The best case time complexity is O(n) when the array is already sorted. The worst case time complexity is O(n^2) when the array is reverse sorted.

Uploaded by

farq.hai123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

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:

Space complexity: __________


Justification:______________________________________________________
________________________________________________________________

Time complexity:
Best case time complexity: __________
Justification:______________________________________________________
________________________________________________________________

Worst case time complexity: __________


Justification:______________________________________________________
________________________________________________________________

You might also like