Sie sind auf Seite 1von 2

Like bubble sort, selection sort is implemented with one loop nested inside another.

This suggests
that the efficiency of selection sort, like bubble sort, is n 2 . To understand why this is indeed correct,
consider how many comparisons must take place. The first iteration through the data require n 1 comparisons to find the minimum value to swap into the first position. Because the first position
can then be ignored when finding the next smallest value, the second iteration requires n 2 comparisons and third requires n - 3 . This progression continues as follows:
(n - 1) + (n - 2) + ... +2 + 1 = n(n - 1)/2 = O(n 2)
Unlike other quadratic tests, the efficiency of selection sort is independent of the data. Bubble sort,
for example, can sort sorted and some nearly-sorted lists in linear time because it is able to identify
when it has a sorted list. Selection sort does not do anything like that because it is only seeking the
minimum value on each iteration. Therefore, it cannot recognize (on the first iteration) the difference
between the following two sets of data: 1 2 3 4 5 6 7 8 9 and 1 9 8 7 6 5 4 3 2. In each case, it will
identify the 1 as the smallest element and then go on to sorting the rest of the list. Because it treats
all data sets the same and has no ability to short-circuit the rest of the sort if it ever comes across a
sorted list before the algorithm is complete, insertion sort has no best or worst cases. Selection sort
always takes O(n 2) operations, regardless of the characteristics of the data being sorted.

RADIX SORT
The Radix Sort Algorithm===http://www.geeksforgeeks.org/radix-sort/
1) Do following for each digit i where i varies from least significant digit to the most significant digit.
.a) Sort input array using counting sort (or any stable sort) according to the ith digit.
Example:
Original, unsorted list:
170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802
occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in
the previous list.]
802, 2, 24, 45, 66, 170, 75, 90

Sorting by most significant digit (100s place) gives:


2, 24, 45, 66, 75, 90, 170, 802

Heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an


improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region,
and it iteratively shrinks the unsorted region by extracting the smallest element and moving that to
the sorted region. The improvement consists of the use of a heap data structure rather than a lineartime search to find the minimum.[2]
Although somewhat slower in practice on most machines than a well-implemented quicksort, it has
the advantage of a more favorable worst-case O(n log n) runtime. Heapsort is an in-place algorithm,
but it is not a stable sort.
Heapsort was invented by J. W. J. Williams in 1964.[3] This was also the birth of the heap, presented
already by Williams as a useful data structure in its own right.[4] In the same year, R. W.
Floyd published an improved version that could sort an array in-place, continuing his earlier research
into the treesort algorithm.[4]

Counting sort is likely one of the simplest sorting algorithms that can be used to sort a list of
integers and is also used as a key component of Radix Sort. Both were invented/discovered
by Harold Seward. In this article I will both explain and code, Counting Sort in C.

Counting Sort===http://austingwalters.com/counting-sort-in-c/
Counting Sort is very basic to implment, the sole purpose of the algorithm is to sort integers
of a given list and will outperform general purpose sorting algorithms. For example, given the
array {1, 3, 5, 2, 4, 1}, applying the Counting Sort algorithm to the array should give you: {1,
1, 2, 3, 4, 5}. The algorithm works by using the following steps:
1. Initializing a counting array to all zeros, the size of the array being equal to the maximum
integer in the list.
2. Iterate through the input array (the array you wish to sort) adding to the count of the
corresponding counting array index. That is to say, if you were iterating over the integer 5 in the
input array, you would add to the count of index 5 in the counting array.
3. Transfer the count of each index in the counting array back to the input array (overriding
previous values), there by making the input array the output array (and saving space).
O(n + k):worst running time compexity

Das könnte Ihnen auch gefallen