Selection Sort 6 7 Selection Sort C void

  • Slides: 108
Download presentation

Selection. Sort 6

Selection. Sort 6

7 Selection Sort )ภาษา C( void selection. Sort(int d[], int n) { int k,

7 Selection Sort )ภาษา C( void selection. Sort(int d[], int n) { int k, i; for (k = n - 1; k > 0; k--) { int max. I =1; for (i = 0; i <= k; i++) if (d[i]> d[max. I]) max. I = i; int t = d[k]; d[k] = d[max. I]; d[max. I] = t; } }

#include <stdio. h> 8 #include <stdlib. h> #include <windows. h> int main(int argc, char

#include <stdio. h> 8 #include <stdlib. h> #include <windows. h> int main(int argc, char *argv[]) { int i, k, n, repeat = 10; for( n = 0; n<=20000; n+=2000) { int *d = malloc( n * sizeof(int) ); long sum = 0; for (k=0; k<repeat; k++) { for (i=0; i<n; i++) d[i] = i; long start = Get. Tick. Count(); selection. Sort(d, n); sum += Get. Tick. Count() - start; } printf("%d t %f n", n, (float)sum/repeat); } system("PAUSE"); return 0; }

9 Selection Sort (ภาษา Java)

9 Selection Sort (ภาษา Java)

public class Test. Selection. Sort { public static void main(String[] args) { 10 int

public class Test. Selection. Sort { public static void main(String[] args) { 10 int repeat = 10; for (int n = 0; n <= 20000; n += 2000) { int[]d = new int[n]; long sum = 0; for (int k = 0; k < repeat; k++) { for (int i = 0; i <n; i++) d[i] = i; { long start = System. current. Time. Millis(); selection. Sort(d); sum += System current. Time. Millis() - start; } System. out. printf("%d t %f n", (double)sum/repeat); } }

public class Test. Selection. Sort { public static void main(String[] args) { 11 int

public class Test. Selection. Sort { public static void main(String[] args) { 11 int repeat = 10; for (int n = 0; n <= 20000; n += 2000) { int[]d = new int[n]; long sum = 0; for (int k = 0; k < repeat; k++) { for (int i = 0; i <n; i++) d[i] = i; { long start = System. current. Time. Millis(); selection. Sort(d); sum += System current. Time. Millis() - start; } System. out. printf("%d t %f n", (double)sum/repeat); } } for (int i = 0; i < 10000; i++) selection. Sort(new int[1]);

12 สงทำงาน พรอมบนทกผล ภาษา C : ใช GNU-C Compiler (3. 4. 2) ภาษา Java

12 สงทำงาน พรอมบนทกผล ภาษา C : ใช GNU-C Compiler (3. 4. 2) ภาษา Java : ใช Java 6 (build 1. 6. 0_14 -b 08) Interpreted-only mode (option –Xint ตอนทำงาน ) Compilation to native code (บงคบ compile ดวยการเรยกเมทอดวำ ๆ สก 10 000 ครง ) Intel Core 2 Duo P 8400 2. 26 Ghz Ram 3 GB Windows XP

ใชจำนวนการทำงานของคำสงแทนเวลา 14 static int counter= 0; static void selection. Sort(int[] d) { counter =

ใชจำนวนการทำงานของคำสงแทนเวลา 14 static int counter= 0; static void selection. Sort(int[] d) { counter = 1+d. length-1; for (int k = d. length-1; k > 0; k--) { int max. I =1; counter += 2 + k+1; for (int i = 0; i <= k; i++) { counter += 1; if (d[i] > d[max. I]) { max. I=i; counter+=1; } } counter += 3; int t = d[k]; d[k] = d[max. I]; d[max. I] = t; } }

15 เขยนโปรแกรมทดลองนบคำสง public class Test. Selection. Sort { public static void main(String[] args) {

15 เขยนโปรแกรมทดลองนบคำสง public class Test. Selection. Sort { public static void main(String[] args) { int repeat = 10; for (int n = 0; n <= 20000; n += 2000) { int[]d = new int[n]; long sum = 0; for (int k = 0; k < repeat; k++) { for (int i = 0; i <n; i++) d[i] = i; ; counter = 0; selection. Sort(d); sum += counter; } System. out. printf("%d t %f n", n, (double)sum/repeat); } }

17 นบเฉพาะคำสงตวแทน static intcounter= 0; static voidselection. Sort(int[] d) { for (int k =

17 นบเฉพาะคำสงตวแทน static intcounter= 0; static voidselection. Sort(int[] d) { for (int k = d. length-1; k > 0; k--) { int max. I =1; for (int i = 0; i <= k; i++) { counter += 1; if (d[i] > d[max. I]) { max. I = i; } } int t d[k]; d[k] = d[max. I]; d[max. I]=t; } }

27

27

29

29

30

30

31

31

32

32

33

33

35

35

36

36

37

37

38

38

39

39

41 f(n) vs. g(n)

41 f(n) vs. g(n)

43

43

44 L'Hôpital's rule

44 L'Hôpital's rule

45

45

47

47

49 สญกรณเชงเสน กำกบ (Asymptotic Notations) • little-o • little – omega • Big –

49 สญกรณเชงเสน กำกบ (Asymptotic Notations) • little-o • little – omega • Big – Omega • Big - Theta

50 little-o

50 little-o

51 little – omega

51 little – omega

52 Big - Theta

52 Big - Theta

53 Big – O

53 Big – O

54 Big – Omega

54 Big – Omega

62

62

67

67

68

68

69

69

72

72

73

73

74

74

75

75

76 ตวอยาง Insertion Sort

76 ตวอยาง Insertion Sort

78

78

81

81

82 ตวอยาง : Tower of Hanoi

82 ตวอยาง : Tower of Hanoi

83 dki

83 dki

86

86

87

87

88

88

89

89

90

90

91

91

92

92

93

93

94

94

95

95

96

96

97

97

98

98

99

99

100

100

101

101

102

102

103

103

104

104

105

105

106

106

107

107

108

108