C RPOGRAMING
ASSIGNMENT-1
NAME : K.VIJAYA KUMAR
ROLL NO : 25PC21
COURSE : M.SC-CS
DATE OF SUBMMISOIN : 15-09-2025
C-PROGRAM ASSIGNMENT
1.GIVE A LIST OF N ELEMENTS AND THE KEY FIND
WETHER THE KEY IS AVALIABLE IN LIST OR NOT?
🔹 1) Linear Search:
• Works on any list (sorted or unsorted).
• Checks each element one by one until it finds the key or reaches the
end.
Simple but slow for large data.
📘 Algorithm:
1. Read n (size of array).
2. Read n elements into the array.
3. Input the key to search.
4. Start from the first element → compare each element with key.
5. If found, print the index and stop.
6. If not found after the loop, print "Key not found".
CODE:
#include <stdio.h>
int main() {
int n, key, i, found = -1;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter key to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++) {
if (arr[i] == key) {
found = i;
break;
}
}
if (found != -1)
printf("Key found at index %d\n", found);
else
printf("Key not found\n");
return 0;
}
Out put:
Enter number of elements: 5
Enter 5 elements: 10 20 30 40 50
Enter key to search: 30
Key found at index 2
🔹 2) Binary Search:
• Works only on sorted arrays.
• Divides the array into halves, and checks the middle element.
• If the key is smaller, search left half; if bigger, search right half.
• Repeats until found or range is empty.
Much faster than Linear Search for large datasets.
📘 Algorithm:
1. Read n (size of array).
2. Read n elements into array in sorted order.
3. Input key to search.
4. Set low = 0, high = n-1.
5. Repeat until low <= high:
Find mid = (low + high) / 2.
If arr[mid] == key, print index and stop.
If arr[mid] < key, search right half (low = mid+1).
Else search left half (high = mid-1).
6. If not found, print "Key not found".
CODE:
#include <stdio.h>
int main() {
int n, key, i, low, high, mid, found = -1;
printf("Enter number of elements (in sorted order): ");
scanf("%d", &n);
int arr[n];
printf("Enter %d sorted elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter key to search: ");
scanf("%d", &key);
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
found = mid;
break;
}
else if (arr[mid] < key) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
if (found != -1)
printf("Key found at index %d\n", found);
else
printf("Key not found\n");
return 0;
}
Out put:
Enter number of elements (in sorted order): 6
Enter 6 sorted elements: 5 10 15 20 25 30
Enter key to search: 20
Key found at index 3
2.GIVE A LIST OF N ELEMENTS AND THE LIST IN SORRTED
ORDER.
🔹 1) Insertion Sort:
• Works like how you arrange cards in your hand while playing.
• Each element is picked and placed into its correct position among the
already sorted elements.
• Terms inside algorithm:
• Key → The element currently being placed in the correct position.
• Shifting → Moving elements one step to the right to make space
for the key.
📘 Algorithm:
• Read n elements into array.
• Start from second element (index = 1).
• Store it in key.
• Compare with previous elements, shifting them to the right if larger.
• Insert key in the correct position.
• Repeat for all elements until array is sorted.
CODE:
#include <stdio.h>
int main() {
int n, i, j, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
printf("Array after Insertion Sort: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Out put:
Enter number of elements: 5
Enter 5 elements: 9 3 7 1 5
Array after Insertion Sort: 1 3 5 7 9
🔹 2) Selection Sort:
• Finds the smallest element and places it at the correct position.
• Terms inside algorithm:
• MinIndex → Index of the smallest element in the unsorted part.
• Swap → Exchange two elements.
📘 Algorithm:
• Read n elements into array.
• For each position i, find the minimum element in the remaining part.
• Swap the minimum with arr[i].
• Repeat until array is sorted.
CODE:
#include <stdio.h>
int main() {
int n, i, j, minIndex, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
printf("Array after Selection Sort: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Out put:
Enter number of elements: 5
Enter 5 elements: 64 25 12 22 11
Array after Selection Sort: 11 12 22 25 64
🔹 3) Bubble Sort:
• Repeatedly compares adjacent elements and swaps them if they are
in the wrong order.
• The largest element “bubbles up” to the end in each pass.
• Terms inside algorithm:
• Pass → One complete traversal of the array.
• Swap → Exchange two adjacent elements.
📘 Algorithm:
1. Read n elements into array.
2. Repeat (n-1) times:
Compare adjacent elements.
Swap if they are in the wrong order.
3. After each pass, the largest element “bubbles up” to its correct
position.
CODE:
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++) {
for (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("Array after Bubble Sort: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Enter number of elements: 6
Enter 6 elements: 5 1 4 2 8 3
Array after Bubble Sort: 1 2 3 4 5 8