Struktur Data & Algoritme ( Data Structures & Algorithms ) - - PDF document

struktur data algoritme data structures algorithms
SMART_READER_LITE
LIVE PREVIEW

Struktur Data & Algoritme ( Data Structures & Algorithms ) - - PDF document

Struktur Data & Algoritme ( Data Structures & Algorithms ) Sorting Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2 .0


slide-1
SLIDE 1

1

Struktur Data & Algoritme ( Data Structures & Algorithms)

Denny (denny@cs.ui.ac.id) Suryana Setiawan (setiawan@cs.ui.ac.id)

Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5

Version 2 .0 - I nternal Use Only

Sorting

SDA/ SORT/ V2.0/ 2

Objectives

Memahami beberapa algoritme sorting dan dapat

menganalisa kompleksitas-nya

slide-2
SLIDE 2

2

SDA/ SORT/ V2.0/ 3

Outline

Beberapa algoritme untuk melakukan sorting Idea Example Running time for each algorithm

SDA/ SORT/ V2.0/ 4

Sort

Sorting = pengurutan Sorted = terurut menurut kaidah tertentu Data pada umumnya disajikan dalam bentuk sorted.

Why?

Bayangkan bagaimana mencari telepon seorang

teman dalam buku yang disimpan tidak terurut.

slide-3
SLIDE 3

3

SDA/ SORT/ V2.0/ 5

Bubble Sort: idea

bubble = busa/udara dalam air, so? Busa dalam air akan naik ke atas. Why? How?

  • Ketika busa naik ke atas, maka air yang di atasnya

akan turun memenuhi tempat bekas busa tersebut.

SDA/ SORT/ V2.0/ 6

Bubble Sort

40 2 1 43 3 65

  • 1

58 3 42 4 65 2 1 40 3 43

  • 1

58 3 42 4 65 58 1 2 3 40

  • 1

43 3 42 4 1 2 3 40 65

  • 1

43 58 3 42 4

1 2 3 4

Perhatikan bahwa pada setiap iterasi, dapat

dipastikan satu elemen akan menempati tempat yang benar

slide-4
SLIDE 4

4

SDA/ SORT/ V2.0/ 7

1

  • 1

3 2 65 3 43 58 42 40 4

Bubble Sort

  • 1

1 2 65 3 43 58 42 40 4 3

  • 1

1 2 65 3 43 58 42 40 4 3

6 7 8 Stop here… why?

1 2 3

  • 1

3 40 65 43 58 42 4

5

SDA/ SORT/ V2.0/ 8

Bubble Sort: algorithms

Algorithm (see jdk1.5.0_01\demo\applets\SortDemo)

void sort(int a[]) throws Exception { for (int i = a.length; --i>=0; ) { boolean swapped = false; for (int j = 0; j<i; j++) { ... if (a[j] > a[j+1]) { int T = a[j]; a[j] = a[j+1]; a[j+1] = T; swapped = true; } ... } if (!swapped) return; } }

slide-5
SLIDE 5

5

SDA/ SORT/ V2.0/ 9

Bubble Sort

Running time: Worst case: O(n2) Best case: O(n) -- when? why? Variant: bi-directional bubble sort

  • original bubble sort: hanya bergerak ke satu arah
  • bi-directional bubble sort bergerak dua arah (bolak

balik).

see jdk1.5.0_01\demo\applets\SortDemo

SDA/ SORT/ V2.0/ 10

Selection Sort: idea

Ambil yang terbaik (select) dari suatu kelompok,

kemudian diletakkan di belakang barisan

Lakukan terus sampai kelompok tersebut habis

slide-6
SLIDE 6

6

SDA/ SORT/ V2.0/ 11

42 40 2 1 3 3 4

  • 1

65 58 43 40 2 1 43 3 4

  • 1

42 65 58 3 40 2 1 43 3 4

  • 1

58 3 65 42 40 2 1 43 3 65

  • 1

58 3 42 4

Selection Sort

SDA/ SORT/ V2.0/ 12

42 40 2 1 3 3 4 65 58 43

  • 1

42

  • 1

2 1 3 3 4 65 58 43 40 42

  • 1

2 1 3 3 4 65 58 43 40 42

  • 1

