You are on page 1of 1

sorting

- Selection :
select the minimum(ascending) then move to sorted subset

- buble :
scan the array, compare 2 elements than swap(depends on Increasing or Decreasing).
A[] = {8,6,9,5,2,4}
step 1 -> {6,8,5,2,4,9}
step 2 -> {6,5,2,4,8,9}
step 3 -> {5,2,4,6,8,9}
step 4 -> {2,4,5,6,8,9}

- insertion :
take the 1st element of the array as the sorted subset, then look at the 2nd
element.
move the 2nd element to the temporary value, check the rest of the array.
anything that greater than the chosen element shift to the right(Increasing)
A[] = {8,6,9,5,2,4}
step 1 -> {6,8,9,5,2,4}
step 2 -> {6,8,9,5,2,4}
step 3 -> {5,6,8,9,2,4}
step 4 -> {2,5,6,8,9,4}
step 5 -> {2,4,5,6,8,9}

- merge(divide and conquer) :


divide the array into 2 parts, then divide again until they are 1 element.
Start merging the sub-array.
A[] = {8,6,9,5,2,4}
step 1 -> {8,6,9} {5,2,4}
step 2 -> {8,6} {9} ; {5,2} {4}
step 3 -> {8} {6}, {9} ; {5} {2}, {4}
step 4 -> {6,8} {9} ; {5} {2} , {4}
step 5 -> {6,8,9} ; {5} {2} , {4}
step 6 -> {6,8,9} ; {2,5} , {4}
step 7 -> {6,8,9} ; {2,4,5}
step 8 -> {2,4,5,6,8,9}

merging algorithm : compare the 1st element of the 2 sub-array, put the smallest in
the array.
then after putting in the array, the sub-array's index are increased.
{6,8,9} ; {2,4,5}
{6} {2} -> A[{2}]
{6,8,9} ; {4,5}
{6} {4} -> A[{2,4}]
{6,8,9} ; {5}
{6} {5} -> A[{2,4,5}]
{6,8,9} -> A[{2,4,5,6,8,9}]
**the sub-array must be sorted first

quick sort :
take any element as the pivot, then anyone smaller than pivot go to left(in any
order).
anyone greater than pivot go to right(in any order)

You might also like