*This preview shows
pages
1–5. Sign up
to
view the full content.*

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
**Unformatted text preview: **Sorting Insertion sort Selection sort Bubble sort The efficiency of handling data can be substantially improved if the data are sorted according to some criteria of order. In a telephone directory we are able to locate a phone number, only because the names are alphabetically ordered. Same thing holds true for listing of directories created by us on the computer. It would be very annoying if we dont follow some order to store book indexes, payrolls, bank accounts, customer records, items inventory records, especially when the number of records is pretty large. We want to keep information in a sensible order. alphabetical order ascending/descending order order according to names, ids, years, departments etc. The aim of sorting algorithms is to put unordered information in an ordered form. There are dozens of sorting algorithms. The more popular ones are listed below: Selection Sort Bubble Sort Insertion Sort Merge Sort Quick Sort As always, we want to decide which algorithms are best for a particular situation. The efficiency is decided by the number of comparisons and the number of data movements, using the big-O notation. The order of magnitude can vary depending on the initial ordering of data. How much time, does a computer spend on data ordering if the data are already ordered? We often try to compute the data movements, and comparisons for the following three cases: best case ( often, data is already in order), worst case( sometimes, the data is in reverse order), and average case( data in random order). Some sorting methods perform the same operations regardless of the initial ordering of data. Why should we consider both comparisons and data movements? If simple keys are compared, such as integers or characters, then the comparisons are relatively fast and inexpensive. If strings or arrays of numbers are compared, then the cost of comparisons goes up substantially. If on the other hand, the data items moved are large, such as structures, then the movement measure may stand out as the determining factor in efficiency considerations. Insertion Sort The key idea is to pick up a data element and insert it into its proper place in the partial data considered so far. An outline of the algorithm is as follows: Let the n data elements be stored in a array list[ ]. Then, Let the n data elements be stored in a array list[ ]....

View
Full
Document