Practical 1
Practical 1
Practical 1
PRACTICAL-1
AIM :Implementation and Time analysis of following sorting
algorithms. Bubble sort, Selection sort and Insertion sort. Also, derive
the time complexity of above algorithms using Asymptotic analysis.
1) Bubble Sort
Algorithm:
Code :
import java.util.*;
public class Sorting
{
static void bubbleSort(int[] arr)
{
boolean swapped;
for (int i = 0; i < arr.length-1; i++)
{
swapped = false;
for (int j = 1; j < arr.length-i; j++)
{
if (arr[j-1] > arr[j])
{
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
swapped = true;
1
210170107505 Analysis and design of algorithm Batch: - H3
}
}
if (!swapped)
{
break;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("Enter Size : ");
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = r.nextInt(2000);
}
long startTime = System.nanoTime();
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
System.out.println("Time to execute : " + (System.nanoTime() -
startTime)/1000 + " microSeconds"); sc.close();
}
}
Output :
2
210170107505 Analysis and design of algorithm Batch: - H3
2) Selection Sort
INTRODUCTION : Selection sort is conceptually the most simplest
sorting algorithm. This algorithm will first find the smallest element in
the array and swap it with the element in the first position, then it will
find the second smallest element and swap it with the element in the
second position, and it will keep on doing this until the entire array is
sorted.
Algorithm :
for i←0 to i←arr.length do
smallest←i
for j←i+1 to j>0 do
if A[j] < A[smallest] then
smallest = j
exchange( A[i], A[smallest] )
Code :
import java.util.*;
class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int k = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[k])
k = j;
int temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[])
3
210170107505 Analysis and design of algorithm Batch: - H3
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
long startTime = System.currentTimeMillis();
SelectionSort ob = new SelectionSort();
Random rd = new Random();
int arr[] = new int[10];
for(int i = 0 ; i<arr.length; i++){
arr[i] = rd.nextInt();
}
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
long endTime = System.currentTimeMillis();
long timeElasped = endTime - startTime;
System.out.println("Time taken in milliseconds : " + timeElasped);
}
}
Output :
4
210170107505 Analysis and design of algorithm Batch: - H3
3) Insertion Sort
INTRODUCTION: - Insertion sort is a simple sorting algorithm that
works similar to the way you sort playing cards in your hands. The array
is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted
part.
Algorithm :
for j=2 to A.length
key = A[j]
//insert A[j] into sorted sequence A[1,…..j-1]
i=j-1
while i>0 and A[i]>key
A[i+1] = A[i]
i=i-1
A[i+1] =key
Code :
import java.util.*;
class InsertionSort {
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
long startTime = System.currentTimeMillis();
InsertionSort ob = new InsertionSort();
Random rd = new Random();
5
210170107505 Analysis and design of algorithm Batch: - H3
Output :