CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.
-
date post
20-Dec-2015 -
Category
Documents
-
view
220 -
download
3
Transcript of CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.
![Page 1: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/1.jpg)
CSE1303 Part ACSE1303 Part AData Structures and AlgorithmsData Structures and Algorithms
Lecture A17/18 – RevisionLecture A17/18 – Revision
![Page 2: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/2.jpg)
2
Lecture OverviewLecture Overview• Subject overview
– Topics covered this semester.
• Exam overview– Types of questions found in Part A of the exam.
• Exam hints and resources– How to deal with exams (resources, and hints
for during the exam).– How to prepare.
• Revision
![Page 3: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/3.jpg)
3
Subject OverviewSubject Overview• Basic C
– Data types
– 1D and multidimensional arrays
– Strings
– I/O & File I/O
– Structures and typedef– Dynamic memory
– Pointers
![Page 4: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/4.jpg)
4
Subject OverviewSubject Overview• ADTs
– Stacks• Array implementation
• Linked implementation
• Push
• Pop
• Initialise
• Check empty/full
![Page 5: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/5.jpg)
5
Subject OverviewSubject Overview• ADTs
– Queues• Array implementation
• Linked implementation
• Append
• Serve
• Initialise
• Check empty/full
![Page 6: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/6.jpg)
6
Subject OverviewSubject Overview• ADTs
– Singly linked list• Array implementation
• Linked implementation
• Insert
• Delete
• Search
• Initialise
• Check empty/full
• Traversal
![Page 7: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/7.jpg)
7
Subject OverviewSubject Overview• ADTs
– Doubly linked list• Linked implementation • Insert (not C code)
• Delete (not C code)
• Search (not C code)
• Initialise • Traversal (not C code)
![Page 8: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/8.jpg)
8
Subject OverviewSubject Overview• ADTs
– Trees• Parse Trees/Expression Trees
– Prefix/Infix/PostFix
• Binary Trees and Binary Search Trees– Insert
– Delete
– Search
– Initialise
– PreOrder, InOrder, PostOrder Traversal
![Page 9: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/9.jpg)
9
Subject OverviewSubject Overview• ADTs
– Hashtables• Hash function
• Insert
• Delete (chaining)
• Search
• Initialise
• Collision resolution (chaining, linear probing)
![Page 10: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/10.jpg)
10
Subject OverviewSubject Overview• Algorithms
– Searching• Linear search (arrays, lists, hashtable)
• Binary search (arrays)
– Recursion• Direct/Indirect
• Unary
• Binary
– Complexity
![Page 11: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/11.jpg)
11
Subject OverviewSubject Overview• Algorithms
– Sorting• Insertion sort (array)
• Selection sort (array)
• Binary Tree sort
• Mergesort (array)
• Quicksort (array)
![Page 12: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/12.jpg)
12
Exam OverviewExam Overview• 0.5 of the 3 hour exam is for Part A
• Typical types of questions:– Multiple choice– Short Answer
• Write a structure or a small piece of C
• Write a short answer to a question
– Long answer• Code a solution to a problem
• Write an algorithm for a problem
• Fill in diagrams/missing code
• We will go over the sample exam next lecture.We will go over the sample exam next lecture.
![Page 13: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/13.jpg)
13
Resources for ExamsResources for Exams• Monash Community services self-help information for exams:
– Exam Skills - Clue Words
– Exam Taking Techniques
– Exam Analysis
– Effective Skills in Examinations
– How to Survive Exam Weeks
– Preparing for Tests and Exams
– Final Exam Review Checklist
– Examination Room Techniques
– General Exam taking Hints
– How to keep Calm during Tests
– Test Anxiety Scale
• All these resource pages found at:– http://www.adm.monash.edu.au/commserv/counselling/selfhelp.html
![Page 14: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/14.jpg)
14
Exam Preparation HintsExam Preparation Hints• Revise all of the lecture notes• Do all of the tutorial questions• Revise and finish all of the pracs (you gain better understanding
doing the bonus questions)• Do the suggested reading at the end of all lectures• Do the suggested additional exercises in the tutorials• Attempt the practice exam• Revise your tests (look at them at the general office)• Try to implement the algorithms that you haven't already done in
the pracs• Prepare questions for the last tutorial
• Come with questions to consultation with the lecturersCome with questions to consultation with the lecturers (additional hours are advertised closer to the exam date)
![Page 15: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/15.jpg)
15
During the ExamDuring the Exam
• Read the questions carefully!Read the questions carefully!
• If you don't know what a question means, ask the lecturer.
• Don't get stuck on one question – move onto something else.
• Plan your time – don't spend it all on a couple of questions.
• Do the easy questions first.
• Attempt all questions.
• Don't use whiteout! (And don't erase your workings)
• Check you have answered all the questions.
![Page 16: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/16.jpg)
16
Revision – Linked ListsRevision – Linked Lists
• Operations:– Initialise– Set position (step to a position in the list)– Insert– Search– Delete– Make a new node
![Page 17: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/17.jpg)
17
void initialiseList(List* listPtr){ listPtr->headPtr = NULL; listPtr->count = 0;}
Initialise ListInitialise List
count
headPtr
0
NULL
listPtr addr of list
![Page 18: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/18.jpg)
18
Set PositionSet Position
headPtr
0 212 position
Node* setPosition(const List* listPtr, int position){ int i; Node* nodePtr = listPtr->headPtr;
if (position < 0 || position >= listPtr->count) { fprintf(stderr, “Invalid position\n”); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } } return nodePtr;}
![Page 19: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/19.jpg)
19
Set PositionSet Position
0 212 position
Node* setPosition(const List* listPtr, int position){ int i; Node* nodePtr = listPtr->headPtr;
if (position < 0 || position >= listPtr->count) { fprintf(stderr, “Invalid position\n”); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } } return nodePtr;}
nodePtr
headPtr
![Page 20: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/20.jpg)
20
Set PositionSet Position
0 212 position
Node* setPosition(const List* listPtr, int position){ int i; Node* nodePtr = listPtr->headPtr;
if (position < 0 || position >= listPtr->count) { fprintf(stderr, “Invalid position\n”); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } } return nodePtr;}
nodePtr
headPtr
![Page 21: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/21.jpg)
21
Set PositionSet Position
0 212 position
Node* setPosition(const List* listPtr, int position){ int i; Node* nodePtr = listPtr->headPtr;
if (position < 0 || position >= listPtr->count) { fprintf(stderr, “Invalid position\n”); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } } return nodePtr;}
nodePtr
headPtr
![Page 22: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/22.jpg)
22
Inserting – Start of ListInserting – Start of List
0x30a80x2008
0x2000
0position
0x2000newNodePtr
headPtr
![Page 23: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/23.jpg)
23
Inserting – Start of ListInserting – Start of List
0x30a80x2008
0x2000
0position
0x2000newNodePtr
headPtr
![Page 24: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/24.jpg)
24
Inserting – Start of ListInserting – Start of List
0x30a80x2008
0x2000
0position
0x2000newNodePtr
headPtr
![Page 25: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/25.jpg)
25
Inserting – Inside the ListInserting – Inside the List
0x2000
0position
0x2000newNodePtr
0x30500x3080
headPtr
![Page 26: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/26.jpg)
26
Inserting – Inside the ListInserting – Inside the List
0x2000
0position
0x2000newNodePtr
0x30500x3080
headPtr
![Page 27: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/27.jpg)
27
Inserting – Inside the ListInserting – Inside the List
0x2000
0position
0x2000newNodePtr
0x30500x3080
headPtr
![Page 28: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/28.jpg)
28
void insertItem(List* listPtr, float item, int position){ Node* newNodePtr = makeNode(item); Node* nodePtr = NULL;
if (position == 0) { newNodePtr->nextPtr = listPtr->headPtr; listPtr->headPtr = newNodePtr; } else { nodePtr = setPosition(listPtr, position-1); newNodePtr->nextPtr = nodePtr->nextPtr; nodePtr->nextPtr = newNodePtr; } listPtr->count++;}
![Page 29: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/29.jpg)
29
Deleting – 1Deleting – 1stst Node Node
0 position
0x4000 oldNodePtr
0x30a80x20300x4000
headPtr
![Page 30: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/30.jpg)
30
0 position
0x4000 oldNodePtr
0x30a80x20300x4000
Deleting – 1Deleting – 1stst Node Node
headPtr
![Page 31: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/31.jpg)
31
Head0 position
0x4000 oldNodePtr
0x30a80x2030
Deleting – 1Deleting – 1stst Node Node
![Page 32: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/32.jpg)
32
Deleting – Middle NodeDeleting – Middle Node
1 position
0x2030 oldNodePtr
0x30a80x20300x4000
0x4000 prevNodePtr
headPtr
![Page 33: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/33.jpg)
33
1 position
0x2030 oldNodePtr
0x30a80x20300x4000
Deleting – Middle NodeDeleting – Middle Node
0x4000 prevNodePtr
headPtr
![Page 34: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/34.jpg)
34
1 position
0x2030 oldNodePtr
0x30a80x4000
Deleting – Middle NodeDeleting – Middle Node
0x4000 prevNodePtr
headPtr
![Page 35: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/35.jpg)
35
void deleteNode(List* listPtr, int position){ Node* prevNodePtr = NULL; Node* oldNodePtr = NULL;
if (listPtr->count > 0 && position < listPtr->count) { if (position == 0) { oldNodePtr = listPtr->headPtr; listPtr->headPtr = nodePtr->nextPtr; } else { prevNodePtr = setPosition(listPtr, position - 1); oldNodePtr = prevNodePtr->nextPtr; prevNodePtr->nextPtr = oldNodePtr->nextPtr; } listPtr->count--; free(oldNodePtr); } else { fprintf(stderr, “List is empty or invalid position.\n”); exit(1); }}
![Page 36: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/36.jpg)
36
Doubly Linked ListDoubly Linked List
currentPtr
0 1 2 3 4
![Page 37: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/37.jpg)
37
struct DoubleLinkNodeRec{ float value; struct DoubleLinkNodeRec* nextPtr; struct DoubleLinkNodeRec* previousPtr;};
typedef struct DoubleLinkNodeRec Node;
struct DoubleLinkListRec{ int count; Node* currentPtr; int position;};
typedef struct DoubleLinkListRec DoubleLinkList;
![Page 38: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/38.jpg)
38
Insert at endInsert at end
currentPtrnewNodePtr
0x4000 0x3080 0x2030 0x2000
0x4000 0x3080
0x3080 0x2030 NULL
NULLNULL
NULL
0x2000prevNodePtr
0x2030
![Page 39: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/39.jpg)
39
Insert at endInsert at end
0x4000 0x3080 0x2030 0x2000
0x4000 0x3080
0x3080 0x2030 0x2000
NULLNULL
NULL
0x2000
0x2030
currentPtrnewNodePtr
prevNodePtr
![Page 40: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/40.jpg)
40
Insert at endInsert at end
0x4000 0x3080 0x2030 0x2000
0x4000 0x3080
0x3080 0x2030 0x2000
NULLNULL
0x2030
0x2000
0x2030
currentPtrnewNodePtr
prevNodePtr
![Page 41: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/41.jpg)
41
Insert inside the listInsert inside the list0x4000 0x3080 0x2030
0x2000
0x4000 0x3080
0x3080 0x2030NULL
NULL
NULL
NULL
0x2000
0x3080
currentPtr
newNodePtr
prevNodePtr
![Page 42: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/42.jpg)
42
Insert inside the listInsert inside the list0x4000 0x3080 0x2030
0x2000
0x4000 0x3080
0x3080 0x2030NULL
NULL
0x2030
NULL
0x2000
0x3080
currentPtr
newNodePtr
prevNodePtr
![Page 43: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/43.jpg)
43
Insert inside the listInsert inside the list0x4000 0x3080 0x2030
0x2000
0x4000 0x3080
0x3080 0x2030NULL
NULL
0x2030
0x3080
0x2000
0x3080
currentPtr
newNodePtr
prevNodePtr
![Page 44: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/44.jpg)
44
Insert inside the listInsert inside the list0x4000 0x3080 0x2030
0x2000
0x4000 0x2000
0x3080 0x2030NULL
NULL
0x2000
0x3080
currentPtr0x2030
0x3080newNodePtr
prevNodePtr
![Page 45: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/45.jpg)
45
Insert inside the listInsert inside the list0x4000 0x3080 0x2030
0x2000
0x4000 0x2000
0x3080 0x2000NULL
NULL
0x2000
0x3080
currentPtr0x2030
0x3080newNodePtr
prevNodePtr
![Page 46: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/46.jpg)
46
Delete from startDelete from start
0x4000 0x3080 0x2030
0x4000 0x3080
0x3080 0x2030 NULL
NULL
oldNodePtr 0x4000
currentPtr
![Page 47: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/47.jpg)
47
Delete from startDelete from start
0x4000 0x3080 0x2030
0x4000 0x3080
0x3080 NULL
NULL
0x4000
0x2030
currentPtr
oldNodePtr
![Page 48: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/48.jpg)
48
Delete from startDelete from start
0x3080
NULL
0x4000
0x2030
0x2030
0x3080
NULL
currentPtr
oldNodePtr
![Page 49: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/49.jpg)
49
Delete from inside listDelete from inside list
oldNodePtr
0x4000 0x3080 0x2030
0x4000 0x3080
0x3080 0x2030 NULL
NULL
0x3080currentPtr
![Page 50: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/50.jpg)
50
Delete from inside listDelete from inside list
0x4000 0x3080 0x2030
0x4000 0x3080
0x2030 0x2030 NULL
NULL
0x3080currentPtr
oldNodePtr
![Page 51: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/51.jpg)
51
Delete from inside listDelete from inside list
0x4000 0x3080 0x2030
0x4000 0x4000
0x2030 0x2030 NULL
NULL
0x3080currentPtr
oldNodePtr
![Page 52: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/52.jpg)
52
Delete from inside listDelete from inside list
0x4000 0x2030
0x4000
0x2030 NULL
NULL
0x3080currentPtr
oldNodePtr
![Page 53: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/53.jpg)
53
Revision – RecursionRevision – Recursion
- Unary- calls itself at most once
- Binary/N-ary - calls itself twice/N times
- Direct - the function calls itself
- Indirect- The function calls another function which calls the
first function again.
![Page 54: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/54.jpg)
54
Revision – RecursionRevision – Recursion- Always
- Converges to a base case (always)- Has a recursive definition- Has a base case
- Can remove recursion using a stack instead- Disadvantages
- May run slower- May use more space
- Advantages- Easier to prove correct- Easier to analyse
![Page 55: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/55.jpg)
55
Recursive - Free ListRecursive - Free ListnodePtr
/* Delete the entire list */void FreeList(Node* nodePtr){ if (nodePtr==NULL) return; FreeList(nodePtr->next); free(nodePtr);}
0x258a 0x4c680x2000
Stack in memory
0x2000
![Page 56: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/56.jpg)
56
Recursive - Free ListRecursive - Free List
0x258a 0x4c680x2000
0x258a
0x2000
Stack in memory
/* Delete the entire list */void FreeList(Node* nodePtr){ if (nodePtr==NULL) return; FreeList(nodePtr->next); free(nodePtr);}
nodePtr
![Page 57: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/57.jpg)
57
Recursive - Free ListRecursive - Free List
0x258a 0x4c680x2000
0x4c68
0x258a
0x2000
Stack in memory
/* Delete the entire list */void FreeList(Node* nodePtr){ if (nodePtr==NULL) return; FreeList(nodePtr->next); free(nodePtr);}
nodePtr
![Page 58: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/58.jpg)
58
Recursive - Free ListRecursive - Free List
0x258a 0x4c680x2000
NULL
0x4c68
0x258a
0x2000
Stack in memory
/* Delete the entire list */void FreeList(Node* nodePtr){ if (nodePtr==NULL) return; FreeList(nodePtr->next); free(nodePtr);}
nodePtr
![Page 59: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/59.jpg)
59
Recursive - Free ListRecursive - Free List
/* Delete the entire list */void FreeList(Node* nodePtr){ if (nodePtr==NULL) return; FreeList(nodePtr->next); free(nodePtr);}
0x258a 0x4c680x2000
0x4c68
0x258a
0x2000
Stack in memory
nodePtr
![Page 60: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/60.jpg)
60
Recursive - Free ListRecursive - Free List
0x258a0x2000
0x258a
0x2000
Stack in memory
/* Delete the entire list */void FreeList(Node* nodePtr){ if (nodePtr==NULL) return; FreeList(nodePtr->next); free(nodePtr);}
nodePtr
![Page 61: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/61.jpg)
61
Recursive - Free ListRecursive - Free List
0x2000
0x2000
Stack in memory
/* Delete the entire list */void FreeList(Node* nodePtr){ if (nodePtr==NULL) return; FreeList(nodePtr->next); free(nodePtr);}
nodePtr
![Page 62: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/62.jpg)
62
Revision - Binary TreesRevision - Binary Trees• Parent nodes always have 2 children• Expression Tree
– A Binary Tree built with operands and operators.– Also known as a parse tree.– Used in compilers.
• Binary Search Tree– Every node entry has a unique key.– All the keys in the left subtree of a node are less than the
key of the node.– All the keys in the right subtree of a node are greater than
the key of the node
![Page 63: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/63.jpg)
63
Revision - Binary TreesRevision - Binary Trees• Traversal
– PreOrder (Visit Left Right)– InOrder (Left Visit Right)– PostOrder (Left Right Visit)
![Page 64: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/64.jpg)
64
Example: Expression TreeExample: Expression Tree
/
+
/
1 3 *
6 7
4
1/3 + 6*7 / 4
1 / 3 + *6 7 / 4 Infix
/1 3 6 7 * 4 / + Postfix
+ / 1 3 / * 6 7 4 Prefix
![Page 65: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/65.jpg)
65
Revision – Binary Search TreesRevision – Binary Search Trees
• Operations:– Initialise– Insert– Search– Delete (not needed to be known for exam)– Make a new node– Traverse (inorder, preorder, postorder)
![Page 66: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/66.jpg)
66
Make a new nodeMake a new node• Steps:
– allocate memory for the new node
– put item into the new node
– set left and right branches to NULL
• returns: pointer to (i.e. address of) new node
newNodePtr NULL
0x2000newNodePtr
0x2000newNodePtr
NULL NULL
value 3.3
3.3
TreeNode* makeTreeNode(float value){ TreeNode* newNodePtr = NULL; newNodePtr = (TreeNode*)malloc(sizeof(TreeNode)); if (newNodePtr == NULL){ fprintf(stderr, “Out of memory\n”); exit(1); } else{ newNodePtr->key = value; newNodePtr->leftPtr = NULL; newNodePtr->rightPtr = NULL; } return newNodePtr;}
![Page 67: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/67.jpg)
67
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
InsertInsert1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 68: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/68.jpg)
68
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 69: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/69.jpg)
69
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 70: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/70.jpg)
70
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 71: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/71.jpg)
71
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
0.9
![Page 72: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/72.jpg)
72
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 73: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/73.jpg)
73
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 74: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/74.jpg)
74
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 75: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/75.jpg)
75
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 76: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/76.jpg)
76
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 77: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/77.jpg)
77
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 78: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/78.jpg)
78
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 79: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/79.jpg)
79
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 80: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/80.jpg)
80
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 81: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/81.jpg)
81
TraversalTraversal• Inorder traversal of a Binary Search Tree always gives the sorted order of the
keys. (left, visit, right)void printInorder(TreeNode* nodePtr){ if(nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f, nodePtr->key); printInorder(nodePtr->rightPtr); }}
• Preorder (visit, left, right):void printPreOrder(TreeNode* nodePtr){ if(nodePtr != NULL){ printf(“ %f, nodePtr->key); printPreOrder(nodePtr->leftPtr); printPreOrder(nodePtr->rightPtr); }}
• Postorder (left, right, visit):void printPostOrder(TreeNode* nodePtr){ if(nodePtr != NULL){ printPostOrder(nodePtr->leftPtr); printPostOrder(nodePtr->rightPtr); printf(“ %f, nodePtr->key); }}
![Page 82: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/82.jpg)
82
Revision - Hash TablesRevision - Hash Tables• Each item has a unique key.• Use a large array called a Hash Table.• Use a Hash Function
– Use prime numbers– Maps keys to positions in the Hash Table.– Be easy to calculate.– Use all of the key.– Spread the keys uniformly.– Use unsigned integers
• Operations:– Insert– Delete– Search– Initialise
![Page 83: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/83.jpg)
83
unsigned hash(char* s){ int i = 0; unsigned value = 0; while (s[i] != ‘\0’){ value = (s[i] + 31*value) % 101;value = (s[i] + 31*value) % 101; i++; } return value;}
Example: Hash Function for a string
KeyHashValue
Aho 49
Kruse 95
Standish 60
Horowitz 28
Langsam 21
Sedgewick 24
Knuth 44
![Page 84: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/84.jpg)
84
Linear Probing - InsertLinear Probing - Insert• Apply hash function to get a position.• Try to insert key at this position.• Deal with collision
– When two keys are mapped to the same position.– Very likely
Linear Probing - SearchLinear Probing - Search• Apply hash function to get a position.• Look at this position.• Deal with collision
– When two keys are mapped to the same position.– Very likely
Linear Probing - DeleteLinear Probing - Delete• Use the search function to find the item
• If found check that items after that also don’t hash to the item’s position
• If items after do hash to that position, move them back in the hash table and delete the item.
![Page 85: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/85.jpg)
85
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
Example: Insert with Linear Probing
Langsam
0
1
2
3
6
4
5
hash table
5
Aho
Kruse
Standish
Horowitz
Hash Function Langsam
module insert(hashTable, item){ position = hash(item) initialise count to 0 while(count < hashTableSize) { if (position in hashTable is empty) { write item at position in hashTable exit loop } else { step position along (wrap around) increment count } } if (count == hashTableSize) then the hashTable is full}
![Page 86: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/86.jpg)
86
Example: Search with Linear Probing
Langsam
0
1
2
3
6
4
5
hash table
5
Aho
Kruse
Standish
Horowitz
Hash Function
module search(hashTable, target){ position = hash(target) initialise count to 0 while (count < hashTableSize) { if (position in hashTable is empty) return -1; else if (key at position in hashTable == target) return position; step position along (wrap around) increment count } if (count == hashTableSize) then return -1;}
![Page 87: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/87.jpg)
87
Hashtable with ChainingHashtable with Chaining• At each position in the array you have a list:
List hashTable[MAXTABLE];
• Must initialise each list (each element of the array)
• Advantages– Insertions and deletions are quick & easy
– Resizable
• Disadvantages– Uses more space
– More complex to implement
0
1
2
1
2
1
:
![Page 88: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/88.jpg)
88
Insert with ChainingInsert with Chaining• Apply hash function to get a position in the array.
• Insert key into the Linked List at this position in the array.
void InsertChaining(Table* hashTable, float item){ int posHash = hash(item) ListInsert (hashTable[posHash], item); }
0
1
2
1
2 Knuth
1
Standish
Aho
Sedgewick
:
![Page 89: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/89.jpg)
89
Search with ChainingSearch with Chaining• Apply hash function to get a position in the array.
• Search the Linked List at this position in the array to see if the item is in it.
/* module returns NULL if not found, or the address of the * node if found */
Node* SearchChaining(Table* hashTable, float item){ posHash = hash(item) Node* found; found = searchList (hashTable[posHash], item); return found;}
0
1
2
1
2 Knuth
1
Standish
Aho
Sedgewick
:
![Page 90: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/90.jpg)
90
Delete with ChainingDelete with Chaining• Apply hash function to get a position in the array.
• Delete the item in the Linked List at this position in the array.
/* module uses the Linked list delete function to delete * an item inside that list, it does nothing if that item * isn’t there. */
void DeleteChaining(Table* hashTable, float item){ int posHash = hash(item) deleteList (hashTable[posHash], item);}
0
1
2
1
2 Knuth
1
Standish
Aho
Sedgewick
:
![Page 91: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/91.jpg)
91
Revision - MergesortRevision - Mergesort• Recursively split the array in half until you have arrays of length 1
• Merge them together in sorted order
• Return the merged array
void mergeSort(float array[], int size){ int* tmpArrayPtr = (int*)malloc(size*sizeof(int)); if (tmpArrayPtr != NULL)
mergeSortRec(array, size, tmpArrayPtr);mergeSortRec(array, size, tmpArrayPtr); else{ fprintf(stderr, “Not enough memory to sort list.\n”); exit(1); } free(tmpArrayPtr);} mergeListmergeList
tmp:tmp:
array:array:
![Page 92: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/92.jpg)
92
Revision - MergesortRevision - Mergesortvoid mergeSortRec(float array[], int size, float tmp[]){ int i; int mid = size/2; if (size > 1) { mergeSortRec(array, mid, tmp);mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp);mergeSortRec(array+mid, size-mid, tmp);
mergeArrays(array, mid, array+mid, size-mid, tmp);mergeArrays(array, mid, array+mid, size-mid, tmp);
for (i = 0; i < size; i++) array[i] = tmp[i];
}} SortSort
SortSort SortSort
SortSort SortSort SortSort SortSort
CombineCombine
CombineCombine CombineCombine
![Page 93: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/93.jpg)
93
Revision - MergesortRevision - Mergesortvoid mergeArrays(float a[],int aSize,float b[],int bSize,float tmp[]){ int k, i = 0, j = 0;
for (k = 0; k < aSize + bSize; k++) { if (i == aSize) { tmp[k] = b[j]; j++; } else if (j == bSize) { tmp[k] = a[i]; i++; } else if (a[i] <= b[j]) { tmp[k] = a[i]; i++; } else { tmp[k] = b[j]; j++; }
}}
4 5
3 6 2 5a:a: b:b:
3 7 8tmp:tmp:
7 4 8
2 6
CombineCombine
![Page 94: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/94.jpg)
94
Revision - QuicksortRevision - Quicksort• Partition
– Choose a pivot element– Swap pivot with array[0]– Sort remaining elements so that the ones less than the pivot are to the left
of the ones that are greater than the pivot.– Swap the pivot back into the correct position (the rightmost less than
element)
• Sort the sub-array to the left of the pivot• Sort the sub-array to the right of the pivot
x < p p p <= x
Partition
Sort Sort
![Page 95: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/95.jpg)
95
Revision - QuicksortRevision - Quicksort
x < p p p <= x
Partition
Sort Sort
void quickSort(float array[], int size){ int index; if (size > 1) { index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index - 1); }}
![Page 96: CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d485503460f94a247b0/html5/thumbnails/96.jpg)
Revision – Quicksort - PartitionRevision – Quicksort - Partitionint partition(float array[], int size){ int k; int mid = size/2; int index = 0;
swap(array, array+mid); for (k = 1; k < size; k++){ if (list[k] < list[0]){ index++; swap(array+k, array+index); } } swap(array, array+index); return index;}
p
p
00
p x < p p <= x
p x < p p <= x
px < p p <= x
96