• Tidak ada hasil yang ditemukan

Sorting Bubble Sort, Selection Sort.

N/A
N/A
Protected

Academic year: 2023

Membagikan "Sorting Bubble Sort, Selection Sort."

Copied!
42
0
0

Teks penuh

(1)

Sorting Bubble Sort, Selection Sort.

Course Code:CSE221 Course Title: Algorithms Program: B.Sc. in CSE

Course Teacher: Masud Rabbani Designation: Lecturer

Email: [email protected] Week-03, Lecture-02

(2)

Bubble

sort

(3)

Bubble sort

This is a simple sorting algorithm.

The Input to this algorithm will be like this

Enter how many numbers you want to sort in

Increasing order: 6

Enter the numbers to be sorted:

1,3,5,2,4,6

(4)

The output of this algorithm will be like this:

The result after sorting your numbers in increasing order is:

1,2,3,4,5,6

(5)

The array of data to be sorted is 1,3,5,2,4,6 (in increasing order) The process of “Bubble sorting”

1 < 3 no swapping

1 3 5 2 4 6

1 3

(6)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

3<5 no swapping

1 3 5 2 4 6

(7)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

5 > 2 swapping

1 3 5 2 2 5 4 6

(8)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

5 > 4 swapping

1 3 2 6 5 4 4 5

(9)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

5 < 6 no swapping

1 3 2 4 5 6

(10)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

2

nd

pass 1 < 3 no swapping

2 4 5 6

1 3

(11)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

2

nd

pass 3 > 2 swapping

1 4 5 6 3 2 2 3

(12)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

2

nd

pass 3 < 4 no swapping

1 2 3 4 5 6

(13)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

3

rd

pass 1 < 2 no swapping

3 4 5 6

1 2

(14)

The array of data to be sorted is 1,3,5,2,4,6

The process of “Bubble sorting”

The result after “Bubble sorting” is

1 2 3 4 5 6

(15)

ALGORITHM:

Bubble sort ( array, n)

{ n is the number of elements

for ( i = 1 to n-1 ) to be sorted {

flag = 0 ; if i = 1 then j = i + 1 = 2 for ( j = i + 1 ; j <= n ; j++ )

{ if ( array ( i ) > array ( j ) ) if first num > second num { Swap( array ( i ) , array ( j ) ) ;

flag = 1 ;

} the value which is in i will be assigned to j } the value which is in j will be assigned to i }

}

(16)

SELECTION SORT

(17)

Description

A sorting technique that is typically used for

sequencing small lists.

(18)

The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It

“swaps” this with the first element in the list. Next it finds the smallest of the remaining elements, and

“swaps” it with the second element.

(19)

The Selection Sort Algorithm

For each index position i

– Find the smallest data value in the array from positions i through length - 1, where

length is the number of data values stored.

– Exchange (swap) the smallest value with the value at position i.

(20)

A Selection Sort Example

We start by searching for the smallest element in the List.

6

2 1

3 5 4

Smallest?

(21)

6

2 1

3 5 4

Smallest?

(22)

6

2 1

3 5 4

Smallest!

(23)

6

2 1

3 5 4

Swap!

(24)

1 2

6 3 5 4

Swapped!

(25)

1

2

6 3 5 4

Smallest?

After the smallest element is in the first position, we continue searching with

the second element and look for the next smallest element.

(26)

1

2

6 3 5 4

Smallest!

In this special case, the next smallest element is in the second position

already. Swapping keeps it in this position.

(27)

1

2

6 3 5 4

Swapped

Swapping keeps it in this position.

(28)

1

2

6 3 5 4

Smallest?

After the next smallest element is in the second position, we continue searching

with the third element and look for the next smallest element.

(29)

1

2

6 3 5 4

Smallest!

(30)

Swap!

1

2

6

3

5

4

(31)

1

2

3 6 5 4

Swapped

(32)

1

2

3 6 5 4

Smallest?

(33)

1

2

3 6 5 4

Smallest?

(34)

1

2

3 6 5

Swap! 4

(35)

1

2

3 6 5

Smallest! 4

(36)

1

2

3 4 5

Swapped 6

(37)

The last two elements are in order, so no swap is necessary

1

2

3

4

5

6

(38)

What “Swapping” means

6

2 1

3 5 4

6

Place the first element into the Temporary Variable.

TEMP

(39)

1

2 1

3 5

Replace the first element with the value

4

of the smallest element.

6

TEMP

(40)

1

2

6 3 5 4

6

TEMP

Replace the third element (in this

example) with the Temporary Variable.

(41)

Textbooks & Web References

• Reference book iii (Chapter 10)

• www.visualgo.net

(42)

Thank you

&

Any question?

Referensi

Dokumen terkait

Selection sort merupakan salah satu teknik pengurutan yang membandingkan elemen yang sekarang dengan elemen yang berikutnya samapai dengan elemen yang

At this stage the process is carried out using the PHP program where the sorting of the data is sorted using the PHP programming language which provides a sort function to sort

Dalam pengurutan data-data yang terindex untuk menghitung sebuah prestasi siswa menggunakan algoritma bubble sort ini, di urutkan secara descending, agar dapat mengetahui bentuk

DASAR TEORI Algoritma Bubble Sort Metode gelembung bubble sort sering juga disebut dengan metode penukaran exchange sort adalah metode yang mengurutkan data dengan cara membandingkan

Bubble Sort • Disebut juga exchange sort : metode yang mengurutkan data dengan cara membandingkan masing2 elemen, kemudian melakukan penukaran bila perlu... Definisi • Metode ini

ALGORITHM OF BUBBLE SORT Bubble sort algorithm starts by comparison of the first two elements of an array and swapping if required, i.e., if you want to sort the elements of an array

String Sort Secara Manual Mengurutkan “saya suka situ sebab situ suka saya” berdasarkan

PERCOBAAN : Selection Sort Secara ASCENDING Codingannya: //MITHA ZALINA RAMADHANY //6304221424 package Sorting; public class SelectionSortMithaASC { public static... PERCOBAAN :