Data Structure Selection Sorting Theory

last = 4   //last tu kat column 4                                 
7 8 3 1 6
compare(8,7) - biggest_value_index = 1  //ni functionnya nka cari nombor apa yang paling besar
compare(3,8) - biggest_value_index = 1  //kat sini dia detect nombor paling besar ada kat column 1
compare(1,8) - biggest_value_index = 1
compare(6,8) - biggest_value_index = 1

swap(element at biggest_value_index, last)//bila dah compare semua...swap yg paling besar kat column 1 ngn column last
swap(8, 6)
7 8 3 1 6
  B     L
7 6 3 1 8

==============================================
last = 3                                  //last-1 bila L=B
7 6 3 1 8
compare(6, 7) - biggest_value_index = 0 
compare(3, 7) - biggest_value_index = 0
compare(1, 7) - biggest_value_index = 0

swap(element at biggest_value_index, last)
swap(7, 1)
7 6 3 1 8
B     L
1 6 3 7 8

==============================================

last = 2
1 6 3 7 8
compare(6, 1) - biggest_value_index = 1
compare(3, 6) - biggest_value_index = 1
swap(element at biggest_value_index, last)
swap(6, 3)
1 6 3 7 8
  B L
1 3 6 7 8

==============================================

last = 1
1 3 6 7 8
compare(3, 1) - biggest_value_index = 1
swap(element at biggest_value_index, last)
swap(3, 3)
1 3 6 7 8
  B
  L
1 3 6 7 8


==============================================

last = 0

DONE

==============================================

9 1 5 2 3
B         L
3 1 5 2 9
---
3 1 5 2 9
      B L
3 1 2 5 9
---
3 1 2 5 9
B   L
2 1 3 5 9
---
2 1 3 5 9
B L
1 2 3 5 9
---
1 2 3 5 9
L
---
DONE

==============================================

IMPROVEMENT

- ignore swapping if biggest_value_index == last

Post a Comment

0 Comments