Wednesday, May 9, 2012

Sorting

It is rearranging a collection of items into increasing or decreasing order. Sorting is used to preprocess the collection to make searching faster, as well as to identify items that are similar.

A sort can be classified as
internal - if the records that it is sorting are within main memory.
external - if some of the records that it is sorting are in auxiliary storage.

There are large number of sorting techniques. Some of them  with there average time complexity are:
Bubble Sort       O(n2)
Selection Sort    O(n2)
Insertion Sort     O(n2)
Quick Sort         O(nlogn)
Merge Sort        O(nlogn)
Heap Sort          O(nlogn)

An ideal sort is an inplace sort whose additional space requirement is O(1).

A sorting technique is said to be stable if for all records i and j such that k[i] equals k[j], if r[i] precedes r[j] in original file, r[i] precedes r[j] in the sorted file. Here, k[i] refer to key associated with each record r[i].

which sort is better we can't tell it totally depends upon the problem characteristics where which will be more suitable.

No comments:

Post a Comment