A ‘C’ program to sort the records of ‘n’ employees on key employee-number using insertion sort method.
Insertion Sort Source code
#include#include typedef struct employee { char name[20]; int empid, salary; } emp; void display(emp m[20], int n) { int i; printf(“\nEMPLOYEE ID\ tNAME\ tSALARY”); for (i = 0; i < n; i++) { printf(“\n % d\ t\ t % s\ t\ t % d”, m[i].empid, m[i].name, m[i].salary); } } void insertion(emp m[20], int n) { int i, j; emp temp; for (i = 1; i < n; i++) { j = i - 1; temp = m[i]; while (strcmp(m[j].empid, temp.empid) < 0 && j >= 0) { m[j + 1] = m[j]; j–; } m[j + 1] = temp; // display(m,n); } display(m, n); } void main() { emp m[20]; int n, i, ch; clrscr(); printf(“\nenter the limit: ”); scanf(“ % d”, & n); printf(“\ngive employee id, name and salary: ”); for (i = 0; i < n; i++) { scanf(“ % d % s % d”, & m[i].empid, m[i].name, & m[i].salary); } display(m, n); insertion(m, n); getch(); }
Output
enter the limit:2
give employee id,name and salary:102 shilpa 700
100 priti 900
EMPLOYEE ID NAME SALARY
102 shilpa 700
100 priti 900
EMPLOYEE ID NAME SALARY
100 priti 900
102 shilpa 700
0 comments:
Post a Comment