0% found this document useful (0 votes)
15 views5 pages

Daa 1

The document details an experiment on the implementation and analysis of Bubble Sort and Insertion Sort algorithms in Java. It includes theoretical explanations, pseudo code, time complexity analysis, and practical coding examples for both sorting methods. The conclusion highlights the educational value of these algorithms while noting their inefficiency for large datasets compared to more advanced sorting techniques.

Uploaded by

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

Daa 1

The document details an experiment on the implementation and analysis of Bubble Sort and Insertion Sort algorithms in Java. It includes theoretical explanations, pseudo code, time complexity analysis, and practical coding examples for both sorting methods. The conclusion highlights the educational value of these algorithms while noting their inefficiency for large datasets compared to more advanced sorting techniques.

Uploaded by

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

Bansilal Ramnath Agarwal Charitable Trust’s

Vishwakarma Institute of Technology, Pune-37


ET3272: Design and Analysis of Algorithm
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering


Name of the student: Ekta Ispande Roll No. 69
Div:B Batch:3
Date of performance:

Experiment no.1

Title: Implementation of sorting algorithm: Bubble Sort and


Insertion Sort
1. Theory :
a) Bubble Sort :
1. It is a comparison-based sorting algorithm.
2. It iterates through the list, comparing nearby elements and swapping them if they are in the
wrong order.
3. The process will be repeated until the entire list is sorted.
4. The main operation in Bubble Sort is to compare and exchange neighboring components.

b) Insertion Sort :

1. It is also a comparison-based sorting algorithm.


2. It creates the final sorted array one element at a time, selecting the next element and inserting it into
its proper location in the already sorted portion of the array.
3. The basic operation of Insertion Sort is to insert elements into the
correct positions in the sorted portion of the array.

2. Algorithm in Pseudo Code format :

a) Bubble Sort :
1. Initializing: Start the sorting with 1st element present in the list.
2. Compare and Swap: Compare the current element to the next one. If the current element is
larger than the next, swap them.
3. Repetition: Repeat the process until the largest element present in the list moves to the end.
4. Iteration: repeat the process for the next element excluding the last sorted element.
Pseudo Code :

BubbleSort(array)
n = length(array)
for i = 0 to n-1 do
for j = 0 to n-i-2 do
if array[j] > array[j+1] then
// Swap array[j] and array[j+1]
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
end if
end for
end for
end BubbleSort

b) Insertion Sort :

1. Initialization: Begin with the second element, as the first is trivially sorted.
2. Key Elements: Take the next element as the "key" and compare it to the elements in the
array's sorted segment (to the left of the key).
3. Shifting: Shift all items greater than the key to the right, making room for the key.
4. Insert Key: insert key into its desired position.
5. Iteration: repeat the process for all elements present in the list until the list is sorted.

Pseudo Code :

InsertionSort(array)
n = length(array)
for i = 1 to n-1 do
key = array[i]
j = i - 1
// Move elements of array[0..i-1] that are greater than
key
// to one position ahead of their current position
while j >= 0 and array[j] > key do
array[j + 1] = array[j]
j = j - 1
end while
array[j + 1] = key
end for
end InsertionSort

3. Analysis of the Algorithm:

a) Bubble Sort :

Cases Time Complexity Analysis


Best Case O(n) when the array has already
been sorted. In this scenario, a
single pass is conducted across
the array to see if any swaps
are required.
Average Case O(n^2) Bubble Sort will need to
perform several passes over the
array, swapping elements to
get the largest unsorted
element to the correct location.
Worst Case O(n^2) when the array is reverse
sorted. In this situation, every
element must be swapped in
each run, resulting in the
highest number of comparisons
and swaps.
b) Insertion Sort :

Cases Time Complexity Analysis


Best Case O(n) when the array has already
been sorted. In this situation,
each new element is compared
to the final element in the
sorted segment and promptly
placed in its proper location,
with no shifts.
Average Case O(n^2) For each element, half of the
previous elements in the sorted
section must be relocated. The
no. of comparisons and swaps
increases narrowly.
Worst Case O(n^2) when the array is reverse
sorted. Each new element is
compared to all items in the
sorted segment in this scenario
and must be moved to the first
position. This gives the
maximum no. of comparisons
and swaps

4. Experiment and result :

a) Bubble Sort :

CODE:

import java.util.Scanner;

public class BubbleSort {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Take user input for array size


System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];

// Take user input for array elements


System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

// Perform Bubble Sort


bubbleSort(arr);

// Display sorted array


System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}

scanner.close();
}

public static void bubbleSort(int[] arr) {


int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped in this pass, the array is
already sorted
if (!swapped) break;
}
}
}

RESULT:

Enter the number of elements: 4


Enter 4 elements:
7
8
3
6
Sorted array:
3 6 7 8

b) Insertion Sort :

CODE:
import java.util.Scanner;

public class InsertionSort {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Take user input for array size


System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];

// Take user input for array elements


System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

// Perform Insertion Sort


insertionSort(arr);

// Display sorted array


System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}

scanner.close();
}
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

// Move elements that are greater than key one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
}

RESULT:

Enter the number of elements: 5


Enter 5 elements:
9
1
5
3
8
Sorted array:
1 3 5 8 9

5. Conclusion:
In the lab experiment, we implemented and analyzed sorting algorithms in Java: Bubble sort and
insertion sort. By calculating the run time complexities of these 2 algorithms we understand their
varying efficiencies. Bubble Sort and Insertion Sort are both useful educational tools for understanding
sorting methods, although they are rarely utilized for large-scale data due to their quadratic time
complexity. Insertion Sort is chosen over Bubble Sort because it is more efficient at managing little or
partially sorted data. For handling the large datasets, we use more defined sorting algorithms such as
Quick Sort, Heap Sort.

You might also like