You are on page 1of 6

Project File

Of

DESIGN AND ANALYSIS OFALGORITHMS

A report submitted in partial fulfilment of the requirement for the Vth Semester
of

BACHELOR OF TECHNOLOGY In COMPUTER


SCIENCE AND ENGINEERING

Session 2022-23

Submitted to: - Submitted by: -


Preeti Gupta Name: Shivani
Chandrawal
Assistant Professor, Roll No.: 200102588
CSE Department Sap ID: 1000014850
Class/Section: CSE-G

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
OBJECTIVE:

Write a project to perform the merge sort and quicksort


a. Creation
b. Display

• Merge Sort

CODE:-
#include <stdio.h>
void mergesort();
void merge ();
int main(){
int a[40],n,i;
printf("Name :-Shivani Chandrawal sapid:-
100001485o \n");printf("enter size of array :");
scanf("%d",&n);
printf("enter elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("sorted elements are :");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
void mergesort (int a[],int l, int r){int m;if(l<r){m =(l+r)/2;
mergesort(a,l,m);
mergesort(a,m+1,r);
merge(a,l,m,r);
}
}
void merge(int a[],int first,int mid,int last){

int b[50];

Shivani chandrawal 1000014850


int i,j,k;
i=first;
j=mid+1;

k=first;
while(i<=mid&&j<=last){
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
if(i>mid){
while(j<=last)
b[k++]=a[j++];
}else{
while(i<=mid)
b[k++]=a[i++];
}
for(i=first;i<=last;i++)
a[i]=b[i];
}

OUTPUT:-

Shivani Chandrawal 1000014850


• QUICK SORT
CODE:-

import java.util.*;
class Main{
public int partition(int [] a,int start,int end){
int pindex=start;
int pivot=a[end];
for(int i=start;i<end;i++){
if(a[i]<=pivot){
int t=a[i];
a[i]=a[pindex];
a[pindex]=t;
pindex++;
}
}
int t=a[end];
a[end]=a[pindex];
a[pindex]=t;
return pindex;
}
public void Quicksort(int a[],int start ,int end){
if(start<end){
int pindex=partition(a, start, end);
Quicksort(a,start,pindex-1);
Quicksort(a, pindex+1, end);

}
}

public static void main(String[] args) {

System.out.println("Name:-Shivani Chandrawal Sapid:-

1000014850");

Shivani chandrawal 1000014850


Scanner sc=new Scanner(System.in);
int [] a=new int[10];
System.out.println("Enter the number of elements");
int n=sc.nextInt();

System.out.println("enter the elements ");


for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
Main m=new Main();
m.Quicksort(a,0,n-1);
System.out.println("elements after sorting :-");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}

}
}

OUTPUT:-

Shivani Chandrawal 1000014850


Shivani chandrawal 1000014850

You might also like