• Save
Data structures and algorithms
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Data structures and algorithms

on

  • 2,446 views

Covers basic data structure like linked list, stack and queue and main sorting snf searching algorithms

Covers basic data structure like linked list, stack and queue and main sorting snf searching algorithms

Statistics

Views

Total Views
2,446
Views on SlideShare
2,439
Embed Views
7

Actions

Likes
9
Downloads
0
Comments
1

2 Embeds 7

https://twitter.com 6
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Depends on implementation,prog. language optimizing compiler capabilities, CPU speed, other h/w characteristics/specifications ……..
  • Stop when value reached is more that item searched for.
  • C++ Data Structures, Nell Dale, 3rd Edition
  • C++ Data Structures, Nell Dale, 3rd Edition
  • C++ Data Structures, Nell Dale, 3rd Edition
  • C++ Data Structures, Nell Dale, 3rd Edition
  • Find max instead of min value.
  • C++ Data Structures, Nell Dale, 3rd Edition
  • C++ Data Structures, Nell Dale, 3rd Edition
  • C++ Data Structures, Nell Dale, 3rd Edition
  • C++ Data Structures, Nell Dale, 3rd Edition
  • Add a flag to stop when no swaps were done in Bubble up
  • Faster to sort already or almost sorted arrays
  • Because one of the halves must be finished for the loop to end
  • Homogeneous or heterogeneous groups of data items

