{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture 29-34.docx - Sorting Algorithms It has long been...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sorting Algorithms It has long been known that sorted data can be searched more quickly than unsorted ed data. This has led to sorting arrays to be a very heavily researched topic area. But it should be noted that array sorting is not required for efficient data management! (Recall an entire unit about the Map and Set Abstract Data Types) But even so, arrays remain very popular for representing data. -- no extra memory required for storing pointers -- easy guaranteed O(1) access to any element The next four lectures will look at common strategies for sorting arrays. (They will assume increasing order, but the algorithms can easily do the reverse) Selection Sort One of the first intuitive strategies for sorting data: Decide which value should be first, and place it first Decide which value should be second, and place it second Decide which value should be third, and place it third This algorithm can easily be done in place, (does not require additional temporary array storage. 3 1 4 5 9 2 6 8 7 1 | 3 4 5 9 2 6 8 7 1 2 | 4 5 9 3 6 8 7 1 2 3 | 5 9 4 6 8 7 1 2 3 4 | 9 5 6 8 7 1 2 3 4 5 | 9 6 8 7 1 2 3 4 5 6 | 9 8 7 1 2 3 4 5 6 7 | 8 9 1 2 3 4 5 6 7 8 | 9 This is very simple, and has a very predictable behavior: O(n²) Unfortunately, it has that same predictable behavior when data is already sorted. (It still has to examine all the data to realize the first should be first). Bubble Sort A very popular sorting algorithm that is able to recognize sorted data. It matches the intuition most people have of recognizing sorted data: if two consecutive elements are out of order, the data is not sorted fix the observed ordering flaw, and look for others Typical implementations scan the data in one direction, swapping as necessary and then repeating with more scans until the data is known to be sorted. 3 1 4 5 9 2 6 8 7 <-> <-> <-> <-> <-> 1 3 4 5 2 6 8 7 9 <-> <-> 1 3 4 2 5 6 7 8 9 <-> 1 3 2 4 5 6 7 8 9 <-> 1 2 3 4 5 6 7 8 9 Each pass through the bubble sort is O(n) and the number of passes may also be O(n) yielding overall behavior of O(n²) If the data is already sorted, one pass is sufficient to detect that: O(n) Primary drawback: swapping values takes time! (three assignments) Some tweaks people have considered, which may be of debatable benefit:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
-- try to cut down swapping: e.g. pull out the 9; move 2,6,8,7; return 9 -- bi-directional bubble sort: traversing both ways alternately works well above -- 'remember' partially sorted data: after 3rd pass above, all after 4 is sorted More code to handle the special cases slows down the normal cases! As shown before: a very simple concept that is easy to make very inefficient! Insertion Sort The intuition resembles how many card players sort their cards. Each card is considered in turn, and one chooses where it belongs in the hand. This algorithm can also be done in place 3 | 1 4 5 9 2 6 8 7 1 3 | 4 5 9 2 6 8 7 1 3 4 | 5 9 2 6 8 7 1 3 4 5 | 9 2 6 8 7 1 3 4 5 9 | 2 6 8 7 1 2 3 4 5 9 | 6 8 7 1 2 3 4 5 6 9 | 8 7 1 2 3 4 5 6 8 9 | 7 1 2 3 4 5 6 7 8 9 O(n) passes, which could be O(n) time --> O(n²) Advantage: very fast when data is mostly sorted
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}