Download presentation
Presentation is loading. Please wait.
Published byEsmeralda Heart Modified over 9 years ago
1
SORTING Matakuliah: T0974 / Algoritma dan Metode Object Oriented Programming I Tahun: 2008 Versi: 1/0
2
Bina Nusantara Learning Outcomes At the end of this session, student can: Explain sorting definition Simulate algorithm of sorting Using sorting in making program
3
Bina Nusantara Outline Sorting Definition Bubble Sort Selection Sort Insertion Sort
4
Bina Nusantara Sorting Definition Sorting of number, alphabet, word or other value with certain rule Illustrate problem solving Technique of using selection, looping, method and array Demonstrate perform/algorithm complexity Accelerate searching process
5
Bina Nusantara Sorting Algorithm Basic sorting algorithm: –Bubble Sort –Insertion Sort –Selection Sort Advanced sorting algorithm: –Merge Sort –Quick Sort –Bucket Sort –Shell Sort –Radix Sort –External Sort
6
Bina Nusantara Bubble Sort
7
Bina Nusantara Bubble Sort Also call as exchange sort Ascending sorting from small to big Descending sorting from big to small Value into array Adjacent value compared If increasing, then change to become decreasing At round: –1, array at 1 (index 0) is a smallest value –2, array at 2 (index 1) is a second smallest value –n-1, array at n (index n-1) is a biggest value Number of round = n-1
8
Bina Nusantara Bubble Sort Bubble sort ascending
9
Bina Nusantara Bubble Sort
10
Bina Nusantara Bubble Sort Sorting ascending Sorting descending
11
Bina Nusantara Bubble Sort
12
Bina Nusantara Bubble Sort
13
Bina Nusantara Bubble Sort
14
Bina Nusantara Bubble Sort
15
Bina Nusantara Bubble Sort
16
Bina Nusantara Bubble Sort
17
Bina Nusantara Bubble Sort
18
Bina Nusantara Bubble Sort
19
Bina Nusantara Bubble Sort
20
Bina Nusantara Bubble Sort
21
Bina Nusantara Bubble Sort
22
Bina Nusantara Bubble Sort
23
Bina Nusantara Bubble Sort
24
Bina Nusantara Bubble Sort
25
Bina Nusantara Bubble Sort
26
Bina Nusantara Bubble Sort
27
Bina Nusantara Bubble Sort
28
Bina Nusantara Bubble Sort
29
Bina Nusantara Bubble Sort
30
Bina Nusantara Bubble Sort
31
Bina Nusantara Selection Sort
32
Bina Nusantara Selection Sort Value sent into array Search for the biggest value put at the end of array At round: –1, array at 1 (index 0) is a small value –2, array at 2 (index 1) is a second small value –n-1, array at n (index n-1) is a biggest value Number of round = n-1
33
Bina Nusantara Selection Sort Selection sort ascending Selection sort descending
34
Bina Nusantara Selection Sort
35
Bina Nusantara Selection Sort
36
Bina Nusantara Selection Sort
37
Bina Nusantara Selection Sort
38
Bina Nusantara Insertion Sort
39
Bina Nusantara Insertion Sort Value sent into array Using helper container Value compared with index previously Every round did not produce the biggest value or the smallest value. Number of round = n-1
40
Bina Nusantara Insertion Sort Insertion sort ascending Insertion sort descending
41
Bina Nusantara Insertion Sort
42
Bina Nusantara Did You Know? If number already sort, Bubble sort keep do the checking Causes long time execution Solusi Bubble Flag
43
exercise Buatlah program BubbleSort Lengkap dengan menggunakan Flag Bina Nusantara
44
answer public class BubbleSortFlag { public static void swap(int[] data,int i, int j) { int temp=data[i]; data[i]=data[j]; data[j]=temp; } public static void BubbleFlag(int[] data) { boolean needNextPass=true; for (int i=1;i<data.length && needNextPass;i++){ for (int j= data.length-1;j>=1;j--) { if (data[j-1]>data[j]) { swap(data,j-1,j); needNextPass=true; }}}} Bina Nusantara public static void main(String[] args) { int []data= {6,4,3,2,1,10,11,15}; System.out.print ("Nilai sebelum diurutkan :"); for (int a:data) System.out.print(a+ " "); BubbleFlag(data); System.out.println(); System.out.print("Nilai sesudah diurutkan: "); for (int a: data) System.out.print (a+ " " ); System.out.println (); }
45
Bina Nusantara Reference Introdution to Java Programming. 7ed. Liang. 2009. p227-230, p864-866 Bubble Sort Algorithm in Java. http://www.geekpedia.com/tutorial272_Bu bble-Sort-Algorithm-in-Java.html http://www.geekpedia.com/tutorial272_Bu bble-Sort-Algorithm-in-Java.html
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.