Delhi Technological University: Data Structures Lab
Delhi Technological University: Data Structures Lab
Delhi Technological University: Data Structures Lab
TECHNOLOGICAL
UNIVERSITY
Code:
#include<iostream>
cin>>n;
cin>>a[i];
if(a[j+1]<a[j]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
cout<<a[i];
}
cout<<endl;
cout<<a[i];
T(n)=(n(n-1))/2=O(n^2/2)-O(1/2)=>O(n^2/2)=>O(n^2)
Time Complexity:
Output:
1.(B)AIM: To sort the entered array using modified bubble sort
Code:
#include<iostream>
int main(){
int a[10],n,flag=1;
cin>>n;
cin>>a[i];
flag=-1;
if(a[j+1]<a[j]){
flag=1;
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
cout<<a[i];
}
return 0;
T(n)=(n-1)+(n-2)+(n-3)+…+3+2+1
T(n)=(n(n-1))/2=O(n^2/2)-O(1/2)=>O(n^2/2)=>O(n^2)
Time Complexity:
In this modified version, less number of comparisons are made if the array is already sorted,
totally or partially.
Output:
2.AIM: To sort the entered array using insertion Sort
Description:
Insertion sort iterates, consuming one input element each repetition,and growing a sorted
output list.At eachiteration, insertion sort removes one element from theinput data, finds the
location it belongs within the sorted list, and inserts it there. It repeats until no input elements
remain.
Code:
#include<iostream>
cin>>n;
cin>>a[i];
temp=a[i+1];
pos=i+1;
while(temp<a[pos-1]&&pos>0){
a[pos]=a[pos-1];
pos--;
a[pos]=temp;
cout<<a[i];
return 0;
Boundary Cases:
Insertion sort takes maximum time to sort if elements are sorted in reverse order. And it takes
minimum time (Order of n) when elements are already sorted.
Output:
3.AIM: To sort the entered array using selection sort
Description:
The selection sort algorithm sorts an array by repeatedly finding the minimum
element(considering ascending order) from unsorted part and putting it at the beginning. The
algorithm maintains two subarrays in a given array.
In every iteration of selection sort, the minimum element (considering ascending order) from
the unsorted subarray is picked and moved to the sorted subarray.
Code:
#include<iostream>
cin>>n;
cin>>a[i];
min=i;
if(a[j]<a[min]){
min=j;
}
temp=a[min];
a[min]=a[i];
a[i]=temp;
cout<<a[i];
Output:
4.AIM: To print fibonacci series
Description:
Fibonacci Series is in the form of 0, 1, 1, 2, 3, 5, 8, 13, 21,...... To find this series we add
twoprevious terms digits and get next term number.
Code:
#include<iostream>
cin>>n;
cout<<first<<"";
next=first+second;
first=second;
second=next;
return 0; }
Output:
5.AIM: To print fibonacci series(using recursion)
Description:
Fibonacci Series is in the form of 0, 1, 1, 2, 3, 5, 8, 13, 21,...... To find this series we add two
previous terms/digits and get next term/number.
Code:
#include<iostream>
if(n==1){
return 0;
}else if(n==2){
return 1;
else{
return (fib(n-1)+fib(n-2));
int main(){
int n;
cin>>n;
cout<<fib(i)<<"";
}
Finding and learning :
Time Complexity: O(2^n)
Recurrence relation-
T(n)=T(n-1)+T(n-2)+4
T(n)=2T(n-2)+c :c=4
T(n)=2{2T(n-4)+c} +c
T(n)=2[2{2T(n-6)+c}]+c
T(n)=2^kT(n-2k)+(2^k-1)c
n-2k=0=> k=n/2
T(n)=2^(n/2)T(0)+(2^(n/2)-1)c≃2^(n/2)
T(n)=2T(n-1)+c :c=4
T(n)=2{2T(n-2)+c}+c
T(n)=2[2{2T(n-3)+c}]+c
T(n)=2^kT( n-k)+(2^k-1)c
n-k=0=>k=n
T(n)=2^nT(0)+(2^n-1)c≃2^n
So, T(n)=O(2^n).
Output:
6.AIM: Program to sort a given array using merge sort
Description:
In Merge Sort we, Split the data into two equal half until we get at most one element in both half.
Then Merge Both into one making sure the resulting sequence is sorted. Recursively split them
and merge them and store them in a temporary array and then elements are copied from
temporary array into the original array in the sorted form.
Code:
#include<iostream>
int k=0;
int i=start;
int j=mid+1;
int b[end-start+1];
if(a[i]<a[j]){
b[k]=a[i];
i=i+1;
k=k+1;
else{
b[k]=a[j];
j=j+1;
k=k+1;
}
while(j<=end){
b[k]=a[j];
k=k+1;
j=j+1;
while(i<=mid){
b[k]=a[i];
i=i+1;
k=k+1;
a[i]=b[i-start];
int mid;
if(start<end){
mid=(start+end)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
int main(){
int a[10],n;
cout<<"Enter Array Size:\n";
cin>>n;
cin>>a[i];
mergesort(a,0,n-1);
cout<<a[i];
Output:
7. AIM: Program to sort an array using quick sort
Description:
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into
smaller arrays. A large array is partitioned into two arrays one of which holds values smaller
than the specified value, say pivot, based on which the partition is made and another array holds
values greater than the pivot value. Quick sort partitions an array and then calls itself recursively
twice to sort the two resulting subarrays.
Code:
#include<iostream>
*a=*b;
*b=temp;
int pi=start;
swap(&a[i],&a[pi]);
pi++;
swap(&a[pi],&a[end]);
return pi;
if(start<end){ p=partition(a,start,end);
quicksort(a,start,p-1);
quicksort(a,p+1,end);
cin>>n;
cin>>a[i];
quicksort(a,0,n-1);
cout<<a[i];
Output:
8.AIM: Insertion and deletion at the beginning of the linked list
Description:
Linked list are data structures where individual elements are called node and each node
consists of a data and next part, the data part contains the data we need to store and the next
part is a pointer which stores the address of the next node, the next part of the last node is 0 or
NULL, which marks the end of the linked list.
Code:
#include<iostream>
struct node{
int data;
node *next;
};
temp->data=v;
temp->next=head;
head=temp;
void display(){
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
void deletebeg(){
node *temp=head;
head=head->next;
delete temp;
int main(){
head=NULL;
int val,c;
char ch,chh;
do{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cin>>c;
switch(c){
case 1: do{
cin>>val;
insbeg(val);
cout<<"Want to insert more values?(Y/N)\n";
cin>>ch;
}while(ch=='Y'||ch=='y');
break;
case 2: deletebeg();
break;
case 3: cout<<"list:";
display();
cout<<"\n";
break;
cin>>chh;
}while(chh=='Y'||chh=='y');
And for deleting an element from the beginning of the linked list is O(1),
The best part about the linked lists are that we do not have to worry about it being full.
Output:
9. AIM: Insertion and deletion at the end of the linked list
Description:
Linked list are data structures where individual elements are called node and each node
consists of a data and next part, the data part contains the data we need to store and the next
part is a pointer which stores the address of the next node, the next part of the last node is 0 or
NULL, which marks the end of the linked list.
Code:
#include<iostream>
struct node{
int data;
node *next;
};
node *temp=head;
temp1->data=v;
temp1->next=NULL;
if(temp==NULL){
head=temp1;
else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=temp1;
void display(){
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
void deletelast(){
node *temp=head;
if(temp->next==NULL){
cout<<head->data;
head=NULL;
else{
while(temp->next->next!=NULL){
temp=temp->next;
node *temp1=temp->next;
temp->next=NULL;
delete temp1;
int main(){
head=NULL;
int val,c;
char ch,chh;
do{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cin>>c;
switch(c){
case 1: do{
cin>>val;
inslast(val);
cin>>ch;
}while(ch=='Y'||ch=='y');
break;
case 2: deletelast();
break;
case 3: cout<<"list:";
display();
cout<<"\n";
break;
cin>>chh;
}while(chh=='Y'||chh=='y');
And for deleting an element from the end of the linked list is O(n),
The best part about the linked lists are that we do not have to worry about it being full.
Output:
10.AIM: Insertion and deletion at nth position of the linked list
Description:
Linked list are data structures where individual elements are called node and each node
consists of a data and next part, the data part contains the data we need to store and the next
part is a pointer which stores the address of the next node, the next part of the last node is 0 or
NULL, which marks the end of the linked list.
Code:
#include<iostream>
struct node{
int data;
node *next;
};
temp1->data=v;
temp1->next=NULL;
if(n==1){
temp1->next=head;
head=temp1;
else{
node *temp2=head;
temp2=temp2->next;
temp1->next=temp2->next;
temp2->next=temp1;
void display(){
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
node *temp=head;
if(n==1){
cout<<temp->data<<"\n";
head=head->next;
delete temp;
else{
node *temp1=head;
temp1=temp1->next;
node *temp2=temp1;
temp2=temp2->next;
cout<<temp2->data<<"\n";
temp1->next=temp2->next;
delete temp2;
int main(){
head=NULL;
int val,c,n,p;
char ch,chh;
do{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cin>>c;
switch(c){
case 1: do{
cin>>val;
cout<<"Enter position:"<<" ";
cin>>n;
insn(val,n);
cin>>ch;
}while(ch=='Y'||ch=='y');
break;
cin>>p;
deleten(p);
break;
case 3: cout<<"list:";
display();
cout<<"\n";
break;
cin>>chh;
}while(chh=='Y'||chh=='y');
Output: