Upload files to "DS/C/Lab/Week1"
This commit is contained in:
parent
181f9113d2
commit
51cc29eb50
75
DS/C/Lab/Week1/binary.c
Normal file
75
DS/C/Lab/Week1/binary.c
Normal file
@ -0,0 +1,75 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a,c,n,i,temp;
|
||||
int flag=0;
|
||||
int arr[10];
|
||||
|
||||
printf("Enter the number of elements you want in the array: ");
|
||||
scanf("%d",&n);
|
||||
|
||||
printf("Enter the numbers you want in the array:\n");
|
||||
for(i=0;i<n;i++){
|
||||
scanf("%d",&arr[i]);
|
||||
}
|
||||
|
||||
printf("The numbers in the array are:\n");
|
||||
for(i=0;i<n;i++){
|
||||
printf("%d\n",arr[i]);
|
||||
}
|
||||
|
||||
//sorting
|
||||
int j;
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
if(arr[i]<arr[j])
|
||||
{
|
||||
temp = arr[i];
|
||||
arr[i]=arr[j];
|
||||
arr[j]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
printf("The numbers in the sorted array are:\n");
|
||||
for(i=0;i<n;i++){
|
||||
printf("%d\n",arr[i]);
|
||||
}
|
||||
|
||||
// binary search
|
||||
|
||||
printf("Enter the number you want to search: ");
|
||||
scanf("%d",&a);
|
||||
|
||||
int max = n-1;
|
||||
int min = 0;
|
||||
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
c=(max+min)/2;
|
||||
if(a==arr[c])
|
||||
{
|
||||
printf("The element is at position %d. \n",c);
|
||||
flag = 1;
|
||||
break;
|
||||
}
|
||||
if(a<arr[max] && a>arr[c])
|
||||
min=c;
|
||||
if(a<arr[c] && a>arr[min])
|
||||
max=c;
|
||||
}
|
||||
|
||||
if(flag){
|
||||
printf("The number has been found.\n");
|
||||
}else{
|
||||
printf("The number does not exist in this array.\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
39
DS/C/Lab/Week1/bubble.c
Normal file
39
DS/C/Lab/Week1/bubble.c
Normal file
@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n,i,j,temp;
|
||||
int flag=0;
|
||||
|
||||
printf("Enter the number of elements you want in the array: ");
|
||||
scanf("%d",&n);
|
||||
|
||||
int arr[n];
|
||||
|
||||
printf("Enter the numbers you want in the array:\n");
|
||||
for(i=0;i<n;i++){
|
||||
scanf("%d",&arr[i]);
|
||||
}
|
||||
|
||||
printf("The numbers in the array are:\n");
|
||||
for(i=0;i<n;i++){
|
||||
printf("%d\n",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("The numbers in the sorted array are:\n");
|
||||
for(i=0;i<n;i++){
|
||||
printf("%d\n",arr[i]);
|
||||
}
|
||||
}
|
||||
|
43
DS/C/Lab/Week1/insertionsort.c
Normal file
43
DS/C/Lab/Week1/insertionsort.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n, i, j, temp;
|
||||
int flag = 0;
|
||||
|
||||
printf("Enter the number of elements you want in the array: ");
|
||||
scanf("%d", &n);
|
||||
|
||||
int arr[n];
|
||||
|
||||
printf("Enter the numbers you want in the array:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
|
||||
printf("The numbers in the array are:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d\n", arr[i]);
|
||||
}
|
||||
|
||||
// sorting
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
int key = arr[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && arr[j] > key)
|
||||
{
|
||||
arr[j + 1] = arr[j];
|
||||
j = j - 1;
|
||||
}
|
||||
arr[j + 1] = key;
|
||||
}
|
||||
|
||||
printf("The numbers in the sorted array are:\n");
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d \n", arr[i]);
|
||||
}
|
||||
}
|
43
DS/C/Lab/Week1/linear.c
Normal file
43
DS/C/Lab/Week1/linear.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a,n,i;
|
||||
int flag=0;
|
||||
int arr[10];
|
||||
|
||||
printf("Enter the number of elements you want in the array: ");
|
||||
scanf("%d",&n);
|
||||
|
||||
printf("Enter the numbers you want in the array:\n");
|
||||
for(i=0;i<n;i++){
|
||||
scanf("%d",&arr[i]);
|
||||
}
|
||||
|
||||
printf("The numbers in the array are:\n");
|
||||
for(i=0;i<n;i++){
|
||||
printf("%d\n",arr[i]);
|
||||
}
|
||||
|
||||
printf("Enter the number you want to search: ");
|
||||
scanf("%d",&a);
|
||||
|
||||
//linear search
|
||||
|
||||
for(i=0;i<n;i++){
|
||||
if(arr[i]==a){
|
||||
flag=1;
|
||||
printf("\nThe desired element is at position %d.\n",i+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(flag){
|
||||
printf("The number has been found.\n");
|
||||
}else{
|
||||
printf("The number does not exist in this array.\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
42
DS/C/Lab/Week1/selectionsort.c
Normal file
42
DS/C/Lab/Week1/selectionsort.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n,i,j,temp;
|
||||
int flag=0;
|
||||
|
||||
printf("Enter the number of elements you want in the array: ");
|
||||
scanf("%d",&n);
|
||||
|
||||
int arr[n];
|
||||
|
||||
printf("Enter the numbers you want in the array:\n");
|
||||
for(i=0;i<n;i++){
|
||||
scanf("%d",&arr[i]);
|
||||
}
|
||||
|
||||
printf("The numbers in the array are:\n");
|
||||
for(i=0;i<n;i++){
|
||||
printf("%d\n",arr[i]);
|
||||
}
|
||||
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
if(arr[i]<arr[j])
|
||||
{
|
||||
temp = arr[i];
|
||||
arr[i]=arr[j];
|
||||
arr[j]=temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
printf("The numbers in the sorted array are:\n");
|
||||
for(i=0;i<n;i++){
|
||||
printf("%d\n",arr[i]);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user