2 1 3 4 65 58 43 40 3

Selection Sort

slide-7
SLIDE 7

7

SDA/ SORT/ V2.0/ 13

1 42

  • 1

2 1 3 4 65 58 43 40 3 42

  • 1

3 4 65 58 43 40 3 2 1 42

  • 1

3 4 65 58 43 40 3 2 1 42 3 4 65 58 43 40 3 2

  • 1

1 42 3 4 65 58 43 40 3 2

  • 1

Selection Sort

SDA/ SORT/ V2.0/ 14

Selection: algoritme

void sort(int a[]) throws Exception { for (int i = 0; i < a.length; i++) { int min = i; int j; /* Find the smallest element in the unsorted list */ for (j = i + 1; j < a.length; j++) ... } if (a[j] < a[min]) { min = j; } ... }

slide-8
SLIDE 8

8

SDA/ SORT/ V2.0/ 15

Selection: algoritme (2)

/* Swap the smallest unsorted element into the end of the sorted list. */ int T = a[min]; a[min] = a[i]; a[i] = T; ... } }

SDA/ SORT/ V2.0/ 16

Selection Sort: analysis

Running time: Worst case: O(n2) Best case: O(n2) Based on big-oh analysis, is selection sort better

than bubble sort?

Does the actual running time reflect the analysis?

slide-9
SLIDE 9

9

SDA/ SORT/ V2.0/ 17

40 2 1 43 3 65

  • 1

58 3 42 4 2 40 1 43 3 65

  • 1

58 3 42 4 1 2 40 43 3 65

  • 1

58 3 42 4 40

Insertion Sort

Idea: mengurutkan kartu-kartu

SDA/ SORT/ V2.0/ 18

1 2 3 40 43 65

  • 1

58 3 42 4 1 2 40 43 3 65

  • 1

58 3 42 4 1 2 3 40 43 65

  • 1

58 3 42 4

Insertion Sort

slide-10
SLIDE 10

10

SDA/ SORT/ V2.0/ 19

1 2 3 40 43 65

  • 1

58 3 42 4 1 2 3 40 43 65

  • 1

58 3 42 4 1 2 3 40 43 65 58 3 42 4 1 2 3 40 43 65

  • 1

Insertion Sort

SDA/ SORT/ V2.0/ 20

1 2 3 40 43 65 58 3 42 4 1 2 3 40 43 65

  • 1

1 2 3 40 43 65 58 42 4 1 2 3 3 43 65

  • 1

58 40 43 65 1 2 3 40 43 65 42 4 1 2 3 3 43 65

  • 1

58 40 43 65

Insertion Sort

1 2 3 40 43 65 42 1 2 3 3 43 65

  • 1

58 4 43 65 42 58 40 43 65

slide-11
SLIDE 11

11

SDA/ SORT/ V2.0/ 21

Insertion Sort: ineffecient version

Insertion sort untuk mengurutkan array integer

public static void insertionSort (int[] a) { for (int ii = 1; ii < a.length; ii++) { int jj = ii; while (( jj > 0) && (a[jj] < a[jj - 1])) { int temp = a[jj]; a[jj] = a[jj - 1]; a[jj - 1] = temp; jj--; } } }

Perhatikan: ternyata nilai di a[jj] selalu sama ⇒

dapat dilakukan efisiensi di sini.

SDA/ SORT/ V2.0/ 22

Insertion Sort

Insertion sort yang lebih efisien

public static void insertionSort2 (int[] a) { for (int ii = 1; ii < a.length; ii++) { int temp = a[ii]; int jj = ii; while (( jj > 0) && (temp < a[jj - 1])) { a[jj] = a[jj - 1]; jj--; } a[jj] = temp; } }

slide-12
SLIDE 12

12

SDA/ SORT/ V2.0/ 23

Insertion Sort

Running time analysis: Worst case: O(n2) Best case: O(n) Is insertion sort faster than selection sort? Notice the similarity and the difference between

insertion sort and selection sort.

SDA/ SORT/ V2.0/ 24

Mergesort

Divide and Conquer approach Idea: Merging two sorted array takes O(n) time Split an array into two takes O(1) time

1 2 3 40 43 65

  • 1

3 4 42 58

slide-13
SLIDE 13

