wisdomatrix.com

Avatar

C Program to Linear Search in an Sorted Array

In a linear search, you iterate through each element of an array until you find the target element or reach the end of the array. In the case of a sorted array, you can optimize the search process by stopping the search when you find an element greater than the target, as it implies the target won’t be found in the remaining elements. Here’s a simple example of a C program for linear search in a sorted array Program : #include <stdio.h> #include <conio.h>   void main( ) { int arr[10]; int i,n,num ; printf(“Enter the number of Elements : “); scanf(“%d”,&n); for(i=0;i<n;i++) { printf(“Element%d : “,i+1); scanf(“%d”,&arr[i]); } printf ( “Enter number to search: ” ) ; scanf ( “%d”, &num ) ;   for ( i = 1 ; i <= n ; i++ ) { if ( arr[n] < num || arr[i] >= num ) { if ( arr[i] == num ) printf ( “The number is at position %d in the array.”, i ) ; else printf ( “Number is not present in the array.” ) ; break ; } }   getch( ) ; } Output :

C Program to Linear Search in an Sorted Array Read More »

C Program to Linear Search in an Unsorted Array

This program first takes the size of the array as input from the user, then takes the elements of the array, and finally, takes the element to be searched. The linearSearch function is then called to perform the linear search, and the result is displayed in the main function. Note that linear search is a simple search algorithm with a time complexity of O(n) for an array of size n. It sequentially checks each element of the array until a match is found or the entire array is traversed. Program : #include <stdio.h> #include <conio.h>   void main( ) { int arr[100];; int i, num, n; printf(“Enter the number of Elements : “); scanf(“%d”,&n); for(i=0;i<n;i++) { printf(“Element%d : “,i+1); scanf(“%d”,&arr[i]); } printf ( “Enter number to search: ” ) ; scanf ( “%d”, &num ) ;   for ( i = 1 ; i <= n ; i++ ) { if ( arr[i] == num ) break ; }   if ( i == n ) printf ( “Number is not present in the array.” ) ; else printf ( “The number is at position %d in the array.”, i ) ;   getch( ) ; } Output :

C Program to Linear Search in an Unsorted Array Read More »

C Program to Implement Bucket or Radix Sort

Bucket sort and radix sort are sorting algorithms that are often used for distributing elements into buckets or bins based on certain characteristics of the elements. Here, I’ll provide a simple C program to implement radix sort, as it is more commonly used for integer sorting. Program : #include <stdio.h> int print(int *a, int n) {int i;for (i = 0; i < n; i++)printf(“%d\t”, a[i]);} void radix_sort(int *a, int n) {int i, b[10], m = 0, exp = 1;for (i = 0; i < n; i++) {if (a[i] > m)m = a[i];} while (m / exp > 0) {int box[10] = { 0 };for (i = 0; i < n; i++)box[a[i] / exp % 10]++;for (i = 1; i < 10; i++)box[i] += box[i – 1];for (i = n – 1; i >= 0; i–)b[–box[a[i] / exp % 10]] = a[i];for (i = 0; i < n; i++)a[i] = b[i];exp *= 10;}} int main() {int arr[10];int i, num; printf(“Input number of elements: “);scanf(“%d”, &num); printf(“\nInput array elements one by one : “);for (i = 0; i < num; i++)scanf(“%d”, &arr[i]); printf(“\nArray elements : “);print(&arr[0], num); radix_sort(&arr[0], num); printf(“\nSorted elements : “);print(&arr[0], num); return 0;} Output :

C Program to Implement Bucket or Radix Sort Read More »

C Program to Implement Bubble Sort of Given Numbers

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Here’s a simple C program to implement Bubble Sort Program : #include<stdio.h>void bubble_sort(long[],long);int main(){long arr[100],n,c,d,swap;printf(“Enter number of element : “);scanf(“%ld”,&n);printf(“Enter %ld Longegers : \n”,n);for(c=0;c<n;c++){scanf(“%ld”,&arr[c]);}bubble_sort(arr,n);printf(“Sorted listin ascending order : \n”);for(c=0;c<n;c++){printf(“%ld\n”,arr[c]);}return 0;}void bubble_sort(long list[], long n){long c,d,t;for(c=0;c<(n-1);c++){for(d=0;d<n-c-1;d++){if(list[d]>list[d+1]){t=list[d];list[d]=list[d+1];list[d+1]=t;}}}} Ouput :

C Program to Implement Bubble Sort of Given Numbers Read More »

C Program to Implement Shell Sort of a Given Numbers

