0% found this document useful (0 votes)
55 views11 pages

CSF302 - Lab File - Ujjwal - 1000014201

This document contains the source code and output of several sorting algorithms implemented in Java including insertion sort, bubble sort, selection sort, merge sort, and quicksort. It was submitted as a lab practical for a course on design and analysis of algorithms by a student named Ujjwal with SAP ID 1000014201 at DIT University. The programs take user input of array sizes and elements, perform the sorting algorithm, and output the sorted array.

Uploaded by

Mahul Sethi
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)
55 views11 pages

CSF302 - Lab File - Ujjwal - 1000014201

This document contains the source code and output of several sorting algorithms implemented in Java including insertion sort, bubble sort, selection sort, merge sort, and quicksort. It was submitted as a lab practical for a course on design and analysis of algorithms by a student named Ujjwal with SAP ID 1000014201 at DIT University. The programs take user input of array sizes and elements, perform the sorting algorithm, and output the sorted array.

Uploaded by

Mahul Sethi
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/ 11

LAB PRACTICAL (CSF302)

A report submitted in partial fulfilment of the requirement for the course

DESIGN AND ANALYSIS OF ALGORITHMS


Part of the degree of

BACHELOR OF

TECHNOLOGY

In
CSE/IT/EE/CE/PE

Submitted to : Submitted by :
Dr. Anurag Aeron Ujjwal
Associate Professor SAP ID : 1000014201
School of Computing Roll NO. : 200102029
DIT University Section : A (P2)

SCHOOL OF COMPUTING
DIT UNIVERSITY, DEHRADUN
(State Private University through State Legislature Act No. 10 of 2013 of Uttarakhand and approved by
UGC)

Mussoorie Diversion Road, Dehradun, Uttarakhand - 248009, India.


2021
Program:
import java.util.* ;
class Main {
public static int factorial(int num) {
int cal=1 ;
if(num == 0 )
return 1 ;
for(int i = 1 ; i <= num ; i++) {
cal = cal*i ;
}
return cal ;
}
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter the number :- ") ;
int number = sc.nextInt() ;
System.out.println("Factorial of "+number+" is :- "+factorial(number)) ;
System.out.print("\nName :- Ujjwal\nSap Id :- 1000014201") ;
}
}
Output:
Program:
import java.util.* ;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter the size of your array :- ") ;
int size = sc.nextInt() ;
int array[] = new int[size] ;
System.out.println("Enter the elements :- ");
for(int i = 0 ; i < size ; i++) {
array[i] = sc.nextInt() ;
}
System.out.print("After insertion sort :- ") ;

int i, key, j;
for (i = 1; i < size; i++) {
key = array[i];
j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
array[j+1] = key ;
}
for(int z = 0 ; z < size ; z++) {
System.out.print(array[z]+" ");
}
System.out.print("\nName :- Ujjwal\nSap Id :- 1000014201") ;

}
}
Output:

Program:
import java.util.* ;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter the size of your array :- ") ;
int size = sc.nextInt() ;
int array[] = new int[size] ;
System.out.println("Enter the elements :- ");
for(int i = 0 ; i < size ; i++) {
array[i] = sc.nextInt() ;
}
System.out.print("After Bubble sort :- ") ;

int i , j ;
for(i = 0 ; i < array.length - 1 ; i++) {
for(j = 0 ; j < array.length - i - 1 ; j++) {
if(array[j] > array[j+1]) {
int temp = array[j] ;
array[j] = array[j+1] ;
array[j+1] = temp ;
}
}

}
for(int z = 0 ; z < size ; z++) {
System.out.print(array[z]+" ");
}

System.out.print("\nName :- Ujjwal\nSap Id :- 1000014201") ;


}
}
Output:

Program:
public class Main
{
void Selection_sort(int Arr[])
{
int len = Arr.length;
for (int j = 0; j<len-1; j++)
{
int min_position = j;
for (int i = j + 1; i < len; i++)
{
if (Arr[i] < Arr[min_position])
{
min_position = i;
}
}

int temp = Arr[j];


Arr[j] = Arr[min_position];
Arr[min_position] = temp;
}
System.out.println("\nThe sorted Array is: ");
for (int i = 0; i < len; i++)
{
System.out.print(Arr[i] +" ");

}
public static void main(String args[])
{
int[] array = {12, 5, 34, 21, 45, 100, 25, 17, 40, 3};
Main ss = new Main();
System.out.print("Array before sorted :- \n") ;
for(int i = 0 ; i < array.length ; i++) {
System.out.print(array[i] +" ") ;
}
ss.Selection_sort(array);
System.out.println("\nName :- Ujjwal\nSap Id :- 1000014201");

}
}
Output:
Program:

import java.util.* ;
class MergeSort
{
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;

int L[] = new int[n1];


int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];

int i = 0, j = 0;

int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

void sort(int arr[], int l, int r)


{
if (l < r) {

int m =l+ (r-l)/2;

sort(arr, l, m);
sort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

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[])


{

Scanner sc = new Scanner(System.in) ;


System.out.print("Enter the size of the array :- ") ;
int size = sc.nextInt() ;
int arr[] = new int[size] ;
System.out.println("Enter the elements :- ") ;
for(int i = 0 ; i < arr.length ; i++) {
arr[i] = sc.nextInt() ;
}

MergeSort ob = new MergeSort();


ob.sort(arr, 0, arr.length - 1);

System.out.println("\nSorted array ");


printArray(arr);
System.out.println("\nName :- Ujjwal \nSap Id :- 1000014201") ;
}
}

Output:

Program:
import java.util.* ;
public class Quick
{
int partition (int a[], int start, int end)
{
int pivot = a[end];
int i = (start - 1);

for (int j = start; j <= end - 1; j++)


{

if (a[j] < pivot)


{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}

void quick(int a[], int start, int end)


{
if (start < end)
{
int p = partition(a, start, end);
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}

void printArr(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter the size of the array :- ") ;
int size = sc.nextInt();

int a[] = new int[size] ;


System.out.println("Enter the element :- ") ;
for(int i = 0 ; i < a.length ; i++) {
a[i] = sc.nextInt() ;
}
int n = a.length;
System.out.println("\nBefore sorting array elements are - ");
Quick q1 = new Quick();
q1.printArr(a, n);
q1.quick(a, 0, n - 1);
System.out.println("\nAfter sorting array elements are - ");
q1.printArr(a, n);
System.out.println();
System.out.println("\nName :- Ujjwal \nSap Id :- 1000014201") ;
}
}
Output:

You might also like