{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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?
Image of page 2
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?
Image of page 3

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

View Full Document Right Arrow Icon
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.
Image of page 4
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.
Image of page 5

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

View Full Document Right Arrow Icon
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?
Image of page 6
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[]
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern