Sorting, ADTs, Linked Lists
No New Material This Week <ul><li>Lets write a practice program... </li></ul><ul><ul><li>make a personT struct with name, ...
Sorting <ul><li>Sort  = arrange data according to its values </li></ul><ul><li>Closely related to the search problem. </li...
Sorting <ul><li>Many sorting algorithms;  We will discuss only two simple methods: </li></ul><ul><ul><li>selection sort </...
Selection Sort <ul><li>Core Idea:  </li></ul><ul><ul><li>Find  the item in list that  should  be first, </li></ul></ul><ul...
Selection Sort <ul><li>Find  smallest item in unsorted part of list  </li></ul><ul><li>Swap  it with first item in the uns...
Selection sort Example:  small-to-large sort
Selection Sort: Example Code <ul><li>void SelSort(int lst[], int siz) </li></ul><ul><li>{ </li></ul><ul><li>int j, k, it, ...
Insertion Sort <ul><li>Core Idea:   </li></ul><ul><li>As before, a  ‘wall’  separates list into  sorted  and  unsorted par...
Insertion Sort Remove Insert
Insertion sort Example (1 st  unsorted item is automatially the 1 st  sorted item)
Abstract Data Types:  ADT s <ul><li>(Recall)  ‘data type’  determines  the set of all possible values for a variable.  </l...
Abstract Data Types:  ADT s <ul><li>An  ADT  is defined by BOTH  </li></ul><ul><ul><li>its structure, and </li></ul></ul><...
ADTs: How to do it <ul><li>The best ADTs are easy to use:  they ‘hide the details’ from user with  well-chosen functions a...
ADTs ADT Data structure  Operations Interface Users (you, and other  project programmers)
ADT Examples <ul><li>lists (sequential order, but easy insert/remove) </li></ul><ul><li>queues (first-come, first-served s...
An ADT: Singly Linked List <ul><li>A  list  is a sequential data collection  without any intrinsic numbering. </li></ul><u...
ADT: Linked Lists <ul><li>What defines a linked list? </li></ul><ul><li>Nodes have pointers to ‘link’ them together in a l...
ADT: Singly Linked Lists <ul><li>Each node of a singly linked list contains: </li></ul><ul><ul><li>a node value  (could be...
ADT: Singly Linked Lists <ul><li>How can we define a node structure?  Simple Example: </li></ul><ul><li>typedef struct nod...
ADT: Singly Linked Lists <ul><li>Be sure you understand this: </li></ul><ul><li>typedef struct GoodNode  </li></ul><ul><li...
Singly Linked Lists <ul><li>What does this program create? </li></ul><ul><li>int main(void)  </li></ul><ul><li>{ </li></ul...
ADT: Singly Linked Lists <ul><li>Typical interface functions you will need to write for a singly linked list ADT: </li></u...
Singly Linked Lists <ul><ul><li>To attach  a newly-created node  at  pNew    right after the  pNow  node: </li></ul></ul><...
Singly Linked Lists <ul><ul><li>To attach  a newly-created node  at  pNew    right after the  pNow  node: </li></ul></ul><...
Singly Linked Lists <ul><ul><li>To attach  a newly-created node  at  pNew    right after the  pNow  node: </li></ul></ul><...
Upcoming SlideShare
Loading in...5
×

Sorting & Linked Lists

8,624

Published on

presentation

Published in: Education, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,624
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
184
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Sorting & Linked Lists

  1. 1. Sorting, ADTs, Linked Lists
  2. 2. No New Material This Week <ul><li>Lets write a practice program... </li></ul><ul><ul><li>make a personT struct with name, ID# </li></ul></ul><ul><ul><li>make some functions: (make stubs first) </li></ul></ul><ul><ul><ul><li>newPerson(personT *nu) – ask user for her info... </li></ul></ul></ul><ul><ul><ul><li>swap(personT *a, personT *b); exchange contents </li></ul></ul></ul><ul><ul><ul><li>isSorted(personT *a, personT *b) TRUE if person 'a' is before 'b' alphabetically </li></ul></ul></ul><ul><li>Gather entries in an array. Can we sort it? </li></ul>
  3. 3. Sorting <ul><li>Sort = arrange data according to its values </li></ul><ul><li>Closely related to the search problem. </li></ul><ul><li>Example: </li></ul><ul><ul><li>arrange numbers in largest-to-smallest order </li></ul></ul><ul><ul><li>arrange names in alphabetical order </li></ul></ul><ul><ul><li>arrange struct s based on a 'key' field (e.g. the .name field of the workerT structure we used in binary search) </li></ul></ul>
  4. 4. Sorting <ul><li>Many sorting algorithms; We will discuss only two simple methods: </li></ul><ul><ul><li>selection sort </li></ul></ul><ul><ul><li>insertion sort </li></ul></ul><ul><li>Demonstrate sorting items in arrays BUT sorts within dynamically-allocated memory are far more common. </li></ul>
  5. 5. Selection Sort <ul><li>Core Idea: </li></ul><ul><ul><li>Find the item in list that should be first, </li></ul></ul><ul><ul><ul><li>How? for small-to-large ordering? large-to-small? </li></ul></ul></ul><ul><ul><li>Swap it with the actual first item in the list </li></ul></ul><ul><ul><ul><li>remember the swap function? How did it work? </li></ul></ul></ul><ul><ul><li>Move a ‘wall’ or marker to separate the list into sorted and unsorted parts </li></ul></ul><ul><ul><li>Repeat on the unsorted part of the list until it disappears </li></ul></ul>
  6. 6. Selection Sort <ul><li>Find smallest item in unsorted part of list </li></ul><ul><li>Swap it with first item in the unsorted part of the list </li></ul><ul><li>Move the ‘wall’ forward by one item, past sorted item </li></ul><ul><li>Repeat until the unsorted part of the list vanishes </li></ul>Example: small-to-large sort 0
  7. 7. Selection sort Example: small-to-large sort
  8. 8. Selection Sort: Example Code <ul><li>void SelSort(int lst[], int siz) </li></ul><ul><li>{ </li></ul><ul><li>int j, k, it, tmp; /* sorted: 0  j unsorted: j+1  siz-1 smallest unsorted: it */ for(j=0; j<siz; j++) /* for all items on list */ { it = j; /* (test item j first) */ for(k=j+1; k<siz; k++) /* Search for min. item: */ { if(lst[k] < lst[it]) /*is ‘it’ smallest? */ { it = k; /* no—keep the smaller ‘k’ */ } } /* Now ‘it’ is the smallest unsorted item. Swap ‘it’ and ‘j’ items*/ tmp = lst[j]; lst[j] = lst[it]; lst[it] = lst[tmp]; } } </li></ul>
  9. 9. Insertion Sort <ul><li>Core Idea: </li></ul><ul><li>As before, a ‘wall’ separates list into sorted and unsorted parts (sorted part is empty at first). </li></ul><ul><li>Find the 1 st unsorted item (just past the ‘wall’) </li></ul><ul><li>Search sorted list for 1 st item’s proper position, </li></ul><ul><li>Swap 1 st item: Remove from the unsorted list, and Insert into sorted list. </li></ul><ul><li>Repeat until unsorted list is empty. </li></ul>
  10. 10. Insertion Sort Remove Insert
  11. 11. Insertion sort Example (1 st unsorted item is automatially the 1 st sorted item)
  12. 12. Abstract Data Types: ADT s <ul><li>(Recall) ‘data type’ determines the set of all possible values for a variable. </li></ul><ul><li>Data types defined by C language syntax: </li></ul><ul><ul><li>basic data types: int, char, float, double </li></ul></ul><ul><ul><li>derived data types: arrays, pointers, structs, and their constructions (e.g. array of int, pointer to float) </li></ul></ul><ul><li>But what is this ‘daisy chain’? </li></ul><ul><ul><li>it is made of dynamically-allocated structs and pointers </li></ul></ul><ul><ul><li>It is a data type defined by the way you use it, an </li></ul></ul><ul><li>A bstract D ata T ype (ADT) </li></ul>
  13. 13. Abstract Data Types: ADT s <ul><li>An ADT is defined by BOTH </li></ul><ul><ul><li>its structure, and </li></ul></ul><ul><ul><li>a set of operations (functions) that control it. </li></ul></ul><ul><li>Structure alone isn’t enough. EXAMPLE: </li></ul><ul><ul><li>Setting pointers properly is required for this ‘daisy chain’ to work as sorted/unsorted list </li></ul></ul>k c e f s v g q pBgn pEnd pSortEnd NULL
  14. 14. ADTs: How to do it <ul><li>The best ADTs are easy to use: they ‘hide the details’ from user with well-chosen functions and structures. </li></ul><ul><li>Users don’t want (or need) to know the details; Give them simple ‘interface’ functions to do the needed tasks (e.g. void insert(pHere,pThis); void remove(pThis); ) </li></ul><ul><li>Good strategy for large programming projects: If the function prototypes do not change, then (e.g. ‘the interface’, or .h file) then the function’s author can improve, repair, upgrade it without changing any programs/code that use it! </li></ul>
  15. 15. ADTs ADT Data structure Operations Interface Users (you, and other project programmers)
  16. 16. ADT Examples <ul><li>lists (sequential order, but easy insert/remove) </li></ul><ul><li>queues (first-come, first-served structures) </li></ul><ul><li>stacks (last-in, first-out; like a stack of paper) </li></ul><ul><li>dynamic sets (a collection of objects, no ordering) </li></ul><ul><li>graphs (trees, networks of connected nodes) </li></ul><ul><li>‘ Interface’ functions often include: </li></ul><ul><ul><li>“ create new item,” </li></ul></ul><ul><ul><li>“ delete/remove this item,” </li></ul></ul><ul><ul><li>“ get next item,” </li></ul></ul><ul><ul><li>“ push an item onto stack,” </li></ul></ul><ul><ul><li>“ pop an item off of the stack,” etc. </li></ul></ul>
  17. 17. An ADT: Singly Linked List <ul><li>A list is a sequential data collection without any intrinsic numbering. </li></ul><ul><ul><li>Does NOT require sequential memory locations </li></ul></ul><ul><ul><li>Much more general, flexible than array </li></ul></ul><ul><li>Each list item can have different type(!) </li></ul><ul><li>CS Jargon: a ‘ node ’ is an item on a linked list </li></ul>k c e f s v g q pBgn pEnd pSortEnd NULL
  18. 18. ADT: Linked Lists <ul><li>What defines a linked list? </li></ul><ul><li>Nodes have pointers to ‘link’ them together in a linear, chain-like arrangement. </li></ul><ul><li>A list is linear : each node has exactly one ‘parent’ and exactly one ‘child’ </li></ul><ul><li>Changing pointers makes a list dynamic: </li></ul><ul><ul><li>the number of nodes can change, AND </li></ul></ul><ul><ul><li>the arrangement of nodes can change </li></ul></ul>
  19. 19. ADT: Singly Linked Lists <ul><li>Each node of a singly linked list contains: </li></ul><ul><ul><li>a node value (could be a complex struct ...) </li></ul></ul><ul><ul><li>a pointer to the next node. </li></ul></ul><ul><li>The last node on the list points to NULL ( tail ). </li></ul><ul><li>We also need a pointer for the list start ( head ). </li></ul><ul><li>Singly linked lists are one-directional; must always move from head to tail </li></ul>head 8 3 12 1 NULL node node node node
  20. 20. ADT: Singly Linked Lists <ul><li>How can we define a node structure? Simple Example: </li></ul><ul><li>typedef struct node </li></ul><ul><li>{ </li></ul><ul><li>struct node *pNext; /*Pointer-to-node*/ </li></ul><ul><li>int value; </li></ul><ul><li>} nodeT; </li></ul>A little tricky here: Recall: data type is struct node , typedef renames it all as nodeT , thus member variable pNext is a pointer-to-nodeT-type-objects
  21. 21. ADT: Singly Linked Lists <ul><li>Be sure you understand this: </li></ul><ul><li>typedef struct GoodNode </li></ul><ul><li>{ </li></ul><ul><li>struct GoodNode *pNext; /*Pointer-to-node*/ </li></ul><ul><li>int value; </li></ul><ul><li>} nodeT; </li></ul><ul><li>typedef struct nodeT </li></ul><ul><li>{ </li></ul><ul><li>nodeT *pNext; /*Pointer-to-node*/ </li></ul><ul><li>int value; </li></ul><ul><li>} nodeT; </li></ul>This works! NO! THE OBVIOUS WAY DOESN’T WORK!
  22. 22. Singly Linked Lists <ul><li>What does this program create? </li></ul><ul><li>int main(void) </li></ul><ul><li>{ </li></ul><ul><li>nodeT *pHead, *pNode1; </li></ul><ul><li> </li></ul><ul><li>pNode1 = (nodeT*)malloc(1*sizeof(nodeT)); </li></ul><ul><li>pNode1–>pNext = NULL; </li></ul><ul><li> pNode1–>value = 10; </li></ul><ul><li>pHead = pNode1; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>10 NULL pHead
  23. 23. ADT: Singly Linked Lists <ul><li>Typical interface functions you will need to write for a singly linked list ADT: </li></ul><ul><ul><li>insert - add an element to the list </li></ul></ul><ul><ul><li>delete - remove an element from the list </li></ul></ul><ul><ul><li>isEmpty - find out if the list is empty </li></ul></ul><ul><ul><li>printList - traverse and display the list. </li></ul></ul><ul><li>How would you write the ‘isEmpty’ function? </li></ul><ul><li>The ‘insert’ function? </li></ul>
  24. 24. Singly Linked Lists <ul><ul><li>To attach a newly-created node at pNew right after the pNow node: </li></ul></ul><ul><ul><li>next ’ member of struct at pNew </li></ul></ul><ul><ul><ul><li>pNew–>pNext = pNow–>pNext; </li></ul></ul></ul><ul><ul><li>Then point pNow ’s next member to pNew . </li></ul></ul><ul><ul><ul><li>pNow–>next = pNew; </li></ul></ul></ul>8 12 1 NULL 10 pNew pHead pNow
  25. 25. Singly Linked Lists <ul><ul><li>To attach a newly-created node at pNew right after the pNow node: </li></ul></ul><ul><ul><li>First set the ` next ’ member of struct at pNew </li></ul></ul><ul><ul><ul><li>pNew–>pNext = pNow–>pNext; </li></ul></ul></ul><ul><ul><li>Then point pNow ’s next member to pNew . </li></ul></ul><ul><ul><ul><li>pNow–>next = pNew; </li></ul></ul></ul>8 12 1 NULL 10 pNew pHead pNow
  26. 26. Singly Linked Lists <ul><ul><li>To attach a newly-created node at pNew right after the pNow node: </li></ul></ul><ul><ul><li>First set the ` next ’ member of struct at pNew </li></ul></ul><ul><ul><ul><li>pNew–>pNext = pNow–>pNext; </li></ul></ul></ul><ul><ul><li>Then point pNow ’s next member to pNew . </li></ul></ul><ul><ul><ul><li>pNow–>next = pNew; </li></ul></ul></ul>8 12 1 NULL 10 pNew pHead pNow
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×