Implement
Linear Search
#include
#include
void main()
{
int array[10];
int i, num, keynum, found = 0;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%dn", array[i]);
}
printf("Enter the element to be searched \n");
scanf("%d", &keynum);
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array\n");
else
printf("Element is not present in the array\n");
}
Output
Enter the value of num
5
Enter the elements one by one
456
78
90
40
100
Input array is
456
78
90
40
100
Enter the element to be searched
70
Element is not present in the array
Enter the value of num
7
Enter the elements one by one
45
56
89
56
90
23
10
Input array is
45
56
89
56
90
23
10
Enter the element to be searched
45
Element is present in the array
Implement
Selection Sort Method using Functions
#include
#include
int findmax(int b[10], int k);
void exchang(int b[10], int k);
void main()
{
int array[10];
int i, j, n, temp;
clrscr();
printf("Enter the value of n \n");
scanf("%d", &n);
printf("Enter the elements one by one \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < n ; i++)
{
printf("%d\n", array[i]);
}
exchang(array, n);
printf("Sorted array is...\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
}
int findmax(int b[10], int k)
{
int max = 0, j;
for (j = 1; j <= k; j++)
{
if (b[j] > b[max])
{
max = j;
}
}
return(max);
}
void exchang(int b[10], int k)
{
int temp, big, j;
for (j = k - 1; j >= 1; j--)
{
big = findmax(b, j);
temp = b[big];
b[big] = b[j];
b[j] = temp;
}
return;
}
Output
Enter the value of n
4
Enter the elements one by one
57
90
34
78
Input array elements
57
90
34
78
Sorted array is...
34
57
78
90
Implement Insertion Sort
#include
#include
#define max 7
void insertion_sort(int *);
void main()
{
int a[max], i;
clrscr();
printf("enter elements to be sorted:");
for (i = 0;i < max;i++)
{
scanf("%d", &a[i]);
}
insertion_sort(a);
printf("sorted elements:\n");
for (i = 0;i < max; i++)
{
printf(" %d", a[i]);
}
}
void insertion_sort(int * x)
{
int temp, i, j;
for (i = 1;i < max;i++)
{
temp = x[i];
j = i - 1;
while (temp < x[j] && j >= 0)
{
x[j + 1] = x[j];
j = j - 1;
}
x[j + 1] = temp;
}
}
Output
Enter elements to be sorted:8 2 4 9 3 6 1
sorted elements:
1 2 3 4 6 8 9
Enter elements to be sorted:1 2 3 4 5 6 7
sorted elements:
1 2 3 4 5 6 7
Enter elements to be sorted:7 6 5 4 3 2 1
sorted elements:
1 2 3 4 5 6 7
Implement
Postman Sort Algorithm
#include
#include
void arrange(int,int);
int array[100], array1[100];
int i, j, temp, max, count, maxdigits = 0, c = 0;
void main()
{
int t1, t2, k, t, n = 1;
clrscr();
printf("Enter size of array :");
scanf("%d", &count);
printf("Enter elements into array :");
for (i = 0; i < count; i++)
{
scanf("%d", &array[i]);
array1[i] = array[i];
}
for (i = 0; i < count; i++)
{
t = array[i];
while(t > 0)
{
c++;
t = t / 10;
}
if (maxdigits < c)
maxdigits = c;
c = 0;
}
while(--maxdigits)
n = n * 10;
for (i = 0; i < count; i++)
{
max = array[i] / n;
t = i;
for (j = i + 1; j < count;j++)
{
if (max > (array[j] / n))
{
max = array[j] / n;
t = j;
}
}
temp = array1[t];
array1[t] = array1[i];
array1[i] = temp;
temp = array[t];
array[t] = array[i];
array[i] = temp;
}
while (n >= 1)
{
for (i = 0; i < count;)
{
t1 = array[i] / n;
for (j = i + 1; t1 == (array[j] / n); j++);
arrange(i, j);
i = j;
}
n = n / 10;
}
printf("\nSorted Array (Postman sort) :");
for (i = 0; i < count; i++)
printf("%d ", array1[i]);
printf("\n");
}
void arrange(int k,int n)
{
for (i = k; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (array1[i] > array1[j])
{
temp = array1[i];
array1[i] = array1[j];
array1[j] = temp;
temp = (array[i] % 10);
array[i] = (array[j] % 10);
array[j] = temp;
}
}
}
}
Output
Enter size of array :8
Enter elements into array :170
45
90
75
802
24
2
66
Sorted Array (Postman sort) :2 24 45 66 75 90 170 802
Enter size of array :7
Enter elements into array :25
64
185
136
36
3645
45
Sorted Array (Postman sort) :25 36 45 64 136 185 3645
Enter size of array :8
Enter elements into array :15
214
166
0836
98
6254
73
642
Sorted Array (Postman sort) :15 73 98 166 214 642 836 6254
No comments:
Post a Comment