Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Bubblesort -By C programming

Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparision sort. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms are better.

Program:
# include<stdio.h>
# include<conio.h>
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;
}
}

2d example insertion sort

Insertion sort is a simple sorting algorithm: a comparision sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
  • Simple implementation
  • Efficient for (quite) small data sets
  • Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
  • More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
  • Stable; i.e., does not change the relative order of elements with equal keys
  • In-place; i.e., only requires a constant amount O(1) of additional memory space
  • Online; i.e., can sort a list as it receives it
When humans manually sort something (for example, a deck of playing cards), most use a method that is similar to insertion sort.

Program:
#include <stdio.h>
#include <conio.h>
struct node
{
int number;
struct node *next;
};
struct node *head = NULL;
/* insert a node directly at the right place in the linked list*/
void insert_node(int value);
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;

/* insert some numbers into the linked list */
for(i = 0; i < i =" 0;">next != NULL)
{
printf("%4d\t%4d\n", test[i++], head->number);
head = head->next;
}

/* free the list */
for(current = head; current != NULL; current = next)
next = current->next, free(current);

return 0;
}

void insert_node(int value)
{
struct node *temp = NULL;
struct node *one = NULL;
struct node *two = NULL;

if(head == NULL) {
head = (struct node *)malloc(sizeof(struct node *));
head->next = NULL;
}

one = head;
two = head->next;

temp = (struct node *)malloc(sizeof(struct node *));
temp->number = value;

while(two != NULL && temp->number <>number) {
one = one->next;
two = two->next;
}

one->next = temp;
temp->next = two;
}

Convert Decimal to Hexadecimal Number

The decimal (base ten) numeral system has ten possible values (0,1,2,3,4,5,6,7,8, or 9) for each place-value. In contrast, the hexadecimal (base sixteen) numeral system has sixteen possible values, using the letters A, B, C, D, E, and F for the six place-values after 9.
To avoid confusion while using different numeral systems, the base of each individual number may be specified by writing it as a subscript of the number. For example, the decimal number 512 may be written as 51210 and read as "five hundred and twelve, base ten". The hexadecimal number 512 may be specified as "base sixteen" by writing it as 51216 (which is 1298 when converted to decimal).

program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
Int x, y=30, z;
clrscr();
printf(“Enter the number:”);
scanf(“%d”, &x);
printf(“\n conversion of decimal to hexadecimal number\n”);
for(;;)
{
if(x= =0)
exit(1);
z=x%16;
x=x/16;
gotoxy(y--,5);
switch(z)
{
Case 10:
Printf(“A”);
Break;
Case 11:
Printf(“%c”, „B?);
Break;
Case 12:
Printf(%c”, „C”);
Break;
Case 13:
Printf(“D”);
Break;
Case 14:
Printf(“E”);
Break;
Case 15:
Printf(“F”);
Default:
Printf(“%d”, z);
}
}
getch();
}

Output:
Enter the number: 31
Conversion of decimal to Hexa decimal number
1F

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More