{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

07-Sorts

# 07-Sorts - Sorting Sorting The binary search algorithm...

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

Sorting Th bi h l ith ti l The binary search algorithm assumes a particular data structure, namely, a sorted array of items. It turns out that sorting is a fundamental algorithmic operation that is important in a wide variety of practical problems problems. (Ch k t ti l ith ) (Check out www.sorting-algorithms.com

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

View Full Document
A Simple Sorting Algorithm The shuffle sort assumes a boolean function, sorted() , that determines whether an array is in sorted order If that determines whether an array is in sorted order. If it is not in sorted order, then a function shuffleArray() is called to randomly permute the contents of the array. shuffleSort (int array[ ], int n) { hil ( ! t d( ) ) { while ( !sorted(array, n) ) { shuffleArray(array, n); } } Is this algorithm correct?
A Simple Sorting Algorithm Is this algorithm correct? Yes, it will iterate until the array is in sorted order shuffleSort (int array[ ], int n) array is in sorted order. { while ( !sorted(array, n) ) { shuffleArray(array n); shuffleArray(array, n); } } Assuming that sorted() and shuffleArray() each takes O(n) time, what is the complexity of the above sorting algorithm?

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

View Full Document
A Simple Sorting Algorithm A i th t t d() d h ffl A () t k O( ) ti Assuming that sorted() and shuffleArray() take O(n) time, what is the complexity of the random sort algorithm? Well, technically, it might be argued that the random shuffling may never generate a sorted sequence and thus the algorithm may require infinite time. The fact is that big-Oh notation doesn't really apply to the analysis of this algorithm. However, we can talk about the t d l it f th l ith expected complexity of the algorithm. Because there are n! possible permutations of n items, and one of those permutations is the sorted order we want, we can say that shuffleSort has expected O(n!) complexity. We could make this a worst-case result by examining all possible make this a worst case result by examining all possible permutations in some order until we find the one we want.
Developing a Better Sorting Algorithm Shuffle sort represents a kind of brute-force algorithm that is clearly impractical. In fact, it would take billions of years for an array of size 30. A better approach is to examine a given order and determine how it deviates from sorted order. If it can be changed so that it is closer to being in sorted order, then we can repeat th til th i l t l t d the process until the array is completely sorted. What is needed is to establish some invariant properties that must be satisfied for an array to be sorted.

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

View Full Document
Invariants for Sorting A i i t diti f t d i th f ll i An invariant condition for sortedness is the following: An array a[] is in sorted order if and only if each element a[ i ] satisfies a[ i ] < a[ i+1 ] a[ i ] satisfies a[ i ] < a[ i+1 ] . This leads to the following algorithm for testing whether an i i di t d d array is in ascending sorted order: boolean isSorted(int a[], int n) { int i; for (i=0; i < n-1; i++) { if ( a[ i ] > a[ i+1 ] ) return FALSE; } return TRUE; } What is the complexity of this algorithm?
Invariants for Sorting A i i t diti f t d i th f ll i An invariant condition for sortedness is the following: An array a[]

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ 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