You are on page 1of 19

MERI COLLEGE

OF
ENGINEERING AND TECHNOLOGY

PRACTICAL FILE
ON
“PYTHON”

Submitted To: Submitted By:


Ms. Kirti Name: KhushbooVashisht
Assistant Professor Roll No.:CSE/14/18
CSE Department Branch:CSE
Semester:3
S.No. Topic Date Signature
1. Write a program to demonstrate the use of linear 16/8/2019
search to search a given element in an array

2. Write a program to demonstrate the use of binary 23/8/2019


search to search a given element in an array

3. Write a program to sort an array of integers in 30/8/2019


ascending order using bubble sort

4. Write a program to sort an array of integers in 6/9/2019


ascending order using selection sort

5. Write a program to sort an array of integers in 13/9/2019


ascending order using insertion sort

6. Write a program to sort an array of integers in 20/9/2019


ascending order using quick sort

7. Write a program to sort an array of integers in 27/9/2019


ascending order using merge sort

8. Write a program to demonstrate the use of stack 4/10/2019


implemented using linear array

9. Write a program to demonstrate the 11/10/2019


implementation of various operation on a linear
queue represented using a linear array

10. Write a program to implement various linked list 18/10/2019


operations
Program No.1

AIM: Write a program to demonstrate the use of linear search to search a given element in an
array

SOURCE CODE:
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
int array[100], search, c, n;
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search: ");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location :%d ", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.", search);
return 0;
}
OUTPUT
Program No.2

AIM: Write a program to demonstrate the use of binary search to search a given element in an
array

SOURCE CODE:
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
OUTPUT
Program No.3

AIM: Write a program to sort an array of integers in ascending order using bubble sort

SOURCE CODE:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
OUTPUT
Program No.4

AIM: Write a program to sort an array of integers in ascending order using selection sort

SOURCE CODE:
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
OUTPUT
Program No.5

AIM: Write a program to sort an array of integers in ascending order using insertion sort

SOURCE CODE:
#include <stdio.h>

int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d-1] > array[d]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}
OUTPUT
Program No.6

AIM: Write a program to sort an array of integers in ascending order using quick sort

SOURCE CODE:
#include<stdio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)


{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{

int pi = partition(arr, low, high);


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
}
// Driver program to test above functions
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5,23,67,85};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}

OUTPUT
Program No.7

AIM: Write a program to sort an array of integers in ascending order using merge sort

SOURCE CODE:
#include <stdio.h>
#include<conio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high)
{
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++)
{
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high)
{
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
}
else
{
return;
}
}
int main()
{
clrscr();
int i;
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}

OUTPUT
Program No.8

AIM: Write a program to demonstrate the use of stack implemented using linear array

SOURCE CODE:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

}
OUTPUT
Program No.9
AIM: Write a program to demonstrate the implementation of various operation on a linear
queue represented using a linear array

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}

OUTPUT
Program No.10

AIM: Write a program to implement various linked list operations

SOURCE CODE:
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
#include<conio.h>
struct node
{
int info;
node *next;
}*start,*newptr,*save,*ptr,*rear;
node *create(int);
void insert(node*);
void display(node*);
void delnode();
int main()
{
clrscr();
start=rear=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"Enter the information for the new node: ";
cin>>inf;
newptr=create(inf);
if(newptr !=NULL)
{
cout<<"New node created successfully!!";
}
else
{
cout<<"Cannot create new node!!";
exit(1);
}
insert(newptr);
cout<<endl<<"Press y to enter new node and n to exit: ";
cin>>ch;
}
do
{
cout<<"The list now is: ";
display(start);
cout<<endl<<"Want to delete first node?(y/n): ";
cin>>ch;
if(ch=='y'||ch=='Y')
delnode();
}while(ch=='y'||ch=='Y');
return 0;
}
node *create(int n)
{
ptr=new node;
ptr -> info=n;
ptr -> next=NULL;
return ptr;
}
void insert(node*np)
{
node*temp;
if(start==NULL)
start=np;
else
{
temp=start;
start=np;
np ->next=temp;
}
}
void delnode()
{
if(start==NULL)
cout<<"UNDERFLOW!!!";
else
{
ptr=start;
start=start->next;
delete ptr;
}
}
void display(node *np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
}
OUTPUT

You might also like