------------------
Quicksort:
Quicksort(Data: values[], Integer: start, Integer: end)
<Pick a dividing item from the array. Call it divider.>
<Move items < divider to the front of the array.
Move items >= divider to the end of the array.
Let middle be the index between the pieces where divider is put.>
// Recursively sort the two halves of the array.
Quicksort(values, start, middle - 1)
Quicksort(values, middle + 1, end)
End Quicksort
The top of the figure above show an array of values to sort. You can choose a number to become a divider. At here I chose 6 for divider
Bubblesort:
Bubblesort uses the fairly obvious fact that, if an array is not sorted, it must contain two adjacent elements that are out of order. The algorithm repeatedly passes through the array, swapping items that are out of order, until it can’t find any more swaps.
in the figure above, items that are farther down than they should be slowly "bubble up" to their correct-possitions.
Pseudocode:
Bubblesort(Data: values[])
// Repeat until the array is sorted.
Boolean: not_sorted = True
While (not_sorted)
// Assume we won't find a pair to swap.
not_sorted = False
// Search the array for adjacent items that are out of order.
For i = 0 To <length of values> - 1
// See if items i and i - 1 are out of order.
If (values[i] < values[i - 1]) Then
// Swap them.
Data: temp = values[i]
values[i] = values[i - 1]
values[i - 1] = temp
// The array isn't sorted after all.
not_sorted = True
End If
Next i
End While
End Bubblesort
0 comments:
Post a Comment