*This preview shows
pages
1–4. 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 1 Mergesort Quicksort The efficiency of handling data can be substantially improved if the data is 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. Retrieval of a data item would be very time consuming 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. It could be one of the following schemes: alphabetical order ascending/descending order order according to name, ID, year, department etc. The aim of sorting algorithms is to organize the available 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 we have been doing throughout the course, we are interested in finding out as to which algorithms are best suited for a particular situation. The efficiency of a sorting algorithm can be worked out by counting the number of comparisons and the number of data movements involved in each of the algorithms. 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 is 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. In this section we shall discuss two efficient sorting algorithms the merge sort and the quick sort procedures. Mergesort The mergesort-sorting algorithm uses the divide and conquer strategy of solving problems in which the original problem is split into two problems, with size about half the size of the original problem. The basic idea is as follows. Suppose you have got a large number of integers to sort. Write each integer on a separate slip of paper. Make two piles of the slips. So the original problem has been reduced to sorting individually two piles of smaller size. Now reduce each pile to half of the existing size. There would be now 4 piles with a smaller set of integers to sort. Keep on increasing the number of piles by reducing their lengths by half...

View
Full
Document