Category: Data Structures
Data Structures
Tree Traversal, In-order, Pre-order, Post-order
C program to implement quicksort algorithm
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first; i=first; j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last) i++;
while(number[j]>number[pivot]) j–;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, n, number[25];
printf(“How many elements are u going to enter?: “);
scanf(“%d”,&n);
printf(“Enter %d elements: “, n);
for(i=0;i<n;i++)
scanf(“%d”,&number[i]);
quicksort(number,0,n-1);
printf(“Order of Sorted elements: “);
for(i=0;i<n;i++)
printf(” %d”,number[i]);
return 0;
}
C program to implement binary search
//Binary Search
#include<stdio.h>
int main()
{
int beg,end,mid,item,n,data[20];
printf(“\nPlease Enter Number of elements in an array\n”);
scanf(“%d”, &n);
for(int i = 0; i <n; ++i)
{
scanf(“%d”, &data[i]);
}
printf(“Enter the item to search\n”);
scanf(“%d”,&item);
beg=0;
end=n;
mid=((beg+end)/2);
while((beg<=end)&&(data[mid]!=item))
{
if(data[mid]>item)
end=mid-1;
else
beg=mid+1;
mid=((beg+end)/2);
}
if(data[mid]==item)
printf(“\nBinary search successfully found searching item %d in location %d”,item,mid);
else
printf(“\nData is not in the array”);
return 0;
}
C program to find sum of two matrices of size 3×3
/* C program to find sum of two matrices of size 3×3 */
#include <stdio.h>
int main()
{
int A[3][3];
int B[3][3];
int C[3][3];
int row, col;
/* Input elements in first matrix*/
printf(“Enter elements in matrix A of size 3×3: \n”);
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf(“%d”, &A[row][col]);
}
}
/* Input elements in second matrix */
printf(“\nEnter elements in matrix B of size 3×3: \n”);
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf(“%d”, &B[row][col]);
}
}
/* C= A +B */
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
C[row][col] = A[row][col] + B[row][col];
}
}
/* Print the value of resultant matrix C */
printf(“\nSum of matrices A+B = \n”);
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
printf(“%d “, C[row][col]);
}
printf(“\n”);
}
return 0;
}
C program to implement linear search
#include<stdio.h>
int main()
{
int item,data[7]={11,13,14,10,54,12,20},loc=-1,k;
printf(“searching item=”);
scanf(“%d”,&item);
for(k=0;k<7;k++)
{
if (data[k]==item)
{
loc=k+1;
break;}
}
if (loc>=0)
printf(“Search is successful and data is found in %d location”,loc);
else
printf(“search is unsuccessful”);
return 0;
}
C program to implement matrix multiplication
#include<stdio.h>
//matrix multiplication with demo data
int main()
{
int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2]={{10,11},{20,21},{30,31}};
int c[2][2]={0};
int i,j,k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<2;i++)
{for(j=0;j<2;j++)
{printf(” %d”,c[i][j]);}
printf(“\n”);
}
}
C Program to Delete an Element from an Array
C Program to Insert an item into an Array
C program to implement bubble sort
#include<stdio.h>
int main()
{
int temp,data[8]={10,13,14,21,54,12,20,65},pass=1,k=0;
printf(“Before sorting we had\n”);
for(k=0;k<8;k++)
printf(” %d”,data[k]);
for(pass=1;pass<8;pass++)
{
for(k=0;k<(8-pass);k++)
{
if(data[k]>data[k+1])
{
temp=data[k];
data[k]=data[k+1];
data[k+1]=temp;
}
}
}
printf(“\nAfter sorting in ascending order we have\n”);
for(k=0;k<8;k++)
printf(” %d”,data[k]);
return 0;
}
Recent Comments