# SORTING Matakuliah: T0974 / Algoritma dan Metode Object Oriented Programming I Tahun: 2008 Versi: 1/0.

## Presentation on theme: "SORTING Matakuliah: T0974 / Algoritma dan Metode Object Oriented Programming I Tahun: 2008 Versi: 1/0."— Presentation transcript:

SORTING Matakuliah: T0974 / Algoritma dan Metode Object Oriented Programming I Tahun: 2008 Versi: 1/0

Bina Nusantara Learning Outcomes At the end of this session, student can: Explain sorting definition Simulate algorithm of sorting Using sorting in making program

Bina Nusantara Outline Sorting Definition Bubble Sort Selection Sort Insertion Sort

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

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

Bina Nusantara Bubble Sort

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

Bina Nusantara Bubble Sort Bubble sort ascending

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort Sorting ascending Sorting descending

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Bubble Sort

Bina Nusantara Selection Sort

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

Bina Nusantara Selection Sort Selection sort ascending Selection sort descending

Bina Nusantara Selection Sort

Bina Nusantara Selection Sort

Bina Nusantara Selection Sort

Bina Nusantara Selection Sort

Bina Nusantara Insertion Sort

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

Bina Nusantara Insertion Sort Insertion sort ascending Insertion sort descending

Bina Nusantara Insertion Sort

Bina Nusantara Did You Know? If number already sort, Bubble sort keep do the checking Causes long time execution Solusi  Bubble Flag

exercise Buatlah program BubbleSort Lengkap dengan menggunakan Flag Bina Nusantara

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 (); }

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