13

SDA/ SORT/ V2.0/ 25

Mergesort: Algorithm

If the number of items to sort is 0 or 1, return. Recursively sort the first and second half separately. Merge the two sorted halves into a sorted group.

SDA/ SORT/ V2.0/ 26

40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 split

Mergesort

slide-14
SLIDE 14

14

SDA/ SORT/ V2.0/ 27

40 2 1 43 3 65

  • 1

58 3 42 4 2 1 3 65

  • 1

58 42 4 40 1 2 43 3 65

  • 1

58 3 4 42 split merge 1 2 40 3 43 65

  • 1

58 3 4 42

Mergesort

SDA/ SORT/ V2.0/ 28

merge 1 2 40 3 43 65

  • 1

58 3 4 42 1 2 3 40 43 65

  • 1

3 4 42 58

  • 1

1 2 3 3 4 40 42 43 58 62

Mergesort

slide-15
SLIDE 15

15

SDA/ SORT/ V2.0/ 29

Merge Sort: implementation

Implement operation to merge two sorted arrays into

  • ne sorted array!

public static void merge (int [] array, int lo, int high) // assume: // mid = (lo + high) / 2; // array [lo..mid] and [mid+1..high] are sorted { }

SDA/ SORT/ V2.0/ 30

Merge Sort: implementation

There are two ways to merge two sorted array: in place merging using extra place merging

slide-16
SLIDE 16

16

SDA/ SORT/ V2.0/ 31

Merge Sort: analysis

Running Time: O(n log n) Why?

SDA/ SORT/ V2.0/ 32

Quicksort

Divide and Conquer approach Quicksort(S) algorithm: If the number of items in S is 0 or 1, return. Pick any element v in S. This element is called the

pivot.

Partition S – {v} into two disjoint groups:

  • L = { x ∈ S – { v} | x ≤ v} and
  • R = { x ∈ S – { v} | x ≥ v}

Return the result of Quicksort(L), followed by v,

followed by Quicksort(R).

slide-17
SLIDE 17

17

SDA/ SORT/ V2.0/ 33

40 2 1 3 43 65

  • 1

58 3 42 4

Quicksort: select pivot

SDA/ SORT/ V2.0/ 34

2 1 3 43 65

  • 1

58 3 42 4 40

Quicksort: partition

slide-18
SLIDE 18

18

SDA/ SORT/ V2.0/ 35

2 1 3 43 65

  • 1

58 3 42 4 40 2 1 3 43 65

  • 1

58 3 42 4 40

Quicksort: recursive sort & merge the results

SDA/ SORT/ V2.0/ 36

40 2 1 43 3 65

  • 1

58 3 42 4 left 40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 40 right

Quicksort: partition algorithm 1

slide-19
SLIDE 19

19

SDA/ SORT/ V2.0/ 37

40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 left right

Quicksort: partition algorithm 1

SDA/ SORT/ V2.0/ 38

  • 1

3 2 1 3 4 40 42 43 58 65

  • 1

2 1 3 3 42 2 1 3

  • 1

3 43 42 65 2 1 3

  • 1

3 4 42 65 58 43 40 2 1 43 3 65

  • 1

58 3 42 4 40 2 1 43 3 65

  • 1

58 3 42 4 3 3 2 3 2 3

Quicksort: partition algorithm 1

slide-20
SLIDE 20

20

SDA/ SORT/ V2.0/ 39

Quicksort: partition algorithm 2

40 2 1 43 3 65

  • 1

58 3 42 4 40

  • riginal

pivot = 40

left++ while < pivot right-- while >= pivot

4 2 1 43 3 65

  • 1

58 3 42 40 40 2 1 43 3 65

  • 1

58 3 42 4 right left 40 2 1 3 3 65

  • 1

58 43 42 4 right left left right

SDA/ SORT/ V2.0/ 40

Quicksort: partition algorithm 2

40 2 1 3 3 65

  • 1

58 43 42 4 right left 40 2 1 3 3

  • 1

65 58 43 42 4 right left 40 2 1 3 3

  • 1

65 58 43 42 4 right left

CROSSI NG! sort sort

slide-21
SLIDE 21

21

SDA/ SORT/ V2.0/ 41

Quicksort: algoritme

static void QuickSort(int a[], int lo0, int hi0) { int lo = lo0; int hi = hi0; int pivot; // base case if ( hi0 <= lo0) { return; } pivot = a[lo0];

SDA/ SORT/ V2.0/ 42

// loop through the array until indices cross while( lo <= hi ) { /* find the first element that is greater than

  • r equal to the partition element starting

from the left Index. */ while( ( lo < hi0 ) && ( a[lo] < pivot )) { ++lo; } /* find an element that is smaller than the partition element starting from the right Index. */ while( ( hi > lo0 ) && ( a[hi] >= pivot )) {

  • -hi;

} // if the indexes have not crossed, swap if( lo <= hi ) { swap(a, lo, hi); ++lo;

  • -hi;

} }

slide-22
SLIDE 22

22

SDA/ SORT/ V2.0/ 43

/* If the right index has not reached the left side

  • f array must now sort the left partition.

*/ if (lo0 < hi) { QuickSort( a, lo0, hi ); } /* If the left index has not reached the right side

  • f array must now sort the right partition.

*/ if( lo < hi0 ) { QuickSort( a, lo, hi0 ); } }

SDA/ SORT/ V2.0/ 44

Quicksort: analysis

Partitioning takes O(n) Merging takes O(1) So, for each recursive call, the algorithm takes O(n) How many recursive calls does a quick sort need?

slide-23
SLIDE 23

23

SDA/ SORT/ V2.0/ 45

Quicksort: selecting pivot

Ideal pivot: median element Common pivot First element Element at the middle Median of three

SDA/ SORT/ V2.0/ 46

40 2 1 43 3 65

  • 1

58 3 42 4 Original: 5-sort: Sort setiap item yang berjarak 5: 40 2 1 43 3 65

  • 1

58 3 42 4

Shellsort

slide-24
SLIDE 24

24

SDA/ SORT/ V2.0/ 47

40 2 1 43 3 65

  • 1

58 3 42 4 Original: 40

  • 1

43 3 42 2 1 58 3 65 4 After 5-sort: 2

  • 1

3 1 4 40 3 42 43 65 58 After 3-sort:

Shellsort

After 1-sort: 1 2 3 40 43 65 42 1 2 3 3 43 65

  • 1

58 4 43 65 42 58 40 43 65

SDA/ SORT/ V2.0/ 48

Shell's Odd Gaps Only Dividing by 2.2 1000 122 11 11 9 2000 483 26 21 23 4000 1936 61 59 54 8000 7950 153 141 114 16000 32560 358 322 269 32000 131911 869 752 575 64000 520000 2091 1705 1249 Shellsort N Insertion Sort

O(N3/2) O(N5/4) O(N7/6)

Performance of Shellsort

slide-25
SLIDE 25

25

SDA/ SORT/ V2.0/ 49

Generic Sort

Bagaimana jika diperlukan method untuk

mengurutkan array dari String, array dari Lingkaran (berdasarkan radiusnya) ?

Apakah mungkin dibuat suatu method yang bisa

dipakai untuk semua jenis object?

Ternyata supaya object bisa diurutkan, harus bisa

dibandingkan dengan object lainnya (mempunyai behavior bisa dibandingkan - comparable ⇒ method).

Solusinya: Gunakan interface yang mengandung method yang

dapat membandingkan dua buah object.

SDA/ SORT/ V2.0/ 50

Generic Sort

REVIEW: Suatu kelas yang meng-implements sebuah

interface, berarti kelas tersebut mewarisi interface (definisi method-method) = interface inheritance, bukan implementation inheritance

Dalam Java, terdapat interface

java.lang.Comparable

method: int compareTo (Object o)

slide-26
SLIDE 26

26

SDA/ SORT/ V2.0/ 51

Other kinds of sort

Heap sort. We will discuss this after tree. Postman sort / Radix Sort. etc.

SDA/ SORT/ V2.0/ 52

Further Reading

http://telaga.cs.ui.ac.id/WebKuliah/IKI101

00/resources/animation/

Chapter 8: Sorting Algorithm

slide-27
SLIDE 27

27

SDA/ SORT/ V2.0/ 53

What’s Next

Recursive (Chapter 7)