Anda di halaman 1dari 32

DELHI

TECHNOLOGICAL
UNIVERSITY

DATA STRUCTURES LAB


Suraj Sharma
2K16/CO/324
1. (A) AIM: To sort the entered array using bubble sort
Description:
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacentelements if they are in wrong order.

Code:
#include<iostream>

using namespace std;

int main(){ int a[10],n;

cout<<"Enter Size Of Array(Max 10):"<<endl;

cin>>n;

cout<<"Enter Array Elements:"<<endl;

for(inti=0; i<n; i++){

cin>>a[i];

for(int i=0; i<n-1; i++){

for(int j=0; j<n-1-i; j++){

if(a[j+1]<a[j]){

int temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

cout<<"Array After"<<" "<<i+1<<" "<<"iteration is:";

for(int i=0; i<n; i++){

cout<<a[i];

}
cout<<endl;

cout<<"Sorted Array(Bubble Sort) Is:"<<endl;

for(inti=0; i<n; i++){

cout<<a[i];

Finding and learning:


For Worst Case: 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:

Worst Case: O(n^2), Best Case : O(n), Average Case: (n^2)

Worst case occurs when array is reverse sorted.

Best case occurs when array is already sorted.

Output:
1.(B)AIM: To sort the entered array using modified bubble sort
Code:
#include<iostream>

using namespace std;

int main(){

int a[10],n,flag=1;

cout<<"Enter Size Of Array(Max 10):"<<endl;

cin>>n;

cout<<"Enter Array Elements:"<<endl;

for(inti=0; i<n; i++){

cin>>a[i];

for(inti=0; i<n-1&&flag==1; i++){

flag=-1;

for(int j=0; j<n-1-i; j++){

if(a[j+1]<a[j]){

flag=1;

int temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

cout<<"Sorted Array(Modified Bubble Sort) Is:"<<endl;

for(inti=0; i<n; i++){

cout<<a[i];
}

return 0;

Finding and learning :


For Worst Case:

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:

Worst Case: O(n^2), Best Case : O(n), Average Case: (n^2)

Worst case occurs when array is reverse sorted.

Best case occurs when array is already sorted.

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>

using namespace std;

int main(){ int a[10],n,temp,pos;

cout<<"Enter Size Of Array(Max 10):"<<endl;

cin>>n;

cout<<"Enter Array Elements:"<<endl;

for(inti=0; i<n; i++){

cin>>a[i];

for(inti=0; i<n-1; 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<<"Sorted Array(Insertion Sort) Is:"<<endl;


for(inti=0; i<n; i++){

cout<<a[i];

return 0;

Finding and Learning :


Time Complexity:

Worst Case:O(n^2), Average Case:O(n^2), Best Case:O(n)

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.

1) The subarray which is already sorted.

2) Remaining subarray which is unsorted.

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>

using namespace std;

int main(){ int a[10],temp,n,min;

cout<<"Enter Size Of Array(Max 10):"<<endl;

cin>>n;

cout<<"Enter Array Elements:"<<endl;

for(inti=0; i<n; i++){

cin>>a[i];

for(inti=0; i<n-1; i++){

min=i;

for(int j=i+1; j<n; j++){

if(a[j]<a[min]){

min=j;

}
temp=a[min];

a[min]=a[i];

a[i]=temp;

cout<<"Sorted Array(Selection Sort) Is:"<<endl;

for(inti=0; i<n; i++){

cout<<a[i];

Finding and Learning :


In selection sort, the smallest element is selected and inserted at the right position.

Time Complexity: T(n)=n+(n-1)+(n-2)+…+1=(n(n+1))/2=n^2/2+n/2, T(n)=O(n^2/2)+O(n/2)=O(n^2)

Worst Case: O(n^2), Average Case:O(n^2), Best Case:O(n^2)

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>

using namespace std;

int main(){ int n,first=0,second=1,next;

cout<<"Enter number of terms you want: "<<endl;

cin>>n;

for(inti=0; i<n; i++){

cout<<first<<"";

next=first+second;

first=second;

second=next;

return 0; }

Finding and learning :


Time Complexity: O(n) as there is only one loop.

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>

using namespace std;

int fib(int n){

if(n==1){

return 0;

}else if(n==2){

return 1;

else{

return (fib(n-1)+fib(n-2));

int main(){

int n;

cout<<"Enter number of terms you want:"<<endl;

cin>>n;

for(inti=1; i<=n; i++)

cout<<fib(i)<<"";

}
Finding and learning :
Time Complexity: O(2^n)

Recurrence relation-

T(n)=T(n-1)+T(n-2)+4

Suppose: T(n-1) ≃ T(n-2)

For Lower Bound:

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)

For Upper Bound:

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>

using namespace std;

void merge(int a[],int start,int mid,int end){

int k=0;

int i=start;

int j=mid+1;

int b[end-start+1];

while(i<=mid && j<=end){

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;

for(int i=start; i<=end; i++){

a[i]=b[i-start];

void mergesort(int a[],int start,int end){

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;

cout<<"Enter Array Elements:\n";

for(int i=0; i<n; i++){

cin>>a[i];

mergesort(a,0,n-1);

cout<<"Sorted Array(By Merge Sort) Is:\n";

for(int i=0; i<n; i++){

cout<<a[i];

Finding and Learning:


Merge-sort is based on an algorithmic design pattern called divide-and-conquer. It forms tree
structure.
The height of the tree will be log(n). We merge n element at every level of the tree.
Time Complexity: Worst Case:O(nlogn), Average Case:O(nlogn), Best Case:O(nlogn)

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>

using namespace std;

int swap(int *a, int *b){ int temp=*a;

*a=*b;

*b=temp;

int partition(int a[], int start, int end){ int pivot=a[end];

int pi=start;

for(int i=start; i<end; i++){ if(a[i]<=pivot){

swap(&a[i],&a[pi]);

pi++;

swap(&a[pi],&a[end]);

return pi;

void quicksort(int a[], int start, int end){ int p;

if(start<end){ p=partition(a,start,end);

quicksort(a,start,p-1);
quicksort(a,p+1,end);

int main(){ int a[10],n;

cout<<"Enter Array Size:\n";

cin>>n;

cout<<"Enter Array Elements:\n";

for(int i=0; i<n; i++)

cin>>a[i];

quicksort(a,0,n-1);

cout<<"Sorted Array(By Quick Sort is):\n";

for(int i=0; i<n; i++)

cout<<a[i];

Finding and Learning:


Quick sort is based on an algorithmic design pattern called divide-and-conquer.

Time Complexity: Worst Case:O(n^2), Average Case:O(nlogn), BestCase:O(nlogn)

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>

using namespace std;

struct node{

int data;

node *next;

};

node *head= new node;

void insbeg(int v){

node *temp= new node;

temp->data=v;

temp->next=head;

head=temp;

void display(){

node *temp = head;

while(temp!=NULL){

cout<<temp->data<<" ";

temp=temp->next;

}
}

void deletebeg(){

node *temp=head;

head=head->next;

cout<<"Deleted element is:"<<" "<<temp->data;

delete temp;

int main(){

head=NULL;

int val,c;

char ch,chh;

do{

cout<<"Operations at the beginning of the linked list:\n";

cout<<"1.Insert\n";

cout<<"2.Delete\n";

cout<<"3.Display\n";

cout<<"Enter your choice:\n";

cin>>c;

switch(c){

case 1: do{

cout<<"Insert value at the beginning of the list:\n";

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;

default: cout<<"Invalid Choice\n";

cout<<"Want to perform more operations in the beginning?(Y/N):\n";

cin>>chh;

}while(chh=='Y'||chh=='y');

Finding and learning:


Time complexity for inserting an element at the beginning of the link list is O(1),

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>

using namespace std;

struct node{

int data;

node *next;

};

node *head= new node;

void inslast(int v){

node *temp=head;

node *temp1= new node;

temp1->data=v;

temp1->next=NULL;

if(temp==NULL){

head=temp1;

else{

while(temp->next!=NULL){

temp=temp->next;
}

temp->next=temp1;

void display(){

node *temp = head;

while(temp!=NULL){

cout<<temp->data<<" ";

temp=temp->next;

void deletelast(){

node *temp=head;

if(temp->next==NULL){

cout<<"Deleted element is:\n";

cout<<head->data;

head=NULL;

else{

while(temp->next->next!=NULL){

temp=temp->next;

node *temp1=temp->next;

temp->next=NULL;

cout<<"Deleted element is:\n";


cout<<temp1->data;

delete temp1;

int main(){

head=NULL;

int val,c;

char ch,chh;

do{

cout<<"Operations at the end of the linked list:\n";

cout<<"1.Insert\n";

cout<<"2.Delete\n";

cout<<"3.Display\n";

cout<<"Enter your choice:\n";

cin>>c;

switch(c){

case 1: do{

cout<<"Insert value at the end of the list:\n";

cin>>val;

inslast(val);

cout<<"Want to insert more values?(Y/N)\n";

cin>>ch;

}while(ch=='Y'||ch=='y');
break;

case 2: deletelast();

break;

case 3: cout<<"list:";

display();

cout<<"\n";

break;

default: cout<<"Invalid Choice\n";

cout<<"Want to perform more operations in the last?(Y/N):\n";

cin>>chh;

}while(chh=='Y'||chh=='y');

Finding and Learning:


Time complexity for inserting an element at the end of the link list is O(n),

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>

using namespace std;

struct node{

int data;

node *next;

};

node *head= new node;

void insn(int v, int n){

node *temp1= new node;

temp1->data=v;

temp1->next=NULL;

if(n==1){

temp1->next=head;

head=temp1;

else{

node *temp2=head;

for(int i=0; i<n-2; i++)

temp2=temp2->next;
temp1->next=temp2->next;

temp2->next=temp1;

void display(){

node *temp = head;

while(temp!=NULL){

cout<<temp->data<<" ";

temp=temp->next;

void deleten(int n){

node *temp=head;

if(n==1){

cout<<"Deleted element is:"<<" ";

cout<<temp->data<<"\n";

head=head->next;

delete temp;

else{

node *temp1=head;

for(int i=0; i<n-2; i++){

temp1=temp1->next;

node *temp2=temp1;
temp2=temp2->next;

cout<<"Deleted element is:"<<" ";

cout<<temp2->data<<"\n";

temp1->next=temp2->next;

delete temp2;

int main(){

head=NULL;

int val,c,n,p;

char ch,chh;

do{

cout<<"Operations on linked list:\n";

cout<<"1.Insert\n";

cout<<"2.Delete\n";

cout<<"3.Display\n";

cout<<"Enter your choice:"<<" ";

cin>>c;

switch(c){

case 1: do{

cout<<"Enter value to insert:"<<" ";

cin>>val;
cout<<"Enter position:"<<" ";

cin>>n;

insn(val,n);

cout<<"Want to insert more values?(Y/N)"<<" ";

cin>>ch;

}while(ch=='Y'||ch=='y');

break;

case 2: cout<<"Enter the node number you want to delete:"<<" ";

cin>>p;

deleten(p);

break;

case 3: cout<<"list:";

display();

cout<<"\n";

break;

default: cout<<"Invalid Choice";

cout<<"Want to perform more operations?(Y/N):"<<" ";

cin>>chh;

}while(chh=='Y'||chh=='y');

Finding and Learning:


Time Complexity:

If n is the position passed,

For insertion a nth position: O(n)

For deletion at nth position: O(n)

Output:

Anda mungkin juga menyukai