Data Structure Assignment
Data Structure Assignment
Data Structure
Submitted By:
Ahmad Sultan
Submitted To:
Sir Umair saab
Roll No:
21-UON-0906
Semester:
BSCS 3rd Evening Old Campus
Assignment Topics:
b) Bubble Sort
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = { 5, 1, 4, 2, 8};
int N = sizeof(arr) / sizeof(arr[0]);
cout << "Unsorted array: \n";
printArray(arr, N);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}
OutPut
c) Selection Sort
#include<bits/stdc++.h>
using namespace std;
void selectionSort(int arr[], int n)
{
int i,j,min_in;
for(i=0;i<n;i++)
{
min_in = i;
for(j=i+1;j<n;j++)
if (arr[j] < arr[min_in])
min_in = j;
swap(arr[i],arr[min_in]);
}
}
void print(int arr[], int n)
{
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main(int argv, char* argc[])
{
int arr[] = {5,4,10,1,6,2};
int i,j,n,temp;
n = sizeof(arr)/sizeof(int);
cout<<"Unsorted Array :";
print(arr,n);
selectionSort(arr,n);
cout<<"Sorted Array :";
print(arr,n);
return(0);
}
OutPut
d) Linear Search
#include<bits/stdc++.h>
using namespace std;
return 0;
}
OutPut
e) Binary Search
#include <bits/stdc++.h>
using namespace std;
int BinarySearch(int sorted_array[], int left, int right, int element)
{
while (left <= right)
{
int middle = (left + right) / 2;
if (sorted_array[middle] == element)
return middle;
if (sorted_array[middle] < element)
left = middle + 1;
else
right = middle - 1;
}
return -1;
}
int main()
{
int a[] = { 10, 12, 20, 32, 50, 55, 65, 80, 99 };
int element;
cout<<"Enter element to search"<<endl;
cin>>element;
int size = sizeof(a) / sizeof(a[0]);
sort(a, a + size);
int result = BinarySearch(a, 0, size - 1, element);
if(result == -1)
cout<<"Element is not present in array";
else
cout<<"Element is present at index = "<< result;
return 0;
}
OutPut
#include <bits/stdc++.h>
using namespace std;
class node {
public:
int data;
node* next;
node(int value)
{
data = value;
next = NULL;
}
};
void insertAt_beginning(node*& head, int val)
{
node* n = new node(val);
n->next = head;
head = n;
}
void insertAt_given_Location(node* head, int key, int val)
{
node* n = new node(val);
if (key == head->data) {
n->next = head->next;
head->next = n;
return;
}
insertAt_beginning(head, 1);
insertAt_beginning(head, 2);
cout << "At the beginning : ";
print(head);
cout<<endl;
insertAtthe_End(head, 4);
insertAtthe_End(head, 5);
cout << "At the end ";
print(head);
cout << endl;
insertAt_given_Location(head, 1, 2);
insertAt_given_Location(head, 5, 6);
cout << "At the given location in the sorted list ";
print(head);
cout << endl;
return 0;
}
OutPut
b) Deletion of first node of last node of given item of node of given item from
sorted list
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(Node** head_ref, int key)
{
Node* temp = *head_ref;
Node* prev = NULL;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next;
delete temp;
return;
}
else {
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
delete temp;
}
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
deleteNode(&head, 1);
cout<<"\nLinked List after Deletion of 1:"<<endl;
printList(head);
return 0;
}
OutPut
The End