120 likes | 253 Views
Sorting – Insertion and Selection. Sorting. Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use the data, e.g. searching Many algorithms – many implementations Bubble sort Selection sort Insertion sort Heap sort. Sorting.
E N D
Sorting • Arranging data into ascending or descending order • Influences the speed and complexity of algorithms that use the data, e.g. searching • Many algorithms – many implementations • Bubble sort • Selection sort • Insertion sort • Heap sort
Sorting Arranging data into ascending or descending order influences the speed and complexity of algorithms that use the data. For instance, when working with data, it was found that sorted data was faster to retrieve. A binary search on sorted data improved the efficiency of retrieving data to O(logn). There's a plethora of solutions to sorting data. Some sorting algorithms are simple and intuitive, such as the bubble sort. Others, such as the quick sort are extremely complicated, but produce lightening-fast results. The question then becomes, which sorting algorithm to use, in which instance.
Two Important Measuresof Sorting Efficiency 1. Running time • The most important measure of efficiency • Compare number of comparisons to number of elements (n) • Less important measure: number of swaps 2. Space • Amount of extra memory used is also important • Sorting in place • Instead of transferring elements out of a sequence and then back into it, we just re-arrange them • Uses a constant amount of memory • More efficient space usage
Selection Sort Just like the Bubble sort, the Selection sort is also another sorting algorithm on the order of O(n² ). The selection sort works by selecting the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled. This is a simple and easy algorithm to implement. However, it is inefficient for large lists.
Selection Sort The main idea: • We find the smallest element and put it in the first position. • Then we find the next smallest element and put it in the second position. • Continue this way until the entire collection is sorted. Unsorted array Sorted Unsorted
Selection Sort #include <algorithm> void selection( int A[], int size ){ for( int i=0; i<size-1; ++i ){ int minIndex = i; for( int j=i+1; j<size; ++j ) if( A[j] < A[minIndex] ) minIndex = j; std::swap( A[i], A[minIndex] ); } } int main(){ const int SIZE = 5; int A[] = {6274,4892,2843,9523,1892}; selection(A,SIZE); }
Analysis: Best Case and Worst Case • If the given list is already ordered in reverse order (worst case) : • It would have to change the value minIndex every time it goes through the inner loop - N(N-1) comparisons • N(N-1) swaps • If the given list is ordered in “random” order: • N(N-1) comparisons • less than N(N-1) swaps • If the given list is already sorted, we do: • N(N-1) comparisons • If statement is skipped, therefore, no swaps In all 3 cases, the running time is O(N2) due to N(N-1) comparisons.
Insertion Sort Just like the Bubble and Selection sort, the Insertion sort is another sorting algorithm on the order of O(n² ). The insertion sort works just like its name suggests - it inserts each item into its proper place in the final list. The simplest implementation of this requires two lists - the source list and the list into which items are inserted in sorted order. However, in order to save memory, most implementations use an in-place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place. Although the insertion sort has the same complexity as the bubble and selection sort, the insertion sort is over twice as fast as the bubble sort and almost 40% faster than the selection sort and is relatively easy to implement. However, it is not too efficient for large lists.
Sorting in Place – Insertion Sort The main idea: • Examine the elements one at a time • Insert each into its proper order among the elements already examined Unsorted array Sorted Unsorted
Insertion Sort #include <algorithm> void insertion( int A[], int size ){ for( int i=1; i<size; ++i ) for( int j=i; j>0, A[j-1]>A[j]; --j ){ std::swap( A[j], A[j-1] ); } } int main(){ const int SIZE = 5; int A[] = {6274,4892,2843,9523,1892}; insertion(A,SIZE); }
Analysis: Best Case and Worst Case • If the given list is ordered in reverse order, we do: • Both loops will have to be done every time. N(N-1) comparisons • N(N-1) swaps • If the given list is ordered in “random” order, we do: • N(N-1) comparisons • less than N(N-1) swaps • If the given list is already sorted, we do: • The inner loop can be skipped. N comparisons • no swaps In this case, the running time is O(N) if the list is already sorted and O(N2) if it is not.