Vivek
Vivek
Vivek
Program 1
Objective:-
C program to sort an array using bubble sort?
Code:-
#include <stdio.h>
int main()
{
int n;
printf("Enter number of elements :");
scanf("%d", &n);
int arr[n], temp;
printf("Enter elements :");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
PAGENO-2
}
printf("Sorted array in ascending order :");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array in descending order :");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:-
Enter number of elements :4
Enter elements :2
4
3
1
Sorted array in ascending order :1 2 3 4
Sorted array in descending order :4 3 2 1
PAGENO-3
Program 2
Objective:-
C program to sort an array using selection sort?
Code:
#include <stdio.h>
int main()
{
int n, temp;
int i, j;
printf("Enter the no of elements in array:");
scanf("%d", &n);
int a[10];
printf("Enter the array elements:");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i <n; i++)
{
for (j = i+1; j <n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
PAGENO-4
}
}
}
printf("The sorted array element in Ascending Order:");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
Program 3
Objective:-
C program to sort an array using insertion sort?
Code:-
#include <stdio.h>
#define n 10
int main()
{
int a[n], temp = 0;
int i, j, k;
temp = a[i];
j = i - 1;
while (j >= 0 && a[j] > temp)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}
printf("\nthe sorted array is as : ");
for (i = 0; i < k; i++)
{
printf("%d", a[i]);
printf(" ");
}
}
Output:-
Enter the size of array : 4
Enter the elements of array : 2
6
8
4
The entered array is as: 2 6 8 4
the sorted array is as : 2 4 6 8
PAGENO-7
Program 4
Objective:-
C program to perform all operation in an array.
Code:-
#include <stdio.h>
void insert(int arr[], int n);
int sort(int arr[], int n);
int search(int arr[], int n, int x);
void del(int arr[], int n, int a);
int main()
{
int n, index, x, y, p, u;
char op;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter operation to perform :c for searching,s for
sorting,d for deletion,i for inserting:");
fflush(stdin);
scanf("%c", &op);
switch (op)
{
case 's':
PAGENO-8
sort(arr, n);
break;
case 'c':
printf("Your array entered array is :\n");
for (int i = 0; i < n; i++){
printf("%d ", arr[i]);
}
printf("\nEnter element to search:");
scanf("%d", &x);
index = search(arr, n, x);
if (index != -1)
{
printf("\nElement %d found at index %d.\n", x, index);
}
else
{
printf("\nElement %d not found in the array.\n", x);
}
break;
case 'd':
printf("Your array entered array is :\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\nEnter index of element to delete:");
scanf("%d", &y);
p = sizeof(arr) / sizeof(arr[0]);
if (0 <= y && y <= p)
{
del(arr, n, y);
printf("\nElement at index %d successfully deleted!", y);
}
else
PAGENO-9
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] < temp)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
printf("Sorted array in descending order:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
// Code for searching
int search(int arr[], int n, int x)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
{
return i;
}
}
return -1;
}
// Code for deleting
void del(int arr[], int n, int a)
{
for (int i = a; i < n - 1; i++)
{
arr[i] = arr[i + 1];
P A G E N O - 11
}
n--;
printf("Array after deletion of index %d:\n", a);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
// inserting element in the array at specific position
void insert(int arr[], int n)
{
int data, pos;
printf("Enter number to be inserted: ");
scanf("%d", &data);
printf("Enter position where you want to insert this element:
");
scanf("%d", &pos);
if (pos < 1 || pos > n + 1)
{
printf("Invalid position!\n");
}
else
{
for (int i = n - 1; i >= pos - 1; i--)
{
arr[i + 1] = arr[i];
}
arr[pos - 1] = data;
}
}
Output:-
For Searching elements.
Enter the number of elements: 4
Enter 4 integers:
10 12 9 15
Enter operation to perform :c for searching,s for sorting,d for deletion,i for inserting:c
Your array entered array is :
10 12 9 15
Enter element to search:9
Element 9 found at index
2.
13 5 16 9 10