Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.

Similar presentations


Presentation on theme: "Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is."— Presentation transcript:

1 Chapter 9 Sorting

2 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.

3 Elementary Sorting Algorithms 1. 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…

4 To find the number of movements and comparisons performed by insertion sort, observe that: 1. The outer for loop always perform n-1 iterations. 2. 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(n 2 )

5 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.

6 Example

7 2. 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..

8 Example

9 3. 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.

10 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.

11 Example

12 Efficient Sorting Algorithm 1. 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.

13 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.

14 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.

15 Example

16 2. 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(N 2 ) time, the merge sort is O(N*logN). 1. 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.

17

18 2. 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.

19

20 Searching

21 Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list. Look at every element : This is a very straightforward loop comparing every element in the array with the key. As soon as an equal value is found, it returns. If the loop finishes without finding a match, the search failed and -1 is returned.

22

23 int LinearSearch(const int *Array, const int Size, const int ValToSearch) { bool NotFound = true; int i = 0; while(i < Size && NotFound) { if(ValToSearch != Array[i]) i++; else NotFound = false; } if( NotFound == false ) return i; else return -1; }

24 Binary search algorithm Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. A fast way to search a sorted array is to use a binary search. The idea is to look at the element in the middle. If the key is equal to that, the search is finished. If the key is less than the middle element, do a binary search on the first half. If it's greater, do a binary search of the second half.

25 Algorithm Algorithm is quite simple. It can be done either recursively or iteratively: 1. Get the middle element; 2. If the middle element equals to the searched value, the algorithm stops; 3. Otherwise, two cases are possible: o searched value is less than the middle element. In this case, go to the step 1 for the part of the array before middle element. o searched value is greater than the middle element. In this case, go to the step 1 for the part of the array after middle element.

26 Example : we have the array X[12]: Search for b=12 mid = (0+11)/2 = 5. Compare b with X[5]: 12<20. So search in left half X[0..4] mid = (0+4)/2 = 2. Compare b with X[2]: 12 > 7. So search right half X[3..4] mid = (3+4)/2 = 3.Compare b with X[3]: b=X[3]=12. Return 3.


Download ppt "Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is."

Similar presentations


Ads by Google