Aim: - Source Code:: Program:-1
Aim: - Source Code:: Program:-1
• Aim:
Write a program for binary search method .
• Source code:
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key){
low = mid + 1;
}
else if (array[mid] == key) {
printf("%d found at location %d", key, mid+1);
break;
}
else{
high = mid - 1;
mid = (low + high)/2;
}
}
if(low > high){
printf("Not found! %d isn't present in the list", key);
}
printf("\nCode written by Ishu ");
return 0;
}
• Output:
PROGRAM:-2(A)
• Aim:
A) Write a program for insertion sort.
• Source code:
#include <math.h>
#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {13, 11, 14, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
printf("\n");
printf("Code is written by Ishu");
return 0;
}
• Output:
PROGRAM:-2(B)
• Aim:
B)Write a program for selection sort.
• Source code:
#include <stdio.h>
*xp = *yp;
*yp = temp;
int i, j, min_idx;
min_idx = i;
min_idx = j;
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
int i;
printf("\n");
int main()
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printArray(arr, n);
printf("\n");
return 0;
• Output:
PROGRAM :-2(c)
• Aim:
Write a program for bubble sort.
• Source code:
#include <stdio.h>
void bubbleSort(int array[], int size) {
for (int step = 0; step < size - 1; ++step) {
for (int i = 0; i < size - step - 1; ++i) {
if (array[i] > array[i + 1]) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int data[] = {-2, 43, 0, 12, -9};
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
printf("Sorted Array in Ascending Order:\n");
printArray(data, size);
printf("\n");
printf("Code is written by Ishu");
}
• Output:
PROGRAM -3
• Aim:
Write a program to implement stack and its operations.
• Source code:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
printf("Code is written by Ishu\n");
top=-1;
printf("\nEnter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n STACK OPERATIONS USING ARRAY");
printf("\n--------------------------------");
printf("\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 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 EXIT POINT ");
break;
}
default:
{
printf ("\n Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\nSTACK 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 Stack is under flow");
}
else
{
printf("\n 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:-4
• Aim:
Write a program for Quick sort.
• Source code:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
quicksort(number,0,count-1);
• Output:
PROGRAM:-5
• Aim:
Write a program for Merge sort.
• Source code:
#include <stdio.h>
#include <stdlib.h>
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
int arr[] = {22, 11, 23, 5, 8, 7, 97, 84, 35, 58,};
int arr_size = sizeof(arr) / sizeof(arr[0]);