Title: a
1???????? ??? ?????????????????
a
7
6
8
5
4
9
a ??????????????????????????????????
?????????????? ???????????? object ?????????
??????????? a ??????????? object reference
??????? ??????????? a ????????????????????????????
??????????????????????????????????
2Index ???????????
- ?????? aindex ???????????????????????????
??????????????????? - a0 ????????? 7 ??? a1 ????????? 6
- ??? index ????????????? 0 (??????????????)
- ?????? index ??????????????????????? ???????????
expression ????????????????????????????? - ???? ??? b2 ??? c3 ???? abc
????????????????????????????????????????????????
??? abc ???????????????????????????????????????
??? ???? abc 5
3???????????????
- ????????? ???? ???????? ?????????? length
- ??????????????????????????????????????????????????
?????? - ???????????? a.length ????????????????? a
????????
4?????????????????? Java
- ????????????????????????? ???
- Array Declaration ???????????????????????????????
- Array Allocation ????????????????????????????????
?????????????? ??????? assignment operator
???????????????? 1 ???????????????????????
5?????????????????? Java (???)
- int x //Declaration ???????????????????? x
?????????????????? - //????????? ????????????????????????????
????? - xnew int5 //Allocation ?????????????????????
??????????????5 ???? - // ?????????????????????????
????????????????????????? default - //?????????????????
???????????? 0 ???????? 5 ???? ?????????? - // x ???????????????????
?????????????????????????????????????????????????
6?????????????????? Java ?????? initializer list
- ?????????????????????? x ???????????? 1 2 ??? 3
??????? - int x 1,2,3
-
- ??? 1,2,3??? initializer list ???????
7??????????????????? Object
- ?????????????? type MyObject ?????????????????????
? 3 ????????????? - MyObject a new MyObject3
- ??????????????????????????????????????????????????
??? null
a
8???????????????????
a
9??????????????????? (??? initializer list)
10Matrix
- ?????????????? 2 ?????????????????????????????????
???? ?????? index ????????????????????? 2 ??????? - ?????????????????????????????????????????
11?????????? initialize ?????????????????????
- int y new int 2
- ??? index ????????????????????????
- ????????????????? 2 ???????? null ??????
- ??????????????? ????????? initialize
??????????????????????????????????????????????????
????????? - ???????????????????????? initialize
????????????????????? - y0 new int2
- y1 new int3
- ??????????????????????????????????????????????????
12Bubble Sort
- ????????????????????????????????
??????????????????????????????????????????????????
?
- ??????????????????????????????????????????????????
???(????????????????????????????????)
??????????????????????????????
2
5
4
3
1
- ?????????????????????????? ???????????????????????
?????????? ???????????
13Running time ??? Bubble Sort
- ??????????????????????????????????????????????????
???? - ?????????????????????????????????????????????????
- ??????????????????????????????????????????????????
n ?????
5
2
4
3
1
Big O O(n2)
- n-1 ???????????????????????????????????????????
(5) ????????????????????????? ????????????????????
? (1) ??????????????????????????????????????? - ????????????????????????????? n-1 ???
14- 1 public static void bubblesort(int
array) - 2 for (int pass 1 passltarray.length-1
pass) - 3 for(int element0 elementlt
array.length 2 element) - 4 if(arrayelement gt
arrayelement1) - 5 swap(array, element,
element 1) - 6
?????????????????? 1 ???
1 public static void swap(int array, int
a, int b) 2 int temp arraya 3
arraya arrayb 4
arrayb temp 5
15Worst case ??? Bubble Sort (??????????????????????
???)
???????????
?????????????????????? n-1 ????? ???????????????
n-1 ?????
??????2 ???????????????? n-1 ???????????????
?????????????????? n-2 ?????
16Worst case ??? Bubble Sort (???)
- ?????????????????????? n-1 ???????????????
??????????????????? n-1 ??? - ?????????????????????????????????????????????
(n-1)2 ????? - ??????????????????????????????? (n-1) (n-2)
(n-3) 1 n(n-1)/2 ????? - bubble sort (worst case)
- (n-1)2
n(n-1)/2 unit time ????????????? - (n-1)2
n(n-1)/2 3 - (5n2 7n
2) /2
17Selection Sort
- ??????? index ?????????????????????????? a
?????????????? maxindex - ????????????????????????????????????????
???????????????????????? amaxindex
??????????????? maxindex ??????? index ???????
????????????????? - ????????????????????????????????????? amaxindex
(?????????????????????????????)
??????????????????????????????????? - ????? 1 3 ???? ????????? n-1 ???????????
??????????? ??????????????????????
18???????? Selection Sort
??????????? n-1 ????? ??????? 1 ?????
??????????? n-2 ????? ??????? 1 ?????
19Selection Sort (Code)
Big O O(n2)
- public static void selectionSort(int a)
- int maxindex //index of the largest value
- for(int unsorted a.length unsorted gt 1
unsorted--) - maxindex 0
- for(int index 1 index lt unsorted index)
- if(arraymaxindex lt arrayindex)
- maxindex index
-
- if(amaxindex ! aunsorted -1)
- swap(array, maxindex, unsorted -1)
-
-
???????????????????????
??????????????? 1 ???????? update maxindex
???????????????????????????
20Selection Sort (worst case)
- ??????????????????????????????????????????????
assign ???????????????????????????????????? - ?????????????????????????????????????? ???????
????????????????????? ????????????????????????????
?? ???? 2,3,4,5,1 ???????????? - ?????????????????????????????? (n-1) (n-2)
1 ????? - ??????????????????????????????????????????????????
? (n-1) ????? - ??????????????????????????? n-1 ?????
?????? (n2 7n -8) /2 ???????????? bubble sort
????? n ???? 3 ??????
21Insertion Sort
- ?????????????????????????????? ???????????????????
????????????????????? (???????????????????????????
?????????????????????????????) - ????????????????????????????????????????
??????????????????????????????????????????????
????????????????????????????????????? - ???????? 1 ??????????????????????????????? 1 ???
???????????
22???????? Insertion Sort
??????????????????????????
??????? 5 ????????????? (?????????? 6
????????????? 5 ??????)
?????? 5 ??? 6 ??????? 1 ???? ??????? 3 ??????????
23???????? Insertion Sort (???)
24Big O O(n2)
- public static void insertionSort(int a)
- int index
- for(int numSorted 1 numSorted lt a.length
numSorted) - int temp anumSorted
- for(index numSorted index gt0 index--)
- if(templt aindex-1)
- aindex aindex 1
- else
- break
-
-
- aindex temp
-
??????????????????????????
????????????????? 6 ??? 5 ???????? ????? 6
????????? 1 ???? ??? 5 ??????????? 1 ????
??????????????????????????? 3 ???????? 5 ???????
25Insertion Sort (worst case)
- Worst case ???????????????????????????????????????
??????????? - ????????????????????????????????????
- ???????? ?????????????????????????????????????????
??????? 1 ???????
26Insertion Sort (worst case ???)
- unit time ??? worst case insertion sort
- (12..n-1)2 n-1
- n(n-1) n-1
- (n1)(n-1) n2 1
???????? selection sort ????? n ???????????? 6
????????
27Insertion Sort (??????????)
- ???????????????????????????? i
- ??? ai ??????????????????????????????
???????????????????? templt aindex-1
??????????????????????? - ??? ai ??????????????????????????????????
??????????????????????????????????????????????????
? i ????? - ??????? i ????????? 2 ?????????????????????? a2
??? a1 (???????????????????????) - ?????????????????????????? a1 ????? a2
??????????????????????????? a2 ??? a0 ????
28Insertion Sort (?????????? ???)
- ?????? ??????????????????????????????????????
???????????????? i ??????????????????????????????
- ?????
- ??????????????????? ??????????????????????????????
????????????????? - ?????
- ??????????????????????????????????????????????????
??? worst case
29Merge Sort
- ??????????????????????? ??????????????????????????
????????????? - (???????????????????????????????? ??????
recursion ???) - ??????????????????????????????????????????
30???????????????? merge sort (??????? 1)
- ?????????? ??????????? a (1,5,8,9) ??? b
(2,4,6,7) - ????? counter ??????? index ?????????????????????
?????????????????????????????????????????
??????????????????????????? c
c
b
a
1
5
8
9
7
6
4
2
indexB
indexC
indexA
31???????????????? merge sort (??????? 2)
- ??????????? aindexA ??? bindexB
????????????????? ??? cindexC ??????????
counter ??????????????????????? c
????????????????????????????????
c
b
a
1
5
8
9
7
6
4
2
1
indexB
indexA
indexC
32???????????????? merge sort (??????? 3)
- ??????????? aindexA ??? bindexB
????????????????????????????????? ???????? b
?????????
c
b
a
1
5
8
9
7
6
4
2
1
2
4
5
7
6
indexB
indexC
indexA
- ????????????????????????????????????? a ??? c
?????? - ????????????????????????????????
33Worst case ?????????????????
- Worst case ????????????????????
??????????????????????????????????????????????????
(a ??? b) ????????????????????????????????? - ????????????????????????????? n-1 ????? (n
?????????????????????????) ???????????
?????????????????????????????????????? O(n)
34?????????????????????
- public static int merge(int a, int b)
- int aIndex 0 int bIndex 0 int cIndex
0 - int aLength a.length int bLength
b.length - int cLength aLength bLength
- int c new intcLength
- //??????????????????? a ??? b
???????????????? c ?????????????????????????? - while((aIndex lt aLength) (bIndex lt
bLength) - if(aaIndexltbbIndex)
- ccIndex aaIndex
- aIndex
- else
- ccIndex bbIndex
- bIndex
-
- cIndex
-
?????
35?????????????????????(???)
- //????????????????????????????????? c ??????
- if(aIndex aLength) //??? a ?????????????
- while(bIndexltbLength)
- ccIndex bbIndex
- bIndex
- cIndex
-
- else //??? b ?????????????
- while(aIndexltaLength)
- ccIndex aaIndex
- aIndex
- cIndex
-
-
- return c
36????????????????????????
- ????????????????????????????????????????? ?????
- ????????????????????????????? ????????????????????
????????????????????????????? ????????????????????
??????????????????????????????????????????? - ?????? ???????????????????????????????????????????
??????????????????
37???????????????????????????????
- 1 public static int mergeSort(int
unsort, int left, int right) - 2 if(left right)//??????????????
??????????? ??????????????????????? 1 - 3 int x new int1
- 4 x0 unsortleft
- 5 return x
- 6
- 7 else if(leftltright)//????????????
??????????????????? - 8 int center (leftright)/2
- 9 int result1
mergeSort(unsort,left,center) - 10 int result2
mergeSort(unsort,center1,right) - 11 return merge(result1,result2)
- 12
- 13
38????????????????? merge sort
- ??????????????????????????????????????????????????
???? (??????? 1 ???) - ?????????????????????? ???????????????????????????
????????????? ?????????????? ?????????????????????
???????????????????????????????? (???????? O(n))
39????????????????? merge sort (???)
(1)
- ???????????????????????????? ????????????? n
????????? ????????????????????????
40????????????????? merge sort (??? 2)
(2)
(3)
(x)
41????????????????? merge sort (??? 3)
- ??????????????? (1) ??? (x) ???????? ?????
- ????????? merge sort ?????????????????????????????
???????????????????????? - ??????????????????????????????????????????????????
??????????????????????????????????????????????????
?????
42Quick Sort
- 1. ??????????????????????????????????????
??????????????????????????????????
??????????????????????????????????????????????? - 2. ????????????????????????????????????
??????????????????????? (??????????????????
pivot) - 3. ??? pivot ???????? ????????????????????
????????? pivot ????????????????? pivot
??????????????????????????????? pivot
???????????????? pivot (?????????????????????????p
ivot ????????????????? ???????????????????????????
???????????????????????????????????????????)
????????????????????? partition - 4. ????????? pivot ??????????????????????????
????????????? ?????????????????????? ??quick sort
???????????????????????????????????? pivot - 5. ????????????????????????????????
quicksort(????????) ??? pivot ???
quicksort(???????)
43???????????? quick sort
??????????????? pivot
????? ???????????????????????? 4
????? quick sort
????? quick sort
????????????? ??????????
44??????? 1 ???????????????????????
- ??????????????????????????????????????????????????
?????????????????????? ???????????????????????????
???????????????????? - ??????????????????????????????????????????????????
??(???????? 20) ??? insertion sort
??????????????? ????????????????????????????
??????????????????????????????????????????????????
???????
45??????? 2 ???????? pivot
- ???????????????????????????? pivot ???????
- ???????? ???????????????????????????????(?????????
??????????????????????????) ??????????????????????
????????????????????????????????????????????
???????????????????
46???????? pivot ???????? (?????????????????????????
?)
pivot
????????????
????????????????????????????? ????????????????????
???????????????????????? ????????????????????????
47???????? pivot ?????
- ????? pivot?????? random number
???????????????????????? partition ???????????? - ???????????????????????????????????? random
number ?????????????????????????? - ??????????????????????????????????????????????????
?????? - ???????????? pivot ???????????????????????????????
??????????????????????????????????????????????????
? - ?????????????????????????????????????????
??????????????????????????????????????????????????
??????????????????????????? ???????????????????
48???????????? pivot ????? median of 3
- 1 private static int
pivotIndex(int a, int l, int r) - 2 int c (lr)/2
- 3 if((alltar algtac)
- 4 (algtar alltac))
- 5 return l
- 6 if((acltal acgtar)
- 7 (acgtal acltar)
- 8 return c
- 9 return r
- 10
49??????? 3 ??????? partition
- ??? pivot ?????????????????????????????
???????????????????????????????????????? - ????????? i ???? index ????????????????????????
????????? j ???? index ???????????????????????????
???? (????????????????????????????????????????
pivot ??????) - ???????????? i ??? j?????????????????? ?????????
i ??????????????? ????????????????????????????????
?????????? pivot ?????????????????????????????????
?????????????????????????????????? pivot - ??????????????? ????????? j ??????????????????????
????????????????????????? pivot
??????????????????????????????????????????????????
?????????????? pivot
50- ??? i ?????????????????? j ???????????????????????
???????????????????????? ????????????????????(????
????????? pivot)????????????? ????????????????????
???????????? ??????????? i ?????????????????????
j ?????????????? 8 - ?????? i ?????????????????? ??? ?????? j
??????????????????? ??????????????????????????????
??????????????? - ???????????????????? 3 ?????????????
- ?????????????????? ???????????????????????????????
?? i ??? pivot ???????????????????? pivot
??????????????????????? ??????????????????????????
???????? ????????????????????????????????????
51??????????????? partition
pivot
??????? pivot ?????????????
?????? i ??????????? j ?????????
????????????????????????????? ??????????? ai
??? aj
52??????????????? partition (???)
i
j
?????? i ??????????? j ?????????
i
j
??????? ai ??? aj
i,j
53??????????????? partition (??? 2)
i,j
?????? i ??????????? j ?????????
i
j
?????? i ??????????? j ????????????????????? i
??? pivot ????????????
3
2
0
4
1
8
9
6
5
7
??????????????? 4 ???????
?????????????? 4 ???????
54??????? partition ???????????????? pivot
??????? 1
- ??????????? i ???? ??? j ?????????????
????????????????????????? ????????
??????????????? pivot ????????????????????????????
?????????
pivot
j
i
?????? i ??????????? j ?????????
i
j
??????? ai ??? aj
i
j
55??????? partition ???????????????? pivot ???????
1 (???)
i
j
?????? i ??????????? j ?????????
i
j
??????? ai ??? aj
i
j
pivot ?????????????????
56??????? partition ???????????????? pivot ???????
2
- ??????? i ??? j ????????????????? pivot ????????
- ???????????? ????? ??????????????????????????????
???
i
j
i ??? j ????????????????????? ????? pivot
????????????????????? ??????????????????????
57??????? partition ???????????????? pivot ???????
3
- ??? i ??? j ??????????????????????????????????
pivot - ????????? ????????????????????????????????????????
?????????? - ????????????????????????????? ????????????????????
??????????? - ??????????????? ??????????????????????????????????
?? ????????????????????? - ??????????????????????????????????????????????????
???????????????????????????????????????
58??????? quick sort
- private static void quicksort(int a,int l, int
r) - if(lCUTOFFltr)
-
- //?????????????? pivot
- int pIndex pivotIndex(a,l,r)
-
- //??? pivot ?????????????? ???????????????????
??? - swap(a,pIndex,r)
- int pivot ar
59- //????????????????
- int il, jr-1
- for( )
-
- while(iltr ailtpivot)i
- while(jgtl ajgtpivot)j--
- if(iltj)
- swap(a,i,j)
- i
- j--
- else //??? i ??? j ????????????????????
???????????? - break
-
????????? index ?????????????? ?????????????????
???????
60- //??????? pivot ???????????????
- swap(a,i,r)
-
- //????? quick sort ??????????????????????
- quicksort(a,l,i-1)
- quicksort(a,i1,r)
-
- else
- insertionSort(a,l,r)
-
61???? quicksort ?????????????
- ?????????????? quick sort ??????????????????
- ??????????? pivot
- ????????????????????????????? ????????
????????????????????????????????????? - ????????????? pivot ??????????????????????????????
???????????????????????(??????????????????????) - ????????? ???????????????????????????????????
- ????????????????????????????????? 2,3,4,,n-1,n,1
- ????????????????????????????
62????????????????? quick sort
- ?????????????? random pivot ????????? insertion
sort ??????????????????????? - ???? ??? T(n) ?????????????????????????????????
?????????? n ?????? T(0)1, T(1)1 - ???????????????? ???????????????????????????????
- ??????????? pivot -gt????? ??????????
- ????????????? partition ??????????????????????????
????? ???????????????????? cn - ??????????? quick sort ??????????
????????????????????????????? i
63????????????????? quick sort (???)
?????????????????????????????
64Worst case
- ??????????? pivot ??????????????????????????
?????????????? partition ?????????????????????????
????????????????????????????? ????????????????
1 ??????????
65Worst case (???)
?????????????? ?????
66Worst case (??? 2)
- ??????? ???? worst case ?????????????????????
sort ????????
67Best case
- ??????????????????????????????????????????????????
???? - ???????? ?????????????????????????????????
- ??????????????????????????????????????????????
merge sort - ???????????
68Best case (???)
?????????
69Best case (??? 2)
- ??????????????????? merge sort
70Average case
- ??????????????????????????????????? 0 ???? 1 ????
2 ???? 3 ?????????????? n-1 ?????????? - ???????????????????? n ?????? ????????????????????
?? pivot - ??????????????????????????????????????????????????
??????? ???????????????????????????????? 1/n - ??????? ??????????????????????????
71Average case (???)
- ??????????????????????? ??? n ???????????
(avg1)
(avg2)
72Average case (??? 2)
- ?????? c ?????????????????????? ???????????
n(n1) ????????
73Average case (??? 3)
??????????????????? ???????????????? ??????
74Average case (??? 4)
(avg3)
- ???????????????????? harmonic number
????????????????
75Average case (??? 5)
- ????????? harmonic number ???? avg3 ?????
- ????????????????????? ln n ????????
- ??????????? O(log n)
- ???????????????????? n1 ?????????????
???????????
76Bucket sort
- ?????????????????????????????????????????
?????????????????????????????????????????? - ??????????????????????????? 52 ??
?????????????????????????????????????? - ????????????????????????????????????
- ??????????????????????? ??????????????????????????
?????????????? - ????????????????????? ????????????????????????????
????? - ?????????????????????????????????? O(n) ????
- ???????????????????????????????? bucket (??????)
???????????????? ????? bucket ????????????????????
77 Bucket sort (???)
- ????????????????????? 1 ??? m ????? n ????? (nltm)
- ??????????????????????????????????????????????????
m ?????? - ????????????????????????????????????????? 0
- ???????????????????????????????????? ????????? k
?????????????? ak ??????? - ??????????????????????????????????????????????????
??? (???counter) - ??????????????????????????????????????????????????
- ?????????????????? O(n) ?????????????????????????
??? O(m) ??????????????????? - ??????????????????????????????????????? O(nm)
78Bucket sort (??? 2)
- ?????????????? bucket ????????????????????????????
???????? ?????????????????????????????? bucket
?????????? - ???? ???????????????????????????? 49 ??
- ?????????????????? ???????????????????????????????
?????????? (???????? bucket ???????)
??????????????????????????????????? - ?????????????????????? insertion sort ??????
??????????????????????????????????????????????
??????????????? - ??????????????????????????????????????????????????
???? bucket
79Radix sort
- ??????? bucket sort ????????
- ??????????? bucket sort ??????
- ????????????????????????????????????????????????
bucket - ?????????????????????????????
- (?????????????????????????????????????????????????
???????????????????????????????????????)
80Radix sort (???)
- ????????????????????????????????? ???????? bucket
??????????????????????????? - ????? bucket ??????????????????? 0
??????????????????????? 9 (??? bucket
?????????????????????) - 002
- 143,013
- 165
- 328
??????????????????
81Radix sort (??? 2)
- ???????????????????????????????? ???????? bucket
?????????? ??? - 002
- 013
- 328
- 143
- 165
??????????????????
82Radix sort (??? 3)
- ????? ?????????????????????????
?????????????????????? - ???????? bucket ??????????? ???
- 002,013
- 143,165
- 328
??????????????????
??????????????
83??????????????????? d ????????? n
- public static int digitTh(int n, int d)
- if (d 0)
- return n10
- else
- return digitTh(n/10,d-1)
???????? O(d)
84?????????????????????????? 10 bucket
????????????? d ????????????
- public static void bucketing(int data, int d)
- int i,j,value
-
- //?????????? 10 bucket ???????? bucket ????
- //vector (????????????????)
- Vector bucket new Vector10
- for(j0jlt10j)
- bucketj new Vector()
-
85- //????????? bucket ???????
- int n data.length
- for(i0iltni)
- value datai
- j digitTh(value,d)
- bucketj.add(new Integer(value))
-
86- //????????????????????????????????????????
- in
- for(j9jgt0j--)
- while(!bucketj.isEmpty())
- i- -
- value
- ((Integer)bucketj.remove()).intValue()
- dataivalue
-
-
87??????? radix sort
- public static void radixSort(int data, int
size) - for(int j0jltsize j)
- bucketing(data,j)
88????????????????????? Java
- public boolean equals(Object obj)
- ???????? x.equals(y) ????????? return true
????? x ??? y ?????????????????????????
?????????? return false - ?????????????????????????????????????????????
(reference ????????) - ??????????????????????????????
- ????????????? overwrite ??????????????????????????
?????????????????????????????????? ??????????????
???? String
89????????????????????? Java (???)
- Comparable Interface
- ??????????????????????????????????????????????????
???? - public int compareTo(Object o)
- ??????????? this object ??????????????? argument
- return ???????? this object ??????????????????????
? argument - return ????????? this object ???????????????
argument - ??? 0 ??? ??????????????????????????????????
90????????????????????? Java (??? 2)
- Comparator Interface
- ??????????????????????????????????????????????????
???? - public int compare(Object o1, Object o2)
- ??????????? o1 ??? o2 ????????????????????????????
- return ???????? o1 ?????????????o2
- return ????????? o1 ???????????? o2
- ??? 0 ??? o1 ??? o2 ????????????
91????????????????????? Java (??? 3)
- ?????? method ?????????????
- public boolean equals(Object obj)
- ????????????????????????? Comparator ??????(this)
?????????????? - ???????? return true ?????obj ???? Comparator
??????????????????????????????? this - ????????????????????????????????????? Comparator
??? - ????????????????? argument ???????????????????????
????????????????????????????????????????????
92?? ??????????????????????????????????