0% found this document useful (0 votes)
9 views9 pages

Data Structure Assignment (1)

The document contains a series of programming assignments that implement various sorting algorithms (Bubble Sort, Quick Sort, Selection Sort, Insertion Sort), search algorithms (Linear Search, Binary Search), and data structures (Stack, Queue, Singly Linked List) in C++. Each assignment includes code snippets, input/output examples, and explanations of the operations performed. The programs demonstrate fundamental concepts in data structures and algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Data Structure Assignment (1)

The document contains a series of programming assignments that implement various sorting algorithms (Bubble Sort, Quick Sort, Selection Sort, Insertion Sort), search algorithms (Linear Search, Binary Search), and data structures (Stack, Queue, Singly Linked List) in C++. Each assignment includes code snippets, input/output examples, and explanations of the operations performed. The programs demonstrate fundamental concepts in data structures and algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment No.

1
Write a program to implement Bubble sort.

//Program to sort an array using bubble sort technique

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[7],i,j,t;

//Enter array elements


cout<<"Enter the Array Elements:\n";
for(i=0;i<=6;i++)
{
cin>>a[i];
}

//sort the array using bubble sort technique


for(i=0;i<=6;i++)
{
for(j=0;j<=6;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

//print the sorted array elements


cout<<"After Sorting Array is:\n";
for(i=0;i<=6;i++)
{
cout<<a[i]<<"\t";
}
getch();
}

Output:

Enter the Array Elements:


4 12 34 76 8 16 25
After Sorting Array is:
4 8 12 16 25 34 76
Assignment No. 2
Write a program to implement Quick sort

#include<iostream.h> quickSort(array, low, pi - 1);


#include<conio.h> quickSort(array, pi + 1, high);
void swap(int *a, int *b) }
{ }
int t = *a;
*a = *b; int main()
*b = t; {
} clrscr();
int partition(int array[], int low, int high) int arr[8] = {7, 2, 1, 6, 8, 5, 3, 4};
{ int n=8,i;
int pivot = array[high];
int i = (low - 1); cout << "Original array is: ";
for (int j = low; j < high; j++) for (i = 0; i < n; i++)
{ cout << arr[i] << " ";
if (array[j] <= pivot) cout << endl;
{ quickSort(arr, 0, n - 1);
i++; cout << "Sorted Array in Ascending
swap(&array[i], &array[j]); Order: ";
} for (i = 0; i < n; i++)
} cout << arr[i] << " ";
swap(&array[i + 1], &array[high]); cout << endl;
return (i + 1); getch();
} }
void quickSort(int array[], int low, int
high) Output:
{ Original array is: 7 2 1 6 8 5 3 4
if (low < high) Sorted Array in Ascending Order: 1 2 3 4
{ 5678
int pi = partition(array, low, high);
Assignment No. 3
Write a program to implement Selection sort

//Program to sort an array using selection sort technique


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[7],i,j,t,min;

//Enter array elements


cout<<"Enter the Array Elements:\n";
for(i=0;i<=6;i++)
{
cin>>a[i];
}

//sort the array using selection sort technique


for(i=0;i<=6;i++)
{
min=i;
for(j=i+1;j<=6;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=i)
{
t=a[min];
a[min]=a[i];
a[i]=t;
}
}

//print the sorted array elements


cout<<"After Sorting Array is:\n";
for(i=0;i<=6;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
Output:
Enter the Array Elements:
61 34 68 21 32 19 38
After Sorting Array is:
19 21 32 34 38 61 68
Assignment No. 4
Write a program to implement Insertion sort

//Program to sort an array using insertion sort technique


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[7],i,j,temp,min;

//Enter array elements


cout<<"Enter the Array Elements:\n";
for(i=0;i<=6;i++)
{
cin>>a[i];
}

//sort the array using insertion sort technique


for(i=1;i<=6;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}

//print the sorted array elements


cout<<"After Sorting Array is:\n";
for(i=0;i<=6;i++)
{
cout<<a[i]<<"\t";
}
getch();
}

Output:
Enter the Array Elements:
32 54 23 9 16 28 19
After Sorting Array is:
9 16 19 23 28 32 54
Assignment No. 5
Write a program to implement Linear search

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n,flag;
cout<<"Enter 10 array elements\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"\n Enter the Element to search:";
cin>>n;

for(i=0;i<10;i++)
{
if(a[i]==n)
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"Element found at position:"<<i;
}
else
{
cout<<"Element not found";
}
getch();
}

Output:
Enter 10 array elements
45 23 12 7 19 30 9 5 14 22

Enter the Element to search:9


Element found at position:6
Assignment No. 6
Write a program to implement Binary search

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n,low=0,high=9,mid;
cout<<"Enter Array Elements: \n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"\nEnter the Element to search\n";
cin>>n;

while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==n)
{
cout<<"Element found at position: "<<mid;
break;
}
else if(n<a[mid])
{
high=mid-1;
}
else if(n>a[mid])
{
low=mid+1;
} }
if(low>high)
{
cout<<"Element Not Found";
}
getch();
}

Output:
Enter Array Elements:
2 3 6 8 10 13 17 21 35 51

