Sort the N
Names in an Alphabetical Order
#include
#include
#include
void main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;
clrscr();
printf("Enter the value of n \n");
scanf("%d", &n);
printf("Enter %d names n", \n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input NamestSorted names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("------------------------------------------\n");
}
Output
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
----------------------------------------
Input Names Sorted names
------------------------------------------
heap class
stack heap
queue object
object program
class project
program queue
project stack
------------------------------------------
Read an Array
and Search for an Element
#include
#include
void main()
{
int array[20];
int i, low, mid, high, key, size;
clrscr();
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the key\n");
scanf("%d", &key);
low = 0;
high = (size - 1);
while (low <= high)
{
mid = (low + high) / 2;
if (key == array[mid])
{
printf("Successful search\n");
return;
}
if (key < array[mid])
high = mid - 1;
else
low = mid + 1;
}
printf("Unsuccessful search\n");
}
Output
Enter the size of an array
4
Enter the array elements
90
560
300
390
Enter the key
90
Successful search
Enter the size of an array
4
Enter the array elements
100
500
580
470
Enter the key
300
Unsuccessful search
Implement Selection Sort
Recursively
#include
#include
void selection(int [], int, int, int, int);
int main()
{
int list[30], size, temp, i, j;
clrscr();
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Enter the elements in list:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
selection(list, 0, 0, size, 1);
printf("The sorted list in ascending order is\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
return 0;
}
void selection(int list[], int i, int j, int size, int flag)
{
int temp;
if (i < size - 1)
{
if (flag)
{
j = i + 1;
}
if (j < size)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
selection(list, i, j + 1, size, 0);
}
selection(list, i + 1, 0, size, 1);
}
}
Output
Enter the size of the list: 5
Enter the elements in list:
23
45
64
12
34
The sorted list in ascending order is
12 23 34 45 64
Input Few
Numbers & Perform Merge Sort on them using Recursion
#include
#include
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
clrscr();
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After merge sort:\n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}
Output
Enter total number of elements:5
Enter the elements:
12
36
22
76
54
After merge sort:
12 22 36 54 76
No comments:
Post a Comment