Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms
Sorting
2
Sorting Sorting is the process of arranging a list of items into a particular order There must be some value on which the order is based There are many algorithms for sorting a list of items These algorithms vary in efficiency We will examine two specific algorithms: Selection Sort Insertion Sort
3
Selection Sort The approach of Selection Sort:
select one value and put it in its final place in the sort list repeat for all other values In more detail: find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are placed
4
Selection Sort An example: smallest is 1: 1 9 6 3 2
original: smallest is 1: smallest is 2: smallest is 3: smallest is 6: See SortGrades.java See Sorts.java -- the selectionSort method
5
The approach of Insertion Sort:
Pick any item and insert it into its proper place in a sorted sublist repeat until all items have been inserted In more detail: consider the first item to be a sorted sublist (of one item) insert the second item into the sorted sublist, shifting items as necessary to make room to insert the new addition insert the third item into the sorted sublist (of two items), shifting as necessary repeat until all values are inserted into their proper position
6
Insertion Sort An example: insert 9: 3 9 6 1 2 insert 6: 3 6 9 1 2
original: insert 9: insert 6: insert 1: insert 2: See Sorts.java -- the insertionSort method
7
Insertion Sort An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct sequence. Both average and worst-case time is O(n2).
8
Sorting First card is already sorted With all the rest,
Card players all know how to sort … First card is already sorted With all the rest, Scan back from the end until you find the first card larger than the new one, Move all the lower ones up one slot insert it A K 10 2 J 2 Q 2 9 9
9
Insertion Sort
10
INSERTION SORT Assuming there are n elements in the array, we must index through n - 1 entries. For each entry, we may need to examine and shift up to n - 1 other entries, resulting in a O(n2) algorithm. The insertion sort is an in-place sort. That is, we sort the array in-place. No extra memory is required. The insertion sort is also a stable sort. Stable sorts retain the original ordering of keys when identical keys are present in the input data.
11
Sorting Objects Integers have an inherent order, but the order of a set of objects must be defined by the person defining the class Recall that a Java interface can be used as a type name and guarantees that a particular class has implemented particular methods We can use the Comparable interface to develop a generic sort for a set of objects See SortPhoneList.java See Contact.java See Sorts.java
12
Comparing Sorts Both Selection and Insertion sorts are similar in efficiency The both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list Therefore approximately n2 number of comparisons are made to sort a list of size n We therefore say that these sorts are of order n2 Other sorts are more efficient: order n log2 n
13
Sorting - Insertion sort
Complexity For each card Scan O(n) Shift up O(n) Insert O(1) Total O(n) First card requires O(1), second O(2), … For n cards operations ç O(n2) S i i=1 n
14
Sorting - Insertion sort
Complexity For each card Scan O(n) O(log n) Shift up O(n) Insert O(1) Total O(n) First card requires O(1), second O(2), … For n cards operations ç O(n2) Use binary search! Unchanged! Because the shift up operation still requires O(n) time S i i=1 n
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.