Download Presentation
## Sorting

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Sorting**Chapter 9**The efficiency of data handling can often be increased if**the data are sorted according to some criteria of order. • The first step is to choose the criteria that will be used to order data. • The final ordering of data can be obtained in a variety of ways\methods, and only some of them can be considered meaningful and efficient. • Certain critical properties of sorting algorithms should be defined when comparing alternative methods. • Which are the number of comparisons and the number of data movements .**9.1 Elementary Sorting Algorithms**• Insertion Sort • Start with considering the two first elements of the array ( ex. data[] array). If they are out of order, an interchange takes place. Then the third element is considered and inserted into its proper place and so on…**To find the number of movements and comparisons performed by**insertion sort, observe that: • The outer for loop always perform n-1 iterations. • The number of elements greater than data[i] to be moved by one position is not always the same. • The best case is when data are already in order - O(n). • The worst case is when data are in reverse order - O(n2 )**An insertion sort advantage is :**• It sorts the array only when it is really necessary (ex. The array is already in order). • Disadvantages : • Overlook the fact that elements may already be in their proper positions . • If an item is being inserted, all elements greater than that item have to be moved.**Selection Sort**• Finding a misplaced element first and putting it in its final place. • The element with the lowest value is selected and exchanged with the element in the first position. • The smallest value among the remaining elements is found and replaced with the second position. And so on..**Bubble Sort**• The array is scanned from the bottom up, and two adjacent elements are interchanged if they are out of order. • First, items data[ n-1 ] and data[ n-2 ] are compared and swapped if they are out of order. • Next, data[ n-2 ] and data[ n-3 ] are compared and swapped if they are out of order and so on up to data[1] and data[0]. • So, the smallest element is bubbled up to the top of the array. • The array now is scanned again comparing consecutive items and interchanging them when needed. • Compare up to data[2] and data[1]. So, the next smallest element is in position 1. • And so on until the last pass when only one comparison, data[n-1] with data[n-2], that may need to be interchanged.**The main disadvantage is that it looks at two adjacent array**elements at a time and swaps them if they are not in order. • If an element has to be moved from the bottom to the top, it is exchanged with every element in the array. • Also, this algorithm concentrates only on the item that is being bubbled up.**9.3 Efficient Sorting Algorithm**• Quick Sort • Quick sort selects one of the entries in the sequence to be the pivot and divides the sequence into two subsequences - one with all elements less than or equal to pivot are placed before it and one with all elements greater than pivot are placed after it. • It is one of the most common sorting algorithms for sequential computers because of its simplicity, low overhead, and optimal average complexity.**The process of sorting is recursively applied to each of the**sub lists. • Quicksort operates in O(N*logN) time. • Quicksort consists of two phases: • Sort phase. • Partition phase.**Algorithm steps :**• Choose a pivot value. Take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array. • Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice that array may be divided in to non-equal parts. • Sort both parts. Apply quicksort algorithm recursively to the left and the right parts of the array.**Merge Sort**• This is a much more efficient sorting technique than the bubble Sort and the insertion Sort at least in terms of speed. • Although the bubble and insertion sorts take O(N2) time, the merge sort is O(N*logN). • Merging two sorted arrays • Merging two sorted arrays A and B (they don’t need to be the same size)creates a third array, C, that contains all the elements of A and B, also arranged in sorted order.**Sorting by Merging**• The idea in the Merge sort is to divide an array in half, sort each half, and then use the merge() function to merge the two halves into a single sorted array. • You divide the array again and again until you reach a sub array with only one element. This is the base case; it’s assumed an array with one element is already sorted. • Algorithm steps : • Divides the array near its midpoint. • Sorts the two half-arrays by recursive calls. • Merges the two sorted half-arrays to obtain a fully sorted array.