Você está na página 1de 31

Sorting

Data structures and algorithms in Java Anastas Misev


Parts used by kind permission:
Bruno Preiss, Data Structures and Algorithms with Object-Oriented Design Patterns in Java David Watt and Deryck F. Brown, Java Collections, An Introduction to Abstract Data Types, Data Structures and Algorithms Klaus Bothe, Humboldt University Berlin, course Praktische Informatik 1

Sorting
Sorters Sorting algorithms Insertion sorting Exchange sorting Selection sorting Merge sorting Distribution sorting

Sorters
a sorter as an abstract machine (an object) its purpose is to sort arrays of data Sorter is an interface method sort takes an array of Comparable objects and sorts the contents of the array AbstractSorter is the base class from which various sorters are derived

Interface and abstract class

Class hierarchy

Insertion sorting

Binary insertion sort

Insertion sorting analysis

Exchange sorting
algorithms that sort by exchanging pairs of items until the sequence is sorted may exchange adjacent elements as well as widely separated ones insertion sorting can be considered as a special case of exchange sorting

Bubble sort

Quick sort
1. 2.

3.

4.

To sort the sequence S = {s1,s2, s3,, sn} Select one of the elements of S. The selected element, p, is called the pivot. Remove p from S and then partition the remaining elements of S into two distinct sequences, L and G, such that every element in L is less than or equal to the pivot and every element in G is greater than or equal to the pivot. Rearrange the elements of the sequence as follows: S = {L, p, G} Recursively quicksort the unsorted sequences L and G.

Quick sort

Quick sort

Exchange sorting analysis

Selection sorting

Heap sorting

Heap sorting

Heap sorting

Merge sorting

Merge sorting

Merge sorting

Lower bound on sorting


consider a sequence of n items to be sorted consider algorithms that sort using only binary comparisons every binary decision tree that sorts the sequence must have at least n! leaves therefore the height of the binary decision tree is at least log 2 n!

Decision tree

Therefore, the running time of any sorting algorithm that sorts using only binary comparisons is (n log n)

Distribution sorting
distribution sorting does not compare the elements to be sorted requires that we know something about the basis set from which the elements to be sorted are drawn e.g., if the basis set is small, we can use bucket sort if the elements can be represented with a small, finite number of bits (or digits, letters or symbols) we can use radix sort

Bucket sorting

Radix sorting
used when each element of the basis set can be viewed as a sequence of symbols make multiple passes through the data in the first pass sort by the least significant symbol in the last pass sort by the most significant symbol use a bucket sort in each pass

Radix sorting

Actual times O(n2)

Actual times O(nlogn)

Actual times O(n)

References
Preiss, ch.14

Você também pode gostar