0% found this document useful (0 votes)
24 views18 pages

Aim: - Source Code:: Program:-1

The document contains source code for implementing various sorting algorithms in C programming language. It includes code for binary search, insertion sort, selection sort, bubble sort, stack operations, quick sort and merge sort. The code is written by Ishu and includes the necessary header files, functions for implementing the algorithms, main function with input/output operations, and comments explaining the purpose and working.

Uploaded by

voyav37617
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)
24 views18 pages

Aim: - Source Code:: Program:-1

The document contains source code for implementing various sorting algorithms in C programming language. It includes code for binary search, insertion sort, selection sort, bubble sort, stack operations, quick sort and merge sort. The code is written by Ishu and includes the necessary header files, functions for implementing the algorithms, main function with input/output operations, and comments explaining the purpose and working.

Uploaded by

voyav37617
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/ 18

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];

printf("Enter number of elements: ");


scanf("%d",&n);
printf("Enter %d integers: ", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find: ");
scanf("%d", &key);

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>

void swap(int *xp, int *yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void selectionSort(int arr[], int n)

int i, j, min_idx;

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

min_idx = i;

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

if (arr[j] < arr[min_idx])

min_idx = j;

if(min_idx != i)

swap(&arr[min_idx], &arr[i]);

void printArray(int arr[], int size)


{

int i;

for (i=0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

int main()

int arr[] = {63, 24, 12, 22, 11};

int n = sizeof(arr)/sizeof(arr[0]);

selectionSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

printf("\n");

printf("Code is written by Ishu");

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];

printf("How many elements are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);
printf("\n Code is written by Ishu");
return 0;
}

• Output:
PROGRAM:-5

• Aim:
Write a program for Merge sort.

• Source code:
#include <stdio.h>
#include <stdlib.h>

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];

for (i = 0; i < n1; i++)


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

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++;
}

while (i < n1)


{
arr[k] = L[i];
i++;
k++;
}

while (j < n2)


{
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)


{
if (l < r)
{
int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

void printArray(int A[], int size)


{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

int main()
{
int arr[] = {22, 11, 23, 5, 8, 7, 97, 84, 35, 58,};
int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");


printArray(arr, arr_size);
printf("Code is written by Ishu");
return 0;
}
• Output:

You might also like