Todays Agenda - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Todays Agenda

Description:

typedef enum { A1=1, A2, B1, B2, C1, C2, D1, D2 } Group; #define NAME_LEN 50 ... After creating a main in testSort.c. gcc o sortX testSort.c insort.o compareX.o ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 27
Provided by: discovery5
Category:
Tags: agenda | enum | todays

less

Transcript and Presenter's Notes

Title: Todays Agenda


1
Todays Agenda
  • Tuple Data
  • Searching / Sorting

2
Tuple Types - Motivation
  • Recall Drama Club Example
  • List of Member elements
  • Member element represented as unsigned int
  • Professor-in-charge wants an update
  • Each Member element will contain an ID, a Name,
    and a Group.
  • What should be the new representation?

3
Tuple types-Drama club ..
  • / file dramaClubMember.h /
  • typedef unsigned int ID
  • typedef enum A11, A2, B1, B2, C1, C2, D1, D2
    Group
  • define NAME_LEN 50
  • typedef struct
  • ID i
  • char nNAME_LEN
  • Group g Member

4
Drama Club Revisited
  • Interfaces for the Drama Club operations depend
    on Member
  • Types for the Drama Club list depend on Member

5
Drama Club Revisited Interfaces
/ file dramaClubOps.h /
include dramaClubList.h
extern ListSize add(Member m, List ms, ListSize
n)
extern ListSize delete(Member m, List ms,
ListSize n)
extern ListSize isMember(Member m, List ms,
ListSize n)
// No change needed in the interface so far!
6
Drama Club Revisited Types
/ file dramaClubList.h /
include dramaClubMember.h
typedef unsigned int ListSize define MAX
1000 // List is an array of at most MAX elements
of type Member typedef Member ListMAX

// Type definitions re-structured to use new
Member
7
Drama Club Revisited - Implementation
/ file dramaClubOps.c / include
dramaClubOps.h
ListSize add(Member m, List ms, ListSize
n) // Implementation re-structured to
use new Member
for (pos0 pos lt n pos) if
(compare(mspos, m) GREATER) break if
(compare(mspos, m) EQUAL) return
8
Drama Club Revisited - Implementation
  • Function compare has been abstracted!
  • Need to define compare so that each member is
    uniquely determined
  • Notion of a key or index needed.
  • ID is the key in our example it uniquely
    determines a Member element in the list.

9
Drama Club Revisited - Implementation
  • / file compare.h /
  • include dramaClubMember.h
  • typedef enum
  • LESS -1, EQUAL 0, GREATER 1 ORDER
  • / Pre-condition
  • m1 and m2 can be ordered by a key field.
  • Post-condition
  • return EQUAL if keys are equal,
  • return LESS if keys are in order,
  • return GREATER if keys are out of order.
  • /
  • extern ORDER compare(Member m1, Member m2)

10
Drama Club Revisited - Implementation
  • / file compare.c /
  • include dramaClubMember.h
  • ORDER compare(Member m1, Member m2)
  • if (m1.i m2.i) return EQUAL
  • else if (m1.i lt m2.i) return LESS
  • else return GREATER

11
Drama Club Revisited Changes!?
  • Prof.-in-charge wants following
  • First degree or Higher Degree
  • PS or TS
  • No problem!
  • Redefine Member
  • Member ID x Name x Group x Degree x PSTS
  • where
  • Degree FIRST, HIGH
  • PSTS PS, TS
  • No other changes needed!

12
Drama Club Revisited Changes!?
  • Prof.-in-charge wants ID to be year-wise
  • Year and Year-wise ID
  • i.e. ID is unique given a Year value
  • To determine a Member element uniquely you need
    Year and ID values.
  • No problem!
  • Redefine Member
  • Redefine compare

