3.
Write a program on Selection sort
#include <stdio.h>
int main( )
{
int arr[50], num, i, j, temp,min;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
printf("Please Enter the Value of Elements: ");
for(i = 0; i < num; i++)
scanf("%d", &arr[i]);
for(i = 0; i < num - 1; i++)
{
min = i;
for( j=i+1; j<num; j++ )
{
if(arr[j]<arr[min])
{
min=j;
}
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
printf("the elements after sorting are:");
for(i=0;i< num; i++)
printf("%d\t",arr[i]);
return 0;
}
OUTPUT:
Please Enter the Number of Elements you want in the array: 5
Please Enter the Value of Elements: 9 5 -2 7 3
the elements after sorting are: -2 3 5 7 9
4. Write a program on insertion sort
#include <stdio.h>
int main( )
{
int arr[50], num, i, j, key;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
printf("Please Enter the Value of Elements: ");
for(i = 0; i < num; i++)
scanf("%d", &arr[i]);
for(i = 1; i < num; i++)
{
key = arr[i];
j = i – 1;
while (j >= 0 && arr[j] >key )
{
arr[j+1] = arr[j];
j = j-1;
}
a[j+1] = key;
}
Printf(“Stored elements are: “);
For(i=0 ; i<num ; i++)
Printf(“%d”, arr[i]);
Return 0;
}
OUTPUT:
Please Enter the Number of Elements you want in the array: 5
Please Enter the Value of Elements: 9 5 -2 7 3
Sorted elements are: -2 3 5 7 9
5. Write a program on bubble sort
#include <stdio.h>
int main( )
{
int arr[50], num, x, y, temp;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
printf("Please Enter the Value of Elements: ");
for(x = 0; x < num; x++)
scanf("%d", &arr[x]);
for(x = 0; x < num - 1; x++)
{
for(y = 0; y < num - x - 1; y++)
{
if(arr[y] >arr[y + 1])
{
temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
printf("Array after implementing bubble sort: ");
for(x = 0; x < num; x++)
printf("%d ", arr[x]);
return 0;
}
OUTPUT :Please Enter the Number of Elements you want in the array: 5
Please Enter the Value of Elements: 9 5 -2 7 3
Array after implementing bubble sort: -2 3 5 7 9
6. Implement a program to implement quick sort using recursion.
#include <stdio.h>
// Function to swap two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Partition function to choose a pivot element and rearrange the array
int partition(int arr[], int low, int high) {
// Choose the rightmost element as the pivot
int pivot = arr[high];
int i = (low - 1); // index of the smaller element
// Rearranging elements based on comparison with the pivot
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
// Swap the pivot element with the element at i+1 so pivot is in correct
place
swap(&arr[i + 1], &arr[high]);
// Return the index of the pivot element
return (i + 1);
}
// Quick Sort function (recursive)
void quickSort(int arr[], int low, int high) {
if (low < high) {
// Find the pivot element
int pi = partition(arr, low, high);
// Recursively sort elements before and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {10, 80, 30, 90, 40, 50, 70};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
// Perform Quick Sort
quickSort(arr, 0, n - 1);
printf("\nSorted array: \n");
printArray(arr, n);
return 0;
}
OUTPUT :
Original array:
10 80 30 90 40 50 70
Sorted array:
10 30 40 50 70 80 90
7. Write a program on linear search
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
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\n");
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.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
OUTPUT: Enter number of elements in array
5
Enter 5 numbers
7
9
10
6
8
Enter the number to search10
10 is present at location 2
8. Write a program on binary search
#include <stdio.h>
intmain()
{
inti, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while(low <= high) {
if(array[mid] < key)
low = mid + 1;
elseif(array[mid] == key) {
printf("%d found at location %d.n", 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.n", key);
return0;
}
OUTPUT:Enter number of elements in array
5
Enter 5 numbers
7
9
10
6
8
Enter the number to search 10
10 s present at location 2
[Link] a program to implement stacks using arrays.
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Maximum size of the stack
// Stack structure
struct Stack {
int arr[MAX]; // Array to store stack elements
int top; // Top index of the stack
};
// Function to initialize the stack
void initStack(struct Stack* stack) {
stack->top = -1; // Set the stack as empty
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX - 1; // Stack is full if top is at MAX-1
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1; // Stack is empty if top is -1
}
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow! Cannot push %d\n", value);
} else {
stack->arr[++stack->top] = value; // Increment top and insert value
printf("%d pushed to stack\n", value);
}
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow! Cannot pop\n");
return -1; // Return -1 for underflow condition
} else {
int poppedValue = stack->arr[stack->top--]; // Get the top value and
decrement top
return poppedValue;
}
}
// Function to get the top element without popping
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
return -1; // Return -1 if the stack is empty
} else {
return stack->arr[stack->top]; // Return the top element
}
}
// Function to display the stack elements
void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
} else {
printf("Stack elements: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}
}
int main() {
struct Stack stack;
initStack(&stack);
// Demonstrate stack operations
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
push(&stack, 50);
display(&stack);
printf("Popped element: %d\n", pop(&stack));
display(&stack);
printf("Top element is: %d\n", peek(&stack));
push(&stack, 60);
display(&stack);
return 0;
}
OUTPUT :
10 pushed to stack
20 pushed to stack
30 pushed to stack
40 pushed to stack
50 pushed to stack
Stack elements: 10 20 30 40 50
Popped element: 50
Stack elements: 10 20 30 40
Top element is: 40
60 pushed to stack
Stack elements: 10 20 30 40 60