Anda di halaman 1dari 58

Sorting III

(Pengurutan)
Selection Sort
Selection sort
• Kombinasi sorting dan searching.

• Untuk setiap proses, dilakukan dengan mencari


elemen dari posisi yang belum diurutkan dan
kemudian memilih elemen yang memiliki nilai terkecil
atau terbesar yang akan ditukarkan ke posisi yang
tepat di dalam array.
Selection sort
• Misalnya untuk putaran pertama, akan dicari data
dengan nilai terkecil dan data ini akan ditempatkan
pada indeks terkecil, pada putaran kedua akan dicari
data kedua terkecil, dan akan ditempatkan di indeks
kedua, begitu seterusnya hingga tidak ada data yang
dicari lagi.

• Selama proses, pembandingan dan pengubahan


hanya dilakukan pada indeks pembanding saja,
pertukaran data secara fisik terjadi pada akhir
proses.
Selection sort
Selection sort
Before using selection sort

5 1 3 7 11 9
0 1 2 3 4 5
Selection sort
n=6

5 1 3 7 11 9
0 1 2 3 4 5
Selection sort
n = 6, i = n-1, i > 0

5 1 3 7 11 9
0 1 2 3 4 5
Selection sort
i=5 j = 1; j <= i

5 1 3 7 11 9
0 1 2 3 4 5

Largest = 5
Index = 0
Selection sort
i=5 j = 1; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

1 > 5
Largest = 5
Index = 0
Selection sort
i=5 j = 1; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

1 > 5 No
Largest = 5
Index = 0
Selection sort
i=5 j = 2; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

3 > 5
Largest = 5
Index = 0
Selection sort
i=5 j = 2; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

3 > 5 No
Largest = 5
Index = 0
Selection sort
i=5 j = 3; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

7 > 5
Largest = 5
Index = 0
Selection sort
i=5 j = 3; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

7 > 5 Yes
Largest = 5
Index = 0
Selection sort
i=5 j = 3; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

> 7
Largest = 7
Index = 3
Selection sort
i=5 j = 4; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

11 > 7
Largest = 7
Index = 3
Selection sort
i=5 j = 4; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

11 > 7 Yes
Largest = 7
Index = 3
Selection sort
i=5 j = 4; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

> 11
Largest = 11
Index = 4
Selection sort
i=5 j = 5; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

9 > 11
Largest = 11
Index = 4
Selection sort
i=5 j = 5; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

9 > 11 No
Largest = 11
Index = 4
Selection sort
i=5 j = 5; j <= 5

5 1 3 7 11 9
0 1 2 3 4 5

> 11
Largest = 11
Index = 4
Selection sort
i=5

5 1 3 7 11 9
0 1 2 3 4 5

Largest = 11 x[index]= x[i]


Index = 4 x[i] = Largest
Selection sort
i=5

5 1 3 7 11 9
0 1 2 3 4 5

Largest = 11 x[4] = 9
Index = 4 x[5] = 11
Selection sort
i=5

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 11 x[4] = 9
Index = 4 x[5] = 11
Selection sort
i=5

5 1 3 7 9 11
0 1 2 3 4 5
Selection sort
i=4 j = 1; j <= i

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 5
Index = 0
Selection sort
i=4 j = 1; j <= 4

5 1 3 7 9 11
0 1 2 3 4 5

1 > 5 No
Largest = 5
Index = 0
Selection sort
i=4 j = 2; j <= 4

5 1 3 7 9 11
0 1 2 3 4 5

3 > 5 No
Largest = 5
Index = 0
Selection sort
i=4 j = 3; j <= 4

5 1 3 7 9 11
0 1 2 3 4 5

7 > 5 Yes
Largest = 7
Index = 3
Selection sort
i=4 j = 3; j <= 4

5 1 3 7 9 11
0 1 2 3 4 5

> 7 Yes
Largest = 7
Index = 3
Selection sort
i=4 j = 4; j <= 4

5 1 3 7 9 11
0 1 2 3 4 5

9 > 7 Yes
Largest = 7
Index = 3
Selection sort
i=4 j = 4; j <= 4

5 1 3 7 9 11
0 1 2 3 4 5

9 > 7 Yes
Largest = 9
Index = 4
Selection sort
i=4 j = 4; j <= 4

5 1 3 7 9 11
0 1 2 3 4 5

> 9 Yes
Largest = 9
Index = 4
Selection sort
i=4

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 9 x[index]= x[i]


Index = 4 x[i] = Largest
Selection sort
i=4

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 9 x[4] = 9
Index = 4 x[4] = 9
Selection sort
i=4

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 9 x[4] = 9
Index = 4 x[4] = 9
Selection sort
i=4

5 1 3 7 9 11
0 1 2 3 4 5
Selection sort
i=3 j = 1; j <= i

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 5
Index = 0
Selection sort
i=3 j = 1; j <= 3

5 1 3 7 9 11
0 1 2 3 4 5

1 > 5 No
Largest = 5
Index = 0
Selection sort
i=3 j = 2; j <= 3

5 1 3 7 9 11
0 1 2 3 4 5

3 > 5 No
Largest = 5
Index = 0
Selection sort
i=3 j = 3; j <= 3

5 1 3 7 9 11
0 1 2 3 4 5

7 > 5 Yes
Largest = 7
Index = 3
Selection sort
i=3 j = 3; j <= 3

5 1 3 7 9 11
0 1 2 3 4 5

> 7 Yes
Largest = 7
Index = 3
Selection sort
i=3

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 7 x[index]= x[i]


Index = 3 x[i] = Largest
Selection sort
i=3

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 7 x[3] = 7
Index = 3 x[3] = 7
Selection sort
i=2

5 1 3 7 9 11
0 1 2 3 4 5
Selection sort
i=2 j = 1; j <= 2

5 1 3 7 9 11
0 1 2 3 4 5

1 > 5 No
Largest = 5
Index = 0
Selection sort
i=2 j = 2; j <= 2

5 1 3 7 9 11
0 1 2 3 4 5

3 > 5 No
Largest = 5
Index = 0
Selection sort
i=2

5 1 3 7 9 11
0 1 2 3 4 5

Largest = 5 x[index]= x[i]


Index = 0 x[i] = Largest
Selection sort
i=2

3 1 5 7 9 11
0 1 2 3 4 5

Largest = 5 x[0] = 3
Index = 0 x[2] = 5
Selection sort
i=1

3 1 5 7 9 11
0 1 2 3 4 5
Selection sort
i=1 j = 1; j <= 1

3 1 5 7 9 11
0 1 2 3 4 5

1 > 3 No
Largest = 3
Index = 0
Selection sort
i=1 j = 1; j <= 1

3 1 5 7 9 11
0 1 2 3 4 5

> 3
Largest = 3
Index = 0
Selection sort
i=1

3 1 5 7 9 11
0 1 2 3 4 5

Largest = 3 x[index]= x[i]


Index = 0 x[i] = Largest
Selection sort
i=1

1 3 5 7 9 11
0 1 2 3 4 5

Largest = 3 x[0] = 1
Index = 0 x[1] = 3
Selection sort
i=1

1 3 5 7 9 11
0 1 2 3 4 5
Selection sort
i = 0 no need to find it

1 3 5 7 9 11
0 1 2 3 4 5
Selection sort
After using selection sort

1 3 5 7 9 11
0 1 2 3 4 5
Selection sort
void selection ( int X [ ], int n )
{
int i, j, index, large;
for ( i = n-1; i > 0; i--)
{
large = X[0];
index = 0;
for ( j = 1; j <= i; j++)
{
if ( X[j] > large )
{
large = X[j];
index = j;
}
}
X[index] = X[i];
X[i] = large;
}
}

Anda mungkin juga menyukai