Why Sorting Is Needed

This section describes why sorting is needed - Search in a sorted collection is much faster than an un-sorted collection.

Sorting makes it possible to search a particular data element in a collection quickly by applying the binary search technique.

Everyone knows what is the binary search technique. We are using it every time when we look up a word in a dictionary.

How much time we can save by using the binary search technique? Let's do a rough calculation. Assume that we have a sorted dictionary and an un-sorted dictionary with the same collection of words printed on about 1000 pages.

Looking up a word in the un-sorted dictionary, the best case is that you found the word on the first page; the worst case is that you looked through all 1000 the pages, and found the word on the last page. So roughly the average time you spend is checking 500 pages.

Looking up a word in the sorted dictionary, the best case is that you found the word on the center page, which is the first page you will be checking based on the binary search technique; the worst case is that you found the word on the final page after you have divided the dictionary 10 times. So roughly the average time you spend is checking 5 pages.

The answer is obvious. We are saving about 99% of our time by using the sorted dictionary!

Table of Contents

 About This Book

Introduction of Sorting Algorithms

 What Is Sorting?

Why Sorting Is Needed

 Most Popular Sorting Algorithms

 Java API for Sorting Algorithms

 Insertion Sort Algorithm and Java Implementation

 Selection Sort Algorithm and Java Implementation

 Bubble Sort Algorithm and Java Implementation

 Quicksort Algorithm and Java Implementation

 Merge Sort Algorithm and Java Implementation

 Heap Sort Algorithm and Java Implementation

 Shell Sort Algorithm and Java Implementation

 Sorting Algorithms Implementations in PHP

 Sorting Algorithms Implementations in Perl

 Sorting Algorithms Implementations in Python

 Performance Summary of All Implementations

 References

 Full Version in PDF/EPUB