sorting – insertion and selection. sorting arranging data into ascending or descending order...

12
Sorting – Insertion and Selection

Upload: brittany-collins

Post on 04-Jan-2016

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

Sorting – Insertion and Selection

Page 2: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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

Page 3: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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.

Page 4: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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

Page 5: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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.

Page 6: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

Selection Sort

6274 4892 2843 9523 1892

Unsorted

1892 4892 2843 9523 6274

Sorted

1892 2843 4892 9523 6274

1892 2843 4892 9523 6274

1892 2843 4892 6274 9523

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

Page 7: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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);}

Page 8: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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(N1) comparisons– N(N1) swaps

If the given list is ordered in “random” order:– N(N1) comparisons– less than N(N1) swaps

If the given list is already sorted, we do:– N(N1) comparisons– If statement is skipped, therefore, no swaps

In all 3 cases, the running time is O(N2) due to N(N1) comparisons.

Page 9: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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.

Page 10: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

Sorting in Place – Insertion Sort

6274 4892 2843 9523 1892

Unsorted

6274 4892 2843 9523 1892

Sorted

4892 6274 2843 9523 1892

2843 4892 6274 9523 1892

2843 4892 6274 9523 1892

1892 2843 4892 6274 9523

The main idea:– Examine the elements one at a time– Insert each into its proper order among the elements already examined

Unsorted array

Page 11: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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);}

Page 12: Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use

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(N1)

comparisons – N(N1) swaps

If the given list is ordered in “random” order, we do:– N(N1) comparisons– less than N(N1) 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.