Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Dai Tian-Shyr

Similar presentations


Presentation on theme: "Data Structures Dai Tian-Shyr"— Presentation transcript:

1

2 Data Structures Dai Tian-Shyr cameldai@mail.nctu.edu.tw

3 What The Course Is About s Data structures is concerned with the representation and manipulation of data. s All programs manipulate data. s So, all programs represent data in some way. s Data manipulation requires an algorithm.

4 What The Course Is About We shall study ways to represent data and algorithms to manipulate these representations. The study of data structures is fundamental to Computer Science & Engineering.

5 Prerequisites s C++ – Read Sec. 1.4 of the textbook

6 Course Information s All course slides can be found in eCampus s 助教 : 劉彥君 –hialan.liu@gmail.comhialan.liu@gmail.com s Textbook: s Fundamental of Data Structures in C++ –Horowitz, Sahni, Metha – 開發圖書魏錦玲 0939852332

7 Slides Presentations and Book Reading LectureContentReadingSlides 1Introduction, Insertion Sort Section 7.2Powerpoint 2Algorithmic Complexity (Insertion Sort). Section 1.7.Powerpoint 3Experimental Performance Measurement. Section 1.7.Powerpoint See ReadingAssignment.doc in eCampus website for more information.

8 評分標準 s Homework s 期中考 s 期末考 s 上課表現 s

9 Sorting s Rearrange a[0], a[1], …, a[n-1] into ascending order. When done, a[0] <= a[1] <= … <= a[n-1] s 8, 6, 9, 4, 3 => 3, 4, 6, 8, 9

10 Sort Methods s Insertion Sort :We focus on it in the following slide. s Bubble Sort s Selection Sort s Count Sort s Shaker Sort s Shell Sort s Heap Sort s Merge Sort s Quick Sort

11 Insert An Element s Given a sorted list/sequence, insert a new element s Given 3, 6, 9, 14 s Insert 5 s Result 3, 5, 6, 9, 14

12 Insert an Element s 3, 6, 9, 14 insert 5 s Compare new element (5) and last one (14) s Shift 14 right to get 3, 6, 9,, 14 s Shift 9 right to get 3, 6,, 9, 14 s Shift 6 right to get 3,, 6, 9, 14 s Insert 5 to get 3, 5, 6, 9, 14

13 Insert An Element // insert t into a[0:i-1] int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t;

14 Insertion Sort s Start with a sequence of size 1 s Repeatedly insert remaining elements

15 Insertion Sort s Sort 7, 3, 5, 6, 1 s Start with 7 and insert 3 => 3, 7 s Insert 5 => 3, 5, 7 s Insert 6 => 3, 5, 6, 7 s Insert 1 => 1, 3, 5, 6, 7

16 Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] // code to insert comes here }

17 Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

18 Homework s Understand all the example codes in sec. 1.4 s Sec. 1.4 Exercise 2 @P24


Download ppt "Data Structures Dai Tian-Shyr"

Similar presentations


Ads by Google