1 / 13

Today’s Agenda

Today’s Agenda. Sorting. Insertion Sort – Top Down Design. Sort list: A[0] to A[N-1]. Sort list: A[0] to A[N-2]. Insert A[N-1] into sorted list. Sort list: A[0] to A[N-3]. Insert A[N-2] into sorted list. …. Sort list: A[0]. Insert A[1] into sorted list.

Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda Sorting

  2. Insertion Sort – Top Down Design Sort list: A[0] to A[N-1] Sort list: A[0] to A[N-2] Insert A[N-1] into sorted list Sort list: A[0] to A[N-3] Insert A[N-2] into sorted list … Sort list: A[0] Insert A[1] into sorted list

  3. int insert(int res[],int x,int size) { int i=0, j, k, flag=0; if(size == 0) { res[0] = x; size = size + 1; } else { while(i< size) { if(res[i] < x || res[i] == x) i++; else { j = i; flag = 1; break; } } if(flag == 1) { for(k=size;k>j;k--) res[k] = res[k-1]; res[k] = x; } else res[i] = x; size = size + 1; } return size; }

  4. #define MAX 20 int main() { int a[10] = {50,16,20,35,18,8,14,41,3,39}; int res[MAX]; int i,size=0; size = insertInOrder(res,a[i],size); for(i=0;i<size;i++) printf("%d ",res[i]); printf("\n"); }

  5. Programming Exercise on data driven programming

  6. Consider a police department containing N police officers. Each police officer contains the following information in his record. 1. An unsigned int id representing the unique identification number of the police. 2. A character array of maximum 30 characters for the name. 3. An unsigned int sc for the number of cases solved 4. An unsigned int ac for the number of cases attended 5. An unsigned int age for storing the age of the police 6. An unsigned int jyr to store the joining year. • A police department is a list containing maximum of MAX_POL number of police officers where MAX_POL is 1000.

  7. readPoliceDept() • rank() • countPoliceOnRank() • countPoliceService() • sortPoliceDept() • comparePolice() • printPoliceDept()

  8. Operations (1) rank() • The rank of police officer is computed as follows. • rank = (100 * solvedCases / attendedCases) • if attendedCases = 0, rank = 0;

  9. comparePolice() • compares police p1 and p2 based on rank, if rank is same then compare based on id .

  10. PoliceDef.h #define NAME_LEN 50 // Maximum length of Name #define MAX_POL 1024 // Max number of Police in one Dept typedef unsigned int ID; // Unique Police identification number typedef char NAME[NAME_LEN]; // Name of Police typedef unsigned int solvedCases; // Number of cases solved typedef unsigned int attendedCases; // Total no. of cases attended typedef unsigned int AGE; // Police officer's Age typedef unsigned int joiningYear; // Police officer's joining year typedef unsigned int noOfPolice; // number of Police officers in Dept

  11. PoliceDef.h typedef enum { GREATER = 1, EQUAL = 0, LESSER = -1 } ORDER; typedef struct { ID id; NAME name; solvedCases sc; attendedCases ac; AGE age; joiningYear jyr; } police; // The police structure typedef police policeDept[MAX_POL]; // the policeDept

  12. PoliceOps.h • Extern void readPoliceDept(policeDeptpdt, noOfPolice N); • Extern float rank(police p); • Extern int countPoliceOnRank (policeDept pdt, noOfPolice N); • Count the number of police who has rank more 75. • Extern int countPoliceService(policeDept pdt, noOfPolice N); • Count the number of police who has completed 25 years of service. • Extern order comparePolice(policep1,police p2); • Extern void sortPoliceDept(policeDept pdt, noOfPolice N); • Sort police based on their ranks. • Extern void printPoliceDept(policeDept pdt, noOfPolice N);

  13. comparePolice() ORDER comparePolice (police p1, police p2) { if(rank(p1)<rank(p2)) return LESSER; else if(rank(p1) == rank(p2)) { if(p1.id < p2.id) return LESSER; else if(p1.id == p2.id) return EQUAL; else return GREATER; } else return GREATER; }

More Related