Sie sind auf Seite 1von 26

BASIC SORTING

TECHNIQUES
PREPARED BY:

JYR MARIE V. REYES


SORTING
 When the elements are in random order, we
need to rearrange them before we can take
advantage of any ordering. This process is
called sorting.
 Suppose we have an array a of five integers
that we wish to sort from smallest to largest.
 In Figure 1, the values currently in a are as
depicted on the left; we wish to end up with
values as on the right.
Prepared by: Jyr Marie V. Reyes 2
Figure 1: Sample of items before and after sorting
Prepared by: Jyr Marie V. Reyes 3
Prepared by: Jyr Marie V. Reyes 4
Sample Output:

Prepared by: Jyr Marie V. Reyes 5


SELECTION SORT
Selection sort is a sorting algorithm,
specifically an IN-PLACE COMPARISON
SORT.
SORT It has Θ(n2) complexity, making it
inefficient on large lists, and generally
performs worse than the similar insertion
sort. Selection sort is noted for its
simplicity, and also has performance
advantages over more complicated
algorithms in certain situations.
Prepared by: Jyr Marie V. Reyes 6
SELECTION SORT
The basic idea of a selection sort is:

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 the smallest value with the
value at position i.
Prepared by: Jyr Marie V. Reyes 7
SELECTION SORT
 Table 1 shows a trace of the elements of an array
after each exchange of elements is made.
 The items just swapped are marked with asterisks,
and the sorted portion is shaded.
 Notice that in the second and fourth passes, since
the current smallest numbers are already in place,
we need not exchange anything.
 After the last exchange, the number at the end of the
array is automatically in its proper place.
Prepared by: Jyr Marie V. Reyes 8
SELECTION SORT

Table 1: Sorting items using Selection Sort


Prepared by: Jyr Marie V. Reyes 9
SELECTION SORT
 Before writing the algorithm for this sorting
method, note the following:
 If the array is of length n, we need n - 1
steps.
 We must be able to find the smallest number.
 We need to exchange appropriate array
items.
 When the code is written for this sort, note that
strict inequality (<) rather than weak inequality
(<=) is used when looking for the smallest
remaining value.
Prepared by: Jyr Marie V. Reyes 10
SELECTION SORT
 The algorithm for selection sort is:

For each i from 0 to n - 1 do


Find the smallest value among a[i],
a[i+1], ..a[n - 1]and store the
index of the smallest value in
minIndex
Exchange the values of a[i] and
a[index], if necessary
Prepared by: Jyr Marie V. Reyes 11
SELECTION SORT

Prepared by: Jyr Marie V. Reyes 12


Prepared by: Jyr Marie V. Reyes 13
Sample Output:

Prepared by: Jyr Marie V. Reyes 14


BUBBLE SORT
It works by repeatedly stepping through the list to
be sorted, comparing two items at a time and
swapping them if they are in the wrong order.
The pass through the list is repeated until no
swaps are needed, which indicates that the list is
sorted. The algorithm gets its name from the way
smaller elements "bubble" to the top of the list.
Because it only uses comparisons to operate on
elements, it is a COMPARISON SORT.
SORT
Prepared by: Jyr Marie V. Reyes 15
BUBBLE SORT
The Bubble Sort compares adjacent elements
in a list, and “swaps” them if they are not in
order. Each pair of adjacent elements is
compared and swapped until the largest
element “bubbles” to the bottom. Repeat this
process each time stopping one indexed
element less, until you compare only the first
two elements in the list. We know that the
array is sorted after both of the nested loops
have finished.
Prepared by: Jyr Marie V. Reyes 16
BUBBLE SORT

Bubble sort has worst-case complexity


О(n²), where n is the number of items being
sorted. There exist many other sorting
algorithms with substantially better worst-
case complexity O(n log n), meaning that
bubble sort should not be used when n is
large.

Prepared by: Jyr Marie V. Reyes 17


BUBBLE SORT
 Given a list of items stored in an array, a
bubble sort causes a pass through the array
to compare adjacent pairs of items.
 Whenever two items are out of order with
respect to each other, they are swapped.
 The effect of such a pass through an array
of items is traced in Table 2.
 The items just swapped are marked with
asterisks, and the sorted portion is shaded.
Prepared by: Jyr Marie V. Reyes 18
BUBBLE SORT

 Notice that after such a pass, we are


assured that the array will have the item
that comes last in order in the final array
position.
 That is, the last item will "sink" to the
bottom of the array, and preceding items
will gradually "percolate" or "bubble" to the
top.
Prepared by: Jyr Marie V. Reyes 19
BUBBLE SORT

Table 2: Sorting items using Selection Sort


Prepared by: Jyr Marie V. Reyes 20
Prepared by: Jyr Marie V. Reyes 21
INSERTION SORT
Insertion sort is a simple sorting algorithm, a comparison sort in
which the sorted array (or list) is built one entry at a time. It is much
less efficient on large lists than more advanced algorithms such as
quick sort, heap sort, or merge sort, but it has various advantages:
 Simple to implement
 Efficient on (quite) small data sets
 Efficient on data sets which are already substantially sorted: it runs
in O(n + d) time, where d is the number of inversions
 More efficient in practice than most other simple O(n2) algorithms
such as selection sort or bubble sort: the average time is n2/4 and it
is linear in the best case
 Stable (does not change the relative order of elements with equal
keys)
 In-place (only requires a constant amount O(1) of extra memory
space)
 It is an online algorithm, in that it can sort a list as it receives it.

Prepared by: Jyr Marie V. Reyes 22


INSERTION SORT
 The insertion sort attempts to take greater
advantage of an array's partial ordering.
 The goal is that on the kth pass through, the kth
item among:
a[0], a[1], ..., a[k]
 Should be inserted into its rightful place among
the first k items in the array.
 This is analogous to the fashion in which many
people pick up playing cards and order them in
their hands. Prepared by: Jyr Marie V. Reyes 23
For each k from 1 to n - 1 (k is the index of array
element to insert)

Set itemToInsert to a[k]


Set j to k - 1
( j starts at k - 1 and is decremented until
insertion position is found)

While (insertion position not found) and (not


beginning of array)

If itemToInsert < a[j]


Move a[j] to index position j + 1
Decrement j by 1

Else
The insertion position has been found
itemToInsert should be positioned at index j + 1
Prepared by: Jyr Marie V. Reyes 24
INSERTION SORT
 An insertion sort for each value of k is traced in Table 3.
 In each column of this table, the data items are sorted in
order relative to each other above the item with the
asterisk; below this item, the data are not affected.

Prepared by: Jyr Marie V. Reyes 25


Prepared by: Jyr Marie V. Reyes 26

Das könnte Ihnen auch gefallen