13
Drama Club Revisited Changes!?
/ file dramaClubMember.h
typedef unsigned int ID typedef unsigned int
Year typedef enum A11, A2, B1, B2, C1, C2,
D1, D2 Group typedef enum FIRST1, HIGH2
Degree typedef enum PS1, TS2 PSTS define
NAME_LEN 50 typedef struct Year y ID i
char nNAME_LEN Degree d Group g PSTS p
Member
14
Drama Club Revisited Changes!?
/ file compare.c / include dramaClubMember.h
ORDER compare(Member m1, Member m2) if (m1.y
m2.y) if (m1.i m2.i) return
EQUAL else if (m1.i lt m2.i) return
LESS else return GREATER else if
(m1.y lt m2.y) return LESS else return GREATER
15
Drama Club Revisited Changes!?
  • Prof.-in-charge wants Dual-degree info. To be
    captured!?
  • Think!

16
Course Agenda
  • Module 1 Personal Software Process ?
  • Module 2 Data Driven Programming
  • Data Abstraction ?
  • Linear Collections Lists and Sets ?
  • Tuple Data ?
  • Searching and Sorting

17
Ordered Search
  • Recall Drama-club list - Implemented as ordered
    list.
  • Search is efficient i.e. O(logN) time using
    ordered list.
  • compare function is used to abstract key
    information
  • int compare(Member a, Member b)
  • if (a.y b.y) if (a.i lt b.i) return

18
Ordered Search
  • How do get ordered lists?
  • E.g. Professor-in-charge wants list ordered by
    Year and then ID today.
  • And tomorrow the requirement changes she wants
    it ordered by Group and then Name.
  • We need an algorithm to be designed
  • Ordering also known as Sorting!

19
Sorting
  • Big idea
  • Inserting an element into a sorted list in the
    appropriate position retains the order.
  • So what?
  • Start with a singleton list sorted trivially.
  • Repeatedly insert elements one at a time
    while keeping it sorted.
  • Leads to sorting technique known as Insertion
    Sort

20
Insertion Sort Top Down Design
Sort list A0 to AN-1
Insert AN-1 into sorted list
Sort list A0 to AN-2
Sort list A0 to AN-3
Insert AN-2 into sorted list

21
Insertion Sort Top Down Design
  • Termination
  • Sort list A0 Nothing to be done!
  • Problem decomposed into 2 modules
  • function for insertion
  • void insert(Member m, Member ms, unsigned int
    size)
  • function for repeated insertion
  • void insertSort(Member ms, unsinged int size)

22
Algorithm for Insertion
  • void insert(Member m, Member ms, unsigned int
    size)
  • j 0
  • for each j from 0 to size-1
  • compare msj with m
  • if (compare(msj,m)GREATER)
    break
  • for each k from size-1 to j
  • msk1 msk
  • msj m

//Loop Invariant 0 lt i lt j implies msj lt m
//Loop Invariant msk1 is empty
23
Algorithm for Insertion Sort
  • void insertionSort(Member ms, unsigned int
    size)
  • // Correctness Assertion ms0 to mssize-1 is
    ordered.

//Loop Invariant ms0 to msj-1 is ordered j
1 while j lt size-1
insert(msj, ms, j) j j 1

24
Insertion Sort
  • Complexity
  • Insertion of a single element O(j) where j is
    size of list.
  • Insertion Sorting Insert single element into
    lists from size 1 to size-1
  • 1 2 (size 1) O(size2)

25
Implementation of Insertion Sort
  • / file insort.h/
  • // Interface for insertionSort function only
  • / file insort.c /
  • // Implementations for insertionSort and insert
  • / file compare.h /
  • //Interface for compare function
  • / file compare.c /
  • // Implementation for compare function

26
Implementation of Insertion Sort
  • After creating insort.c
  • gcc c insort.c
  • After creating each compareX.c (with same compare
    function)
  • gcc c compareX.c
  • After creating a main in testSort.c
  • gcco sortX testSort.c insort.o compareX.o
  • // for each compareX.o separately
Write a Comment
User Comments (0)
About PowerShow.com