Sure, I can provide you with a simple implementation of the Shell Sort algorithm in C. Shell Sort is an in-place comparison-based sorting algorithm that sorts elements far apart from each other and then progressively reduces the gap between elements to be compared. Here’s a basic implementation in C Program : Program : #include<stdio.h>#include<conio.h>void shellsort(int a[],int n){int j,i,m,mid;for(m=n/2;m>0;m/=2){for(j=m;j<n;j++){for(i=j-m;i>=0;i-=m){if(a[i+m]>=a[i])break;else{mid=a[i];a[i]=a[i+m];a[i+m]=mid;}}}}}void main(){int a[10],i,n;printf(“Enter the number of Elements : “);scanf(“%d”,&n);for(i=0;i<n;i++){printf(“Element%d : “,i+1);scanf(“%d”,&a[i]);}printf(“\nArray before sorting :”);for(i=0;i<n;i++){printf(“\t%d”,a[i]);}shellsort(a,n);printf(“\nArray after sorting : “);for(i=0;i<n;i++){printf(“\t%d”,a[i]);}getch();} Output :

C Program to Implement Shell Sort of a Given Numbers Read More »

C Program to Implement Heap Sort of a Given Numbers

Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure to build a min-heap or max-heap. The basic idea of the algorithm is to repeatedly remove the root of the heap (which is the minimum element in a min-heap or the maximum element in a max-heap) and swap it with the last element of the heap. After each removal, the heap property is restored by heapifying the remaining elements. Here’s a simple implementation of Heap Sort in C Program : #include <stdio.h>int main(){int arr[10], no, i, j, c, heap_root, temp;printf(“Input number of elements: “);scanf(“%d”, &no);printf(“\nInput array values one by one : “);for (i = 0; i < no; i++)scanf(“%d”, &arr[i]);for (i = 1; i < no; i++){c = i;do{heap_root = (c – 1) / 2;if (arr[heap_root] < arr[c]){temp = arr[heap_root];arr[heap_root] = arr[c];arr[c] = temp;}c = heap_root;} while (c != 0);}printf(“Heap array : “);for (i = 0; i < no; i++)printf(“%d\t “, arr[i]);for (j = no – 1; j >= 0; j–){temp = arr[0];arr[0] = arr[j];arr[j] = temp;heap_root = 0;do{c = 2 * heap_root + 1;if ((arr[c] < arr[c + 1]) && c < j-1)c++;if (arr[heap_root]<arr[c] && c<j){temp = arr[heap_root];arr[heap_root] = arr[c];arr[c] = temp;}heap_root = c;} while (c < j);} printf(“\nSorted array : “);for (i = 0; i < no; i++)printf(“%d\t”, arr[i]);printf(“\n”);} Output :

C Program to Implement Heap Sort of a Given Numbers Read More »

C program to Implement Selection Sort .

Selection Sort is a simple sorting algorithm that repeatedly selects the smallest (or largest, depending on the order) element from the unsorted part of the array and swaps it with the element at the beginning of the unsorted part. The algorithm divides the array into a sorted and an unsorted region. In each iteration, the smallest element from the unsorted region is selected and moved to the sorted region. Here’s a simple implementation of Selection Sort in the C programming language Program : #include<stdio.h>main(){int a[20],n,temp,i,j;printf(“Enter the Number to terms : “);scanf(“%d”,&n);printf(“\nEnter the Elements of the Array : “);for(i=1;i<=n;i++){scanf(“%d”,&a[i]);}for(i=1;i<=n-1;i++)for(j=i+1;j<=n;j++)if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}printf(“The Ascending Order list is : “);for(i=1;i<=n;i++)printf(“\n%d”,a[i]);} Output :

C program to Implement Selection Sort . Read More »

C Program to Use Trigonometric Functions

C programming, you can use trigonometric functions from the <math.h> header to perform various trigonometric calculations. Here’s a simple program that demonstrates the use of some common trigonometric functions. sin( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=sin(x);printf(“The sin() of %lf is %lf\n”,x,result);return 0;} Output : cos( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=cos(x);printf(“The cosine() of %lf is %lf\n”,x,result);return 0;} Output : tan( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=tan(x);printf(“The tan of %lf is %lf\n”,x,result);return 0;} Output : abs( ) : #include<stdio.h>#include<math.h>int main(){int result,x;printf(“Enter the Value : “);scanf(“%d”,&x);result=abs(x);printf(“The Absolute value of %d is %d\n”,x,result);return 0;} Output : sqrt( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=sqrt(x);printf(“The square root of %lf is %lf.\n”,x,result);return 0;} Output : log( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=log(x);printf(“The natural log of %lf is %lf.\n”,x,result);return 0;} Output :

C Program to Use Trigonometric Functions Read More »

Shopping Basket
Verified by MonsterInsights