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

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

View Full Document Right Arrow Icon
orting Sorting 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 roblems problems. (Check out www.sorting-algorithms.com )
Background image of page 1

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

View Full DocumentRight Arrow Icon
A Simple Sorting Algorithm The shuffle sort assumes a boolean function, sorted() , at 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) { while ( !sorted(array, n) ) { shuffleArray(array, n); } } Is this algorithm correct?
Background image of page 2
A Simple Sorting Algorithm Is this algorithm correct? Yes, it will iterate until the rray is in sorted order shuffleSort (int array[ ], int n) array is in sorted order. (y [ ] , ) { while ( !sorted(array, n) ) { huffleArray(array n); shuffleArray(array, n); } } Assuming that sorted() and shuffleArray() each takes O(n) time, what is the complexity of the above sorting algorithm?
Background image of page 3

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

View Full DocumentRight Arrow Icon
A Simple Sorting Algorithm 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 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 ake this a worst- ase 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.
Background 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. better approach is to examine a given order and determine 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 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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Invariants for Sorting An invariant condition for sortedness is the following: An array a[] is in sorted order if and only if each element [ i ] atisfies [ i ] [ i+1 ] a[ i ] satisfies a[ i ] < a[ i+1 ] . This leads to the following algorithm for testing whether an 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?
Background image of page 6
Invariants for Sorting An invariant condition for sortedness is the following: An array a[] is in sorted order if and only if each element [ i ] atisfies [ i ] [ i+1 ] a[ i ] satisfies a[ i ] < a[ i+1 ] .
Background image of page 7

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

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

This note was uploaded on 11/06/2010 for the course CS 2050 taught by Professor Uhlmann during the Fall '09 term at Missouri (Mizzou).

Page1 / 122

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online