Data structures and algorithms Presentation Transcript

  • 1. Data Structures and Algorithms Julie Iskander
  • 2. Algorithm + Data Structure = Program
  • 3. Topics we will try to cover
  • 4. Algorithms • Sorting • Searching
  • 5. Data Structures • • • • Linked Lists Stack Queue Tree
  • 6. Algorithms
  • 7. Algorithms • What is an algorithm? o Is a step-by-step finite sequence of instruction, to solve a well-defined computational problem • Algorithm analysis o Space complexity o Time complexity
  • 8. Space Complexity • Amount of memory allocated to the program • Space used by a program o Instruction space: store executable version of the program (fixed) o Data space: store constants and variables • Constants and simple variables (fixed) • Fixed sized structural variables, arrays and structures (fixed) • Dynamically allocated space (varies) o Environment stack space: stpore information to resume the suspended functions. • Return address • Values of function parameters
  • 9. Time Complexity • Amount of time needed to run to completion. • How to measure it accurately? o Count all operations performed and know the time needed for each primitive operation. o This will vary from machine to another o Also depends on data inputted • Solution: o Order of magnitude for the time required o Irrespective of machine used o Identify key operations (operations that require max. time among all possible operations) o Time complexity is expressed as a function of number of key operations.
  • 10. Time-Space Trade Off • The best solution to problem is the one that requires less space in memory and takes less time to complete its execution. • In practice, Not AVAILABLE. • A Sacrifice must be made according to the constraints of the problem.
  • 11. Input data • When analyzing an algorithm, it depends on the input data. • There are 3 cases: o Best Case o Average Case o Worst Case
  • 12. Searching Algorithms
  • 13. Linear Sequential Search • Simplest type
  • 14. Analysis • Best-case Scenario: find item at first location o O(1) • Worst-case Scenario: Not found , or found but in last location. o O(N) • Average-case Scenario: o O(N/2)
  • 15. How can knowing that a list is sorted, enhance linear search?
  • 16. Binary Search
  • 17. Binary Search • Data in list must be sorted and stored sequentially. • Divide and Conquer appraoch • No enhanced performance for very small lists.
  • 18. Binary Search
  • 19. Analysis
  • 20. Sorting Algorithms
  • 21. Sorting Algorithm • Sorting a large number of elements can be an extremely time consuming operation. • The key operation is comparing two values which is related to the number of elements in the list (N)
  • 22. Selection Sort
  • 23. Selection Sort
  • 24. Selection Sort
  • 25. Analysis • The minimum finding is run N-1 times (for whatever size) • Inside minimum finding function we have another loop that runs a varying number of times o o o o o •  1st time  N-1 times 2nd time  N-2 times 3rd time  n-3 times ………………. N-1th  1 times N(N-1)/2 times
  • 26. How can we make it a descending sort algorithm?
  • 27. Bubble Sort Bubble Up smallest item
  • 28. Bubble Sort
  • 29. Bubble Sort
  • 30. Analysis • The minimum finding is run N-1 times (for whatever size) • Inside minimum finding function we have another loop that runs a varying number of times o o o o o 1st time  N-1 times 2nd time  N-2 times 3rd time  n-3 times ………………. N-1th  1 times •  N(N-1)/2 times • Similar to Selection but with more overhead of swap with each iteration.
  • 31. How can we make Bubble sort shorter?
  • 32. Short Bubble Sort Analysis
  • 33. Why use Short Bubble sort?
  • 34. Merge Sort
  • 35. Merge Sort
  • 36. Merge Sort
  • 37. Merge Sort • What is the terminating condition of the MergeSort function? o If the array size is less than 2.
  • 38. Merge Sort
  • 39. Not both Copy remaining elements step are executed. Why?
  • 40. Analysis
  • 41. Lab 1 Implement Selection sort on an array of strings Implement Short Bubble sort on an array of integers Implement Merge sort on an array of integers Implement Sequential Search on array of Employees (search by code and search by name). • Implement Binary Search on array of strings. • • • • • Bonus: Implement Merge sort on an array of Employees
  • 42. Assignment 1 • What is big “Oh” notation? • Read and Implement Quick Sort on array of integers • Read and Implement Insertion Sort on array of integers • Discuss the complexity of each
  • 43. Data Structures • Data is the basic entity, used calculations and manipulation processs. • Data can be numeric (integer and float) or alphanumeric (strings). • Data can be single-valued or a set of values. • Data structures is a way of organizing data items by considering its relationship to each other. •Data structures affects the design of structural and functional aspects of a program
  • 44. Data Structures • Primitive DS: o Directly operated on by machine instructions o Integers, floating point numbers, characters, strings, pointers • Non-Primitive DS: o Arrays o Lists • Linear Lists o Stack o Queue • Non-linear Lists o Graph o Trees o Files
  • 45. Arrays • The most frequently used data structure • A collection of homogenous data elements described by a single name • Can be single or multiple dimensional • Elements are stored in successive memory locations.
  • 46. Disadvantage of Arrays • Fixed size • Difficulty of insertion and deletion of elements
  • 47. Vectors
  • 48. Linked Lists • An set of varying number of elements to which insertion and deletion can be made. • A linear list, has elements adjacent to each other. • Each elements is called a node Head X
  • 49. Linked Lists • Single Linked List Head X • Double Linked List Head Tail X
  • 50. Double Linked Lists • Each node must contain: o o o Data saved, A pointer to the next node, A pointer to the previous node. • We need a pointer to point to first Node and a pointer to point to last Node. struct node { struct node * pNext; struct node *pPrev; int data; } struct node * pHead struct node * pTail
  • 51. Operations on a Double Linked List • Initially a list is empty (NULL/Empty List) o pHead=pTail=NULL; • • • • • Addition Insertion Deletion Search (Linear Sequential Search) Free List
  • 52. Insertion At BEGIN Head Tail X ptr
  • 53. Insertion At END Adding Nodes Tail Head X ptr
  • 54. Insertion At LOC • Have the following cases to test: o o o o Empty List (pHead=pTail=NULL) LOC=0 (same as Insertion At BEGIN) LOC out of List (same as Insertion AT END) LOC in the anywhere in list
  • 55. Insertion At LOC • Empty List (pHead=pTail=NULL) Head ptr Tail pNext=pPrev=NULL
  • 56. Insertion At LOC • LOC in the anywhere in list o Start from Head and iterate (LOC-1) number of times Head pCur LOC=3 Tail X ptr
  • 57. Deletion At LOC • Have the following cases to test: o o o o o o Empty List (pHead=pTail=NULL)  Noting to delete Only One NODE (pHead=pTail !=NULL)  pHead=pTail=NULL LOC=0 Delete Last Node LOC out of List  Iterate till end and Noting to delete LOC in the anywhere in list
  • 58. Deletion At LOC=0 Tail Head X pPrev=NULL ptr
  • 59. Deletion At Tail Tail Head X pNext=NULL ptr
  • 60. Deletion At LOC • LOC is anywhere in list o Start from Head and iterate (LOC) number of times Head ptr LOC=2 Tail X
  • 61. Implementation in C struct node { int data; struct node *pNext; struct node *pPrev; }; struct node *pHead; struct node *pTail; struct node* createnode(int data); int search(int data); void printall(void); int addnode(int data); int insert(int data,int loc); int delete (int loc);
  • 62. Implementation in C struct node* createnode(int data) { struct node *ptr=NULL; ptr=(struct node*) malloc(sizeof(struct node)); if(ptr) { ptr->data=data; ptr->pNext=ptr->pPrev=NULL; } return ptr; }
  • 63. Implementation in C int addnode(int data) { int flag=0; struct node *ptr=createnode(data) ; if(ptr) { flag=1; if(pTail==NULL) //Empty List pTail=pHead=ptr; else //List exists so add at end { pTail->pNext=ptr; ptr->pPrev=pTail; pTail=ptr; } } return flag; }
  • 64. Implementation in C int search(int data) { struct node * pCur=pHead; int flag=0; while(pCur && !flag) { if(pCur->data==data) { flag=1; } else pCur=pCur->pNext; } return flag; }
  • 65. Implementation in C int insert(int data, int loc) { int flag=0,i=0; struct node *ptr,*pCur; ptr=createnode(data); if(ptr) { flag=1 ; if(!pHead) //Empty List pHead=pTail=ptr; else if(loc==0) //as first location { ptr->pNext=pHead; pHead->pPrev=ptr; pHead=ptr; } else{ pCur=pHead; for(;(i<loc-1&&pCur) ;i++) pCur=pCur->pNext; } if(!pCur || pCur==pTail) //reached Tail { ptr->pPrev=pTail; pTail->pNext=ptr; pTail=ptr; } else { pCur->pNext->pPrev=ptr; ptr->pNext=pCur->pNext; pCur->pNext=ptr; ptr->pPrev=pCur; }}}return flag;
  • 66. Lab 2 • Implement All functionalities of LL that carries an Employee(id, name,salary) o o o o o int addtotail(struct Employee data) int addtohead(struct Employee data); int Insert(struct Employee data, int loc) int delete (int loc) struct node * search(int id) • Bonus: Write Insert function such that new element are inserted sorted.
  • 67. Queue (FIFO) • First element inserted is first one retrieved • Can’t retrieve elements from middle of queue or end of stack • Can’t insert elements into middle or start of stack • Examples: o Process/Thread Queue o Event Queue o Print Queue
  • 68. Queue
  • 69. Queue Implementation • We need to know Front and Back of Queue • Operations done on Queue: o EnQueue  insert element at Back o DeQueue  retrieve element from Front
  • 70. Implementing Queue using Linked Lists #define SIZE 3 struct node { int data; struct node *pNext; }; struct node *pHead; struct node *pTail; int count; struct node* createnode(int data); int enqueue(int data); int dequeue(int *flag);
  • 71. Implementing Queue using Linked Lists struct node* createnode(int data) { struct node *ptr=NULL; count++; if(count<=SIZE) { ptr=(struct node*) malloc(sizeof(struct node)); if(ptr) { ptr->data=data; ptr->pNext=NULL; } } return ptr; }
  • 72. Implementing Queue using Linked Lists int enqueue(int data) { int flag=0; struct node *ptr=createnode(data) ; if(ptr) { flag=1; if(pTail==NULL) //Empty List pTail=pHead=ptr; else //List exists so add at end { pTail->pNext=ptr; pTail=ptr; } } return flag; }
  • 73. Implementing Queue using Linked Lists int dequeue(int* flag) { struct node * pCur=pHead; int data; *flag=0; if(pCur) { pHead=pHead->pNext; data=pCur->data; free(pCur); *flag=1; } return data; }
  • 74. Stack (LIFO) • Last element inserted is first one retrieved • Can’t retrieve elements from middle of stack or end of stack • Can’t insert elements into middle or end of stack • Examples: o A pile of dishes o Call Stack o Undo Stack
  • 75. Stack
  • 76. Stack Implementation • We need to know ToS (top of stack) • Operations done on stack: o Push  insert element at ToS o Pop  retrieve element from ToS
  • 77. Implementing Stack using Arrays #define SIZE 10 int stack[SIZE]; int tos = 0; int push (int data); int pop(int *flag);
  • 78. Implementing Stack using Arrays int push(int data) { int flag=0; if(tos<SIZE) { flag=1; stack[tos++]=data; } return flag; }
  • 79. Implementing Stack using Arrays int pop(int* flag) { int data; *flag=0; if(tos>0) { data=stack[--tos]; *flag=1; } return data; }
  • 80. Lab 3 • Implement Stack Queue with Linked Lists. • Implement Queue with arrays.
  • 81. Trees and Binary Search Trees
  • 82. Trees and Binary Search Trees • BST: is a tree where no node can have more than 2 children. And all elements in the left subtree must be smaller than values in the right subtree
  • 83. Binary Search Trees • Access time is decrease from N to log N (depth of tree) • Worst case: not fully balanced tree •  O(N) 7 11 14 42 58
  • 84. Binary Search Example 7 12 42 59 71 Looking for 89 86 104 212
  • 85. Binary Search Example 7 12 42 59 71 Looking for 89 86 104 212
  • 86. Binary Search Example 7 12 42 59 71 Looking for 89 86 104 212
  • 87. Binary Search Example 7 12 42 59 71 86 104 89 not found – 3 comparisons 3 = Log(8) 212
  • 88. Implementing a BST struct node { int data; struct node *pLeft; struct node *pRight; }; struct node* createnode(int data); struct node* insert(struct node* root, int data);
  • 89. Implementing a BST struct node* createnode(int data) { struct node *ptr=NULL; ptr=(struct node*) malloc(sizeof(struct node)); if(ptr) { ptr->data=data; ptr->pLeft=ptr->pRight=NULL; } return ptr; }
  • 90. Implementing a BST struct node* insert(struct node* root, int data) { if(!root) root=createnode(data); else if(root->data>data) root->pLeft=insert(root->pLeft,data); else if(root->data<data) root->pRight=insert(root->pRight,data); return root; }
  • 91. Traversing a BST • PreOrder 1. Process Root 2. Traverse left tree using PreOrder 3. Traverse right tree using PreOrder • InOrder 1. Traverse left tree using InOrder 2. Process Root 3. Traverse right tree using InOrder • PostOrder 1. Traverse left tree using PostOrder 2. Traverse right tree using PostOrder 3. Process Root
  • 92. Traversing a BST • PreOrder void preorder(struct node *root) { if(root) { printf(" %d ",root->data); preorder(root->pLeft); preorder(root->pRight); } }
  • 93. Lab 4 • Implement Binary Tree of Integer values • Implement inorder, preorder and postorder traversing of the tree to print its content. • Implement a function to find max of a BST • Implement a function to find min of a BST
  • 94. References • C++ Data Structures, Nell Dale, 3rd Edition