Enter the Element to search


10
Element found at position: 4
Assignment No. 7
Write a program to implement Stack operations: push, pop, display

#include<iostream.h> cout<<"4) exit"<<endl;


#include<conio.h> do
int stack[5],n=5,top=-1; {
void push(int val) cout<<"Enter your choice: ";
{ cin>>ch;
cout<<"\nEnter element: ";
cin>>val; switch(ch)
if(top>=n-1) {
{ case 1: push(val);
cout<<"Stack Overflow"; break;
} case 2: pop();
else break;
{ case 3: display();
top++; break;
stack[top]=val; case 4: cout<<"Exit"<<endl;
} break;
} default: cout<<"Innvalid Choice";
void pop() }
{ }while(ch!=4);
if(top<0)
{ getch();
cout<<"Stack Underflow"; }
}
else Output:
{ 1) push element into stack
cout<<"\nPopped element is: 2) pop from stack
"<<stack[top]<<endl; 3) display stack
top--; 4) exit
}
} Enter your choice: 1
void display() Enter element: 10
{ Enter your choice: 1
if(top>=0) Enter element: 20
{ Enter your choice: 1
cout<<"\nElements in stack are: "; Enter element: 30
for(int i=top;i>=0;i--) Enter your choice: 1
{ Enter element: 40
cout<<stack[i]<<"\t"; Enter your choice: 1
} } Enter element: 50
else Enter your choice: 3
{ Elements in stack are: 50 40 30 20 10
cout<<"Stack is Empty"; Enter your choice: 2
}} Popped element is: 50
Enter your choice: 2
int main() Popped element is: 40
{ Enter your choice: 2
clrscr(); Popped element is: 30
int ch, val; Enter your choice: 3
cout<<"1) push element into stack"<<endl; Elements in stack are: 20 10
cout<<"2) pop from stack"<<endl; Enter your choice: 4
cout<<"3) display stack"<<endl; Exit
Assignment No. 8
Write a program to implement Linear Queue operations: Insert, Delete, Display

#include<iostream.h> cout<<"2) Delete element from


#include<conio.h> queue"<<endl;
int queue[10], n = 10, front = - 1, rear = - 1; cout<<"3) Display all the elements of
queue"<<endl;
void Insert() cout<<"4) Exit"<<endl;
{ do
int val; {
if (rear == n - 1) cout<<"Enter your choice : ";
cout<<"Queue Overflow"<<endl; cin>>ch;
else switch (ch)
{ { case 1: Insert();
if (front == - 1) break;
front = 0; case 2: Delete();
cout<<"Insert the element in queue : "; break;
cin>>val; case 3: Display();
rear++; break;
queue[rear] = val; case 4: cout<<"Exit"<<endl;
} break;
} default: cout<<"Invalid
void Delete() choice"<<endl;
{ } } while(ch!=4);
if (front == - 1 || front > rear) getch();
{ }
cout<<"Queue Underflow ";
return ; Output:
} 1) Insert element to queue
else 2) Delete element from queue
{ 3) Display all the elements of queue
cout<<"Element deleted from queue is : 4) Exit
"<< queue[front] <<endl; Enter your choice : 1
front++;; Insert the element in queue : 10
} Enter your choice : 1
} Insert the element in queue : 20
void Display() Enter your choice : 1
{ Insert the element in queue : 30
if (front == - 1) Enter your choice : 1
cout<<"Queue is empty"<<endl; Insert the element in queue : 40
else Enter your choice : 1
{ Insert the element in queue : 50
cout<<"Queue elements are : "; Enter your choice : 3
for (int i = front; i <= rear; i++) Queue elements are : 10 20 30 40 50
cout<<queue[i]<<" "; Enter your choice : 2
cout<<endl; Element deleted from queue is : 10
} Enter your choice : 2
} Element deleted from queue is : 20
int main() Enter your choice : 2
{ Element deleted from queue is : 30
clrscr(); Enter your choice : 3
int ch; Queue elements are : 40 50
cout<<"1) Insert element to queue"<<endl; Enter your choice : 4
Exit
Assignment No. 9
Write a program to implement singly linked list with operations i) create ii) insert iii)
delete

#include<iostream.h> struct Node *temp = head;


#include<conio.h> while(temp!=NULL)
struct Node {
{ cout<<temp->num<<" ";
int num; temp=temp->next;
Node *next; }
}; cout<<endl;
struct Node *head=NULL; }

void insert_Node(int n) int main()


{ {
struct Node *new_node=new Node; clrscr();
new_node->num=n; insert_Node(1);
new_node->next=head; insert_Node(3);
head=new_node; insert_Node(5);
} insert_Node(7);
insert_Node(9);
void deletenode() insert_Node(11);
{ display();
if (head == NULL) deletenode();
{ cout<<"List after deleting the node: ";
return; display();
} getch();
Node *temp = head; }
head = head->next;
delete temp; Output:
} The list contains the data:
11 9 7 5 3 1
void display() List after deleting the node:
{ The list contains the data:
cout<<"\nThe list contains the data:\n"; 97531

You might also like