Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Vivek

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

PAGENO-1

NAME: VIVEK BELWAL


COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392339

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

NAME: VIVEK BELWAL


COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392339

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

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;
}
}
}
printf("\nThe sorted array element in Descending Order:");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
Output:-
Enter the no of elements in
array:4 Enter the array elements:3
2
4
1
The sorted array element in Ascending Order:1 2 3 4
The sorted array element in Descending Order:4 3 2 1
PAGENO-5

NAME: VIVEK BELWAL


COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392339

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;

// taking no of rows and coloumn as an input from user.


printf("Enter the size of array : ");
scanf("%d", &k);
// Taking input for elements form user.
printf("Enter the elements of array : ");
for (i = 0; i < k; i++)
{
scanf("%d", &a[i]);
}
printf("The entered array is as: ");
for (i = 0; i < k; i++)
{
printf("%d ", a[i]);
}

for (i = 1; i < k; i++)


{
PAGENO-6

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

NAME: VIVEK BELWAL


COURSE: BCA
SEC: C
SEMESTER: 2
ROLL NO: 2392339

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

printf("Enter index in range!");


break;
case 'i':
insert(arr, n);
break;
default:
printf("Choose correct option!\n");
break;
}
return 0;
}
// Code for sorting
int sort(int arr[], int n)
{
int temp, j;
for (int i = 1; i < n; i++)
{
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 ascending order:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
for (int i = 1; i < n; i++)
{
P A G E N O - 10

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;

printf("\nNew Array After Insertion:\n");


for (int i = 0; i <= n; i++)
{
printf("%d ", arr[i]);
}
P A G E N O - 12

}
}
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.

For Sorting elements.


Enter the number of elements: 4
Enter 4 integers:
21 19 25 15
Enter operation to perform :c for searching,s for sorting,d for deletion,i for inserting:s
Sorted array in ascending order:
15 19 21 25
Sorted array in descending order:
25 21 19 15

For Deleting elements.


Enter the number of elements: 4
Enter 4 integers:
10 6 8 3
Enter operation to perform :c for searching,s for sorting,d for deletion,i for inserting:d
Your array entered array is :
10 6 8 3
Enter index of element to
delete:3 Array after deletion of
index 3:
10 6 8

For Inserting elements.


Enter the number of elements: 4
Enter 4 integers:
13 16 9 10
Enter operation to perform :c for searching,s for sorting,d for deletion,i for inserting:i
Enter number to be inserted: 5
Enter position where you want to insert this element: 2
New Array After Insertion:
P A G E N O - 13

13 5 16 9 10

You might also like