lecture 29-34.docx

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

• Notes
• 14

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

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:

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

-- 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
This is the end of the preview. Sign up to access the rest of the document.
• Spring '08
• CHRISTMAN

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern