1 / 36

MT258 Computer Programming and Problem Solving

MT258 Computer Programming and Problem Solving. Unit 9. UNIT Nine From algorithms to problem solving. Stack. The stack data structure is characterized as ‘last in, first out’ or LIFO. Stack can be implemented using contiguous arrays or using linked lists with pointers. Stack.

seanna
Download Presentation

MT258 Computer Programming and Problem Solving

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. MT258Computer Programming andProblem Solving Unit 9

  2. UNIT NineFrom algorithms to problem solving

  3. Stack • The stack data structure is characterized as ‘last in, first out’ or LIFO. • Stack can be implemented using contiguous arrays or using linked lists with pointers.

  4. Stack • define as array : • #define MAXSTACK 10 • typedef char StackEntry; • typedef struct stack • { int top; • StackEntry entry[MAXSTACK]; • } Stack;

  5. Stack • define as pointer • typedef struct node • { StackEntry entry; • stack node *next; • } Node; • typedef struct stact { • Node* top; • } Stack;

  6. Stack • Primitive operators : • creating a stack • pushing (insert an element to a stack) • popping (delete an element from a stack) • checking the emptiness of the stack • checking the fullness of the stack

  7. Stack • Please refer to the example in page 478-484 in our textbook ( C How to Program). • Please refer to the example in page 83-85 in our reference book (Data Structure & Program Design in C).

  8. Queue • The queue has a characteristic of ‘first in,first out’.

  9. Queue • define as array • #define MAXQUEUE 10; • typedef char QueueEntry; • typedef struct queue • { int count; • int front; • int rear; • QueueEntry entry[MAXQUEUE]; • }Queue;

  10. Queue • define as pointer • typedef char QueueEntry; • typedef struct queuenode • { QueueEntry info; • struct queuenode *next; • } QueueNode; • typedef struct queue • { QueueNode *front; • QueueNode *rear; • } Queue;

  11. Queue • Primitive operators : • creating a queue • appending an element to a queue • removing an element from a queue • checking the current queue size • checking the emptiness of the queue • checking the fullness of the queue

  12. Queue • Please refer to the example in page 484-490 in our textbook ( C How to Program). • Please refer to the example in page 135-137 in our reference book (Data Structure & Program Design in C).

  13. Big-O Notation • If f(n) and g(n) are functions defined for positive integers, then to write f(n) = O(g(n)) • It means that there exists a constant c such that |f(n)| <= c|g(n)|

  14. Examples • linear function • f(n) = 100n • since f(n) <= c n for any larger constant c>100 • then f(n) is O(n) • f(n) = 4n + 200 • since f(n) <= 5n whenever n >=200 • then f(n) is O(n)

  15. Examples • Quadratic function • f(n) = 3n2 + 100n • since f(n) <= c n for any larger constant c>3 • then f(n) is O(n2)

  16. Common orders • Constant - O(1) • linear time - O(n) • quadratic time - O(n2) • cubic time - O(n3) • exponential time - O(2n) • logarithmic time - O(log n) and O(n log n)

  17. Growing speed (starting from more efficient for large n) • 1 • lg n • n • n lg n • n2 • n3 • 2n

  18. Activity • Find the big-o notation of the followings: • 20 n3 + 10 n lg n + 5 • 3 lg n + lg(lg(n) • 2100

  19. Activity • 20 n3 + 10 n lg n + 5 • since n lg n < n3 • f(n) = 20 n3 + 10n lg n + 5 • <= 20n3 + 10n3 + 5n3 • <= 35 n3 since f(n) <= c n for any larger constant c>35 • = O(n3)

  20. Activity • 3 lg n + lg(lg(n) • f(n) = 3 lg n + lg(lg(n)) • <= 3 lg n + lg n • <= 4 lg n since f(n) <= c n for any larger constant c>4 • = O(lg n)

  21. Activity • 2100 • f(n) = O(1)

  22. Activity • For each of the following pairs of function, find the smallest integer value of n > 1 for which the first becomes larger than the second. • n2, 15n+5 • 0.1n, 10 lg n • 2n, 8n4 • 0.1n2, 100n lg n

  23. Activity • n = 16 : n2 = 256, 15n+5 = 245 • n = 997 : 0.1n = 99.70, 10 lg n = 99.61 • n = 21 : 2n = 2097152, 8n4 = 1555848 • n = 13747 : 0.1n2 = 18898000, • 100n lg n = 18897766

  24. Activity • Assume you have just bought the fastest PC that runs at 1GHz (1,000,000,000Hz). Also assume the CPU is so good that each instruction can be performed within one clock cycle, i.e. this super PC can handle 1 billion instructions per second (1 gips or 1,000 mips). Now you have 64 inputs (n=64) to be executed in three different algorithms • How long would it take to execute the algorithm that takes log2 (n) steps? • How long would it take to execute the algorithm that takes n3 steps? • How many years for the algorithm that takes 2n steps?

  25. Activity • log2 (64) = 6 (26 = 64) Thus, it takes 6 nanoseconds. • 643 = 262144 nanoseconds or .000262 seconds • 2n= 1.8 * 10 19 nanoseconds Thus, 584.9 years

  26. Activity • void Arrange2(int *a, int n) • { int i, j, k, min; • for (i=0; i < n; i++) • { k = i; • for (j = 1; j < n; j++) • if (a[j] < a[k]) k = j; • min = a[k]; • a[k] = a[i]; • a[i] = min; • } • }

  27. Activity • Number of steps : • n ( 4 (n - 1) + 2 + 2 + 4) + 2 • = n ( 4n + 4) + 2 • = 4 n2 + 4n + 2 • = O(n2)

  28. Sorting algorithms • insertion sort • selection sort • bubble sort • mergesort • quicksort

  29. Insertion sort • Starting with the second key, each item is extracted and placed in relative order with the items preceding it. • Extra variable is required to aid the comparisons. • Lots of swapping • O(n2)

  30. Selection sort • Starting with the last position, the largest unsorted key is swapped into the last unsorted position. • No extra variable is required to aid the comparisons • the movement of data is limited to one-to-one exchange. • O(n2)

  31. Bubble sort • They are very easy to understand. • Each key is compared and swapped with the adjacent key in the list. • O(n2)

  32. Mergesort • The list is divided into sublists, each sublist is sorted, then the sublists are merged together in proper order. • It is one of divide and conquer sorting. • The drawback of array implementation is the additional memory space, a temporary array with the same size as the original list is required. • O(n lg n) • Please refer to the example in page 304-306 in our reference book (Data Structure & Program Design in C).

  33. Quicksort • The list is divided into sublists, each sublist is sorted, then the sublists are merged together in proper order. • It is one of divide and conquer sorting. • The drawback of array implementation is the additional memory space, a temporary array with the same size as the original list is required. • It requires a pivot. • O(n lg n)

  34. Activity • You need to fill in the numbers for pass 1 and the remaining passes for merging part of Mergesort. • Pass 0: {66}{33}{40}{22}{55}{88}{60}{11}{80}{20}{50}{44}{77}{60} • Pass 1: { }{ }{ }{ }{ } • { }{ }

  35. Activity • Pass 1: {33,66} {22,40} {55, 88} {11, 60} • {20, 80} {44,50} {60, 77} • Pass 2: {22, 33, 40, 66} {11, 55, 60, 88} • {20, 44, 50, 80} {60, 77} • Pass 3: {11, 22, 33, 40, 55, 60, 66, 88} • {20, 44, 50, 60, 77, 80} • Pass 4: {11, 20, 22, 33, 40, 44, 50, 55, 60, 60, 66, • 77, 80, 88}

  36. Activity • Suppose Data contains 1,000,000 elements. Using the binary search algorithm, how many comparisons are required to find the location of an item in the Data array even in the worst case? (log 2 n) • 210 > 1000 • 220 > 10002 = 1,000,000 • It only requires about 20 comparisons to find the location of an item in a data array with 1,000,000 elements.

More Related