2d Example Insertion Sort
2d Example Insertion Sort
#include <stdio.h>
#include <conio.h>
struct node
{
int number;
struct node *next;
};
int main(void)
{
struct node *current = NULL;
struct node *next = NULL;
int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};
int i = 0;
return 0;
}
if(head == NULL) {
head = (struct node *)malloc(sizeof(struct node *));
head->next = NULL;
}
one = head;
two = head->next;
one->next = temp;
temp->next = two;
}
A bubblesort routine
# include
# include
void bubblesort(int array[],int size);
void main()
{
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);
}
void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i
for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
#include <stdio.h>
#include <conio.h>
int m1,n1,m2,n2,i,j,k,z[10][10]={0};
}
void mat_mul(int a,int b,int arr[][10],int brr[][10])
int k=0;
for(i=0;i<a;i++)
for(j=0;j<b;j++)
z[i][j]+=arr[i][j]+brr[i][j];
printf(“%d\t”,z[i][j]);
printf(“\n\n”);
int main()
int A[10][10]={0},B[10][10]={0};
scanf(“%d%d”,&m1,&n1);
printf(“Enter the column and row of second matrix(m x n)\n”);
scanf(“%d%d”,&m2,&n2);
printf(“\n\n”);
if (n1==m1||n2==m2)
value_sub(m1,n1,A);
printf(“\n\n”);
value_sub(m2,n2,B);
printf(“\n\n”);
mat_mul(m1,n2,A,B);
else
getch();
AREA OF CIRCLE
#include
void main ()
float r,c;
clrscr();
scanf ("%f",&r);
c=3.14*r*r;
main()
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}
return 0;
}
Binary search
#define TRUE 0
#define FALSE 1
int main(void)
{
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int left = 0;
int right = 10;
int middle = 0;
int number = 0;
int bsearch = FALSE;
int i = 0;
printf("ARRAY: ");
for(i = 1; i <= 10; i++)
printf("[%d] ", i);
printf("\nSearch for Number: ");
scanf("%d", &number);
if(number == array[middle])
{
bsearch = TRUE;
printf("** Number Found **\n");
} else {
if(number < array[middle]) right = middle - 1;
if(number > array[middle]) left = middle + 1;
}
}
if(bsearch == FALSE)
printf("-- Number Not found --\n");
return 0;
}
struct lnode
{
int data;
struct lnode *next;
} *head, *visit;
int main(void) {
/* linked list */
struct lnode *newnode = NULL;
int i = 0; /* a general counter */
head = newnode;
printf("Before bubble sort:\n");
llist_print();
printf("After bubble sort:\n");
llist_bubble_sort();
llist_print();
return 0;
}
tmp = *q;
while(visit != NULL)
{
printf("%d ", visit->data);
visit = visit->next;
}
printf("\n");
}
/*
// the `c' node precedes the `a' and `e' node
// pointing up the node to which the comparisons
// are being made.
*/
while(e != head->next)
{
c = a = head;
b = a->next;
while(a != e)
{
if(a->data > b->data)
{
if(a == head)
{
tmp = b -> next;
b->next = a;
a->next = tmp;
head = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else
{
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}
bubble sort
#include <stdio.h>
int main(void) {
int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
int i = 0;
printf("before:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");
bubble_sort(arr, 10);
printf("after:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
size -= 1;
#define MAX 50
#define N 2000
int main(void)
{
char word[MAX];
char *x[N];
int n = 0;
int i = 0;
n = i;
sort_words(x, n);
for(i = 0; i < n; ++i)
printf("%s\n", x[i]);
return(0);
}
tmp = *p;
*p = *q;
*q = tmp;
}
Note: Output is not in the form of table and rate is taken as 2%. It calculates amount of each year
#include <stdio.h>
#include <conio.h>
void main()
{
int t=1;
int r=2;
int y;
int y1=0;
long int p,a;
float i1;
double total;;
clrscr();
printf("enter starting amount&year");
scanf("%ld""%d",&p,&y);
while(y1<2009) i1="(p*r*t)/100;" total="i1+a+p;" p="p+a;" style="color: rgb(255, 0, 0);">