1 / 98

Data Structures and DBMS ( Data Base Management System )

Learn how data is represented in the form of 1s and 0s, how to represent characters, integers, and real numbers. Understand different data structures like stack, queue, list, and tree. Explore various sorting algorithms like Bubble Sort, Selection Sort, and Insertion Sort. Gain insights into the complexity and performance of these algorithms.

valariek
Download Presentation

Data Structures and DBMS ( Data Base Management System )

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. Data Structures and DBMS ( Data Base Management System) Wen-Nung Tsai tsaiwn@csie.nctu.edu.tw CSIE Department, NCTU

  2. Data Representation • 所有的資料都是 1 和 0 • 如何表示文字符號? 回想有與沒有的猜數遊戲 • 整數如何表示? • 負數? 正負配絕對值? 用1的補數? 用2的補數? • 實數如何表示? 先標準化, 拆成指數與小數 • IEEE754/854 • 兩倍準的實數? • IEEE754/854

  3. Data Structures • 更High Level 的看法 • 如何表示 Stack ? Queue? List? • 如何表示族譜? 公司或學校組織架構? • Tree? • Binary tree • General tree • AVL 平衡樹 • B-Tree • …

  4. Sorting (排序) • Take a set of items, order unknown • Set: Linked list, array, file on disk, … • Return ordered set of the items • For instance: • Sorting names alphabetically • Sorting by height • Sorting by weight

  5. Sorting Algorithms Issues of interest: • Running time in worst case, other cases • Space requirements • In-place algorithms: require constant space • The importance of empirical testing Often Critical to Optimize Sorting

  6. Short Example: Bubble Sort Key: “large unsorted elements bubble up” • Make several sequential passes over the set • Every pass, fix local pairs that are not in order Considered inefficient, but useful as first example

  7. (Naïve)BubbleSort(array A, length n) • for in to 2// note: going down • for j2 to i // loop does swaps in [1..i] • if A[j-1]>A[j] • swap(A[j-1],A[j])

  8. Bubble sort example(1/3) Pass 1: 25 57 48 37 12 92 86 33 25 48 57 37 12 92 86 33 25 48 37 57 12 92 86 33 25 48 37 12 57 92 86 33 25 48 37 12 57 86 92 33 25 48 37 12 57 86 33 92

  9. Bubble Sort example(2/3) Pass 2: 25 48 37 12 57 86 33 92 25 37 48 12 57 86 33 92 25 37 12 48 57 86 33 92 25 37 12 48 57 33 86 92 Pass 3: 25 37 12 48 57 33 86 92 25 12 37 48 57 33 86 92 25 12 37 48 33 57 86 92

  10. Bubble Sort example(3/3) Pass 4: 25 12 37 48 33 57 86 92 12 25 37 48 33 57 86 92 12 25 37 33 48 57 86 92 Pass 5: 12 25 37 33 48 57 86 92 12 25 33 37 48 57 86 92 Pass 6: 12 25 33 37 48 57 86 92 Pass 7: 12 25 33 37 48 57 86 92

  11. Bubble Sort Features • Worst case: Inverse sorting • Passes: n-1 • Comparisons each pass: (n-k) where k pass number • Total number of comparisons: (n-1)+(n-2)+(n-3)+…+1 = n2/2-n/2 = O(n2) • In-place: No auxilary storage • Best case: already sorted • O(n2) Still: Many redundant passes with no swaps

  12. Big O, Big , Big  • Big O • 描述 complexity 上限 • Big  • 描述 complexity 下限 • Big  ? 參考資料結構或演算法的書

  13. 改良型氣泡排序法BubbleSort(array A, length n) • in • quitfalse • while (i>1 AND NOT quit) // note: going down • quittrue • for j=2 to i // loop does swaps in [1..i] • if A[j-1]>A[j] • swap(A[j-1],A[j]) // put max in I • quitfalse • ii-1

  14. Bubble Sort Features • Best case: Already sorted • O(n) – one pass over set, verifying sorting • Total number of exchanges • Best case: None • Worst case: O(n2) -- 與 n 平方成正比 Lots of exchanges: A problem with large items

  15. Selection Sort • Observation: Bubble-Sort uses lots of exchanges • These always float largest unsorted element up • We can save exchanges: • Move largest item up only after it is identified • More passes, but less total operations • Same number of comparisons • Many fewer exchanges

  16. SelectSort(array A, length n) • for in to 2 // note we are going down • largest  A[1] • largest_index  1 • for j1 to i // loop finds max in [1..i] • if A[j]>A[largest_index] • largest_index  j • swap(A[i],A[largest_index]) // put max in i

  17. Selection Sort Example Initial: 25 57 48 37 12 92 86 33 Pass 1: 25 57 48 37 12 33 86| 92 Pass 2: 25 57 48 37 12 33 I 86 92 Pass 3: 25 33 48 37 12 I 57 86 92 Pass 4: 25 33 12 37I 48 57 86 92 Pass 5: 25 33 12 I 37 48 57 86 92 Pass 6: 25 12 I 33 37 48 57 86 92 Pass 7: 12 I 25 33 37 48 57 86 92

  18. Selection Sort Summary • Best case: Already sorted • Passes: n-1 • Comparisons each pass: (n-k) where k pass number • # of comparisons: (n-1)+(n-2)+…+1 = O(n2) • Worst case: Same. • In-place: No external storage • Very few exchanges: • Always n-1 (better than Bubble Sort)

  19. Selection Sort vs. Bubble Sort • Selection sort: • more comparisons than bubble sort in best case O(n2) • But fewer exchanges O(n) • Good for small sets/cheap comparisons, large items • Bubble sort: • Many exchangesO(n2) in worst case • O(n) on sorted input

  20. Insertion Sort • Improve on # of comparisons • Key idea: Keep part of array always sorted • As in selection sort, put items in final place • As in bubble sort, “bubble” them into place

  21. InsertSort(array A, length n) • for i2 to n // A[1] is sorted • y=A[i] • j  i-1 • while (j>0 AND y<A[j]) • A[j+1]  A[j] // shift things up • jj-1 • A[j+1]  y // put A[i] in right place

  22. InsertSort example(1/4) Initial: 25 57 48 37 12 92 86 33 Pass 1: 25 | 57 48 37 12 92 86 33 Pass 2: 25 57 I 48 37 12 92 86 33 25 48 | 57 37 12 92 86 33 Pass 3: 25 48 57 | 37 12 92 86 33 25 48 57 | 57 12 92 86 33 25 48 48 | 57 12 92 86 33 25 37 48 | 57 12 92 86 33

  23. InsertSort example(2/4) Pass 4: 25 37 4857 | 12 92 86 33 25 37 48 57 | 57 92 86 33 25 37 48 48 | 57 92 86 33 25 37 37 48 | 57 92 86 33 25 25 37 48 | 57 92 86 33 12 25 37 48 | 57 92 86 33

  24. InsertSort example(3/4) Pass 5: 12 25 37 48 57 | 92 86 33 Pass 6: 12 25 37 48 57 92 | 86 33 12 25 37 48 57 86 | 92 33 Pass 7: 12 25 37 48 57 86 92 | 33 12 25 37 48 57 86 92 | 92 12 25 37 48 57 86 86 | 92 12 25 37 48 57 57 86 | 92 12 25 37 48 48 57 86 | 92

  25. InsertSort example(4/4) Pass 7: 12 25 37 48 48 57 86 | 92 12 25 37 37 48 57 86 | 92 12 25 33 37 48 57 86 | 92

  26. Insertion Sort Summary • Best case: Already sorted  O(n) • Worst case: O(n2) comparisons • # of exchanges: O(n2) • In-place: No external storage • In practice, best for small sets (<30 items) • BubbleSort does more comparisons! • Very efficient on nearly-sorted inputs

  27. Nicklas (Nicholas) Wirth • Invented the Pascal Language • Wrote a book: Data Structures + Algorithms = Programs

  28. Divide-and-Conquer Algorithm An algorithm design technique: • Divide a problem of size N into sub-problems • Solve all sub-problems • Merge/Combine the sub-solutions This can result in VERY substantial improvements

  29. Small Example: f(n) • if ( n == 0 OR n == 1) • return 1; • else • return f(n-1)*n; What is this function?

  30. Small Example: f(n) • if ( n == 0 OR n == 1) • return 1; • else • return f(n-1)*n; What is this function? Factorial ! 算 n 階乘 你告訴我 (n-1)階乘; 我就告訴你 n 階乘

  31. Recursion

  32. Divide-and-Conquer in Sorting • Mergesort • O(n log n) always, but O(n) storage • Quick sort • O(n log n) average, O(n2) worst in time • Good in practice when n>30, O(log n) storage • But, Quick sort is not a Stable sort Key 一樣之 data 其相對順序在排好後與原先不同

  33. Selection Sort, Insertion Sort, Bubble Sort • Selection Sort does more comparisons! (key) • Selection Sort does less exchanges ! (data) • Worst case 對 n 個 data 做 sort 三者都是 O(n2) Big-O of n square • Quick Sort : (average case) O(n * log n) = O(n*log2 n)

  34. Quick Sort到底有多快 ? • Consider the comparison • Selection sort: (n-1)+(n-2)+…+2+1 = n(n-1)/2 • n 個資料經過 n-1 次比較可以使一個資料到定位 • 到最前面(最左邊)? 到最後面(最右邊)? • 可否到中間?  幾乎中間 : Quick sort • 假設 100 個資料 • Selection sort: 100*99/5 = 50*99 comparison times • 若先用一pass quick sort 概念 : 99 次比較使一個排定 • 再來切成兩半若運氣好為 49 個 和 50 個 • 都算 50 個且用 selection sort: 2 * 50 * 49 / 2 = 50*49 • 共 99 + 50 * 49 = 約 50 * 51 次比較

  35. Quicksort Algorithm Given an array of n elements : • If array only contains one element, return • Else • pick one element to use as pivot. • Partition elements into two sub-arrays: • Elements less than or equal to ( <= ) pivot • Elements greater than ( > ) pivot • Quicksort two sub-arrays • Return results

  36. Quick Sort Example We are given array of n integers to sort: 38 18 10 80 60 50 7 30 98

  37. Pick Pivot Element Quick Sort Example There are a number of ways to pick the pivot element. In this example, we will use the first element in the array: 38 18 10 80 60 50 7 30 98 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  38. Partitioning Array Quick Sort Example Given a pivot, partition the elements of the array such that the resulting array consists of: • One sub-array that contains elements >= pivot • Another sub-array that contains elements < pivot The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements below/above pivot.

  39. Quick Sort Example 38 18 10 80 60 50 7 30 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  40. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index 38 18 10 80 60 50 7 30 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  41. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index 38 18 10 80 60 50 7 30 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  42. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index 38 18 10 80 60 50 7 30 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  43. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index 38 18 10 80 60 50 7 30 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  44. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index 38 18 10 80 60 50 7 30 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  45. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index • If too_big_index < too_small_index • swap data[too_big_index] and data[too_small_index] 38 18 10 80 60 50 7 30 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  46. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index • If too_big_index < too_small_index • swap data[too_big_index] and data[too_small_index] 38 18 10 30 60 50 7 80 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  47. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index • If too_big_index < too_small_index • swap data[too_big_index] and data[too_small_index] • While too_big_index < too_small_index, go to 1. 38 18 10 30 60 50 7 80 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  48. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index • If too_big_index < too_small_index • swap data[too_big_index] and data[too_small_index] • While too_big_index < too_small_index, go to 1. 38 18 10 30 60 50 7 80 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  49. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index • If too_big_index < too_small_index • swap data[too_big_index] and data[too_small_index] • While too_big_index < too_small_index, go to 1. 38 18 10 30 60 50 7 80 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

  50. Quick Sort Example • While data[too_big_index] <= data[pivot_index] • ++too_big_index • While data[too_small_index] > data[pivot_index] • --too_small_index • If too_big_index < too_small_index • swap data[too_big_index] and data[too_small_index] • While too_big_index < too_small_index, go to 1. 38 18 10 30 60 50 7 80 98 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 想辦法在 n-1 次 比較後, 使小的在左半; 大的在右半

More Related