15
1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Herbert G. Mayer, PSU Status 5/23/2015 Status 5/23/2015

1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

Embed Size (px)

DESCRIPTION

3 Sorting Constraints Sorting rearranges elements of a data structure in a defined order; but does not change the total content Sorting rearranges elements of a data structure in a defined order; but does not change the total content One order of the arrangement is descending One order of the arrangement is descending Another order is ascending Another order is ascending Without loss of generality we focus on ascending order only; the other order is purely complementary Without loss of generality we focus on ascending order only; the other order is purely complementary Also, data structures can store information repeatedly, or just once per unique element Also, data structures can store information repeatedly, or just once per unique element If repeated, the duplicates may be stored sequentially; or else a count at one instance indicates the total number of occurrences If repeated, the duplicates may be stored sequentially; or else a count at one instance indicates the total number of occurrences Without loss of generality we focus on unique occurrence only Without loss of generality we focus on unique occurrence only

Citation preview

Page 1: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

1

CS 163Data Structures

Chapter 5Sorting

Herbert G. Mayer, PSUHerbert G. Mayer, PSUStatus 5/23/2015Status 5/23/2015

Page 2: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

2

Syllabus Sorting Constraints Complexity Bubble Sort Insertion Sort

Page 3: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

3

Sorting Constraints Sorting rearranges elements of a data structure in a Sorting rearranges elements of a data structure in a

defined order; but does not change the total contentdefined order; but does not change the total content One order of the arrangement is One order of the arrangement is descendingdescending Another order is Another order is ascendingascending Without loss of generality we focus on Without loss of generality we focus on ascendingascending

order only; the other order is purely complementaryorder only; the other order is purely complementary Also, data structures can store information Also, data structures can store information

repeatedlyrepeatedly, or just , or just once once per unique elementper unique element If repeated, the duplicates may be stored sequentially; If repeated, the duplicates may be stored sequentially;

or else a or else a count count at one instance indicates the total at one instance indicates the total number of occurrencesnumber of occurrences

Without loss of generality we focus on Without loss of generality we focus on uniqueunique occurrence onlyoccurrence only

Page 4: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

4

Complexity The cost of a The cost of a searchsearch for 1 elements in a data structure of for 1 elements in a data structure of nn

unordered elements is unordered elements is nn, or , or O(n)O(n) in in Big-O Big-O notation, withnotation, with n n being the distinct number of elementsbeing the distinct number of elements

Cost of a Cost of a sort sort is generally higher than is generally higher than nn, as each element’s , as each element’s position is considered versus all position is considered versus all nn, hence cost can be , hence cost can be O(nO(n22))

Purpose of the sort is often to allow for more efficient Purpose of the sort is often to allow for more efficient searching algorithms, more efficient than searching algorithms, more efficient than O(n) O(n) for 1 elementfor 1 element

This weighs, when the number of lookups is large, i.e. large This weighs, when the number of lookups is large, i.e. large vs. the cost of the initial sortvs. the cost of the initial sort

In In Big-O notationBig-O notation, only the complexity , only the complexity nn of the data structure of the data structure is considered, i.e. the number is considered, i.e. the number nn of elements included of elements included

Page 5: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

5

Bubble Sort The Bubble Sort is the most intuitive sorting method, The Bubble Sort is the most intuitive sorting method,

but also a most costlybut also a most costly For a data structure of size For a data structure of size nn the cost to sort is the cost to sort is O(nO(n22)) To sort ascendingly, each element in turn is compared To sort ascendingly, each element in turn is compared

against all other against all other nn elements to determine the correct elements to determine the correct position; uniqueness assumedposition; uniqueness assumed

That means, each of That means, each of nn elements is compared against elements is compared against O(n)O(n) other elements other elements

In reality, only comparison against In reality, only comparison against n-in-i are needed, with are needed, with i = 1..n-1i = 1..n-1, but in , but in Big-O Big-O notation such an effective notation such an effective reduction factor ½ does not have any impact on the reduction factor ½ does not have any impact on the Big-O cost functionBig-O cost function

Page 6: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

6

Bubble Sort Implementation

// bubble sort sorts in ascending order// bubble sort sorts in ascending order// assume elements to be unique in data structure a[]// assume elements to be unique in data structure a[]// there are // there are MAXMAX integers included in integers included in a[]a[]// .. Core of some bubble sort algorithm// .. Core of some bubble sort algorithmfor ( int outer = 0; outer < MAX-1; outer++ ) {for ( int outer = 0; outer < MAX-1; outer++ ) {

for ( int inner = outer+1; inner < MAX; inner++ ) {for ( int inner = outer+1; inner < MAX; inner++ ) {if ( a[ outer ] > a[ inner ] ) {if ( a[ outer ] > a[ inner ] ) {

// element at a[ outer ] is larger! Exchange!// element at a[ outer ] is larger! Exchange!swap( a[ inner ], a[ outer ] ); // C++swap( a[ inner ], a[ outer ] ); // C++

} //end if} //end if} //end for} //end for

} //end for} //end for

Page 7: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

7

Bubble Sort, swap() with & Parameter// swap works with & ref parameters// swap works with & ref parameters

// else resort to de-tour via pointers// else resort to de-tour via pointers

// swap() makes no assumption, where// swap() makes no assumption, where

// val1 and val2 are stored// val1 and val2 are stored

// Also, since val1 and val2 are reference parameters// Also, since val1 and val2 are reference parameters

// actuals do not need to be passed with “address of”// actuals do not need to be passed with “address of”

void void swapswap( int & val1, int & val2 ) ( int & val1, int & val2 ) // must be C++// must be C++

{ // swap{ // swap

int temp = val1;int temp = val1;

val1 = val2;val1 = val2;

val2 = temp;val2 = temp;

} //end swap} //end swap

Page 8: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

8

Bubble Sort, swap() in Situ// sort in ascending order// sort in ascending order// assume elements to be unique in data structure a[]// assume elements to be unique in data structure a[]// there are MAX integers inside a[]// there are MAX integers inside a[]// .. Core of some bubble sort algorithm// .. Core of some bubble sort algorithmfor ( int outer = 0; outer < MAX-1; outer++ ) {for ( int outer = 0; outer < MAX-1; outer++ ) {

for ( int inner = outer+1; inner < MAX; inner++ ) {for ( int inner = outer+1; inner < MAX; inner++ ) {if ( a[ outer ] > a[ inner ] ) {if ( a[ outer ] > a[ inner ] ) {

// element at lower index outer is larger!// element at lower index outer is larger!int temp = a[ inner ];int temp = a[ inner ];a[ inner ] = a[ outer ];a[ inner ] = a[ outer ];a[ outer ] = temp;a[ outer ] = temp;// swapping done in situ!// swapping done in situ!

} //end if} //end if} //end for} //end for

} //end for} //end for

Page 9: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

9

Bubble Sort, ptr_swap()

// sort in ascending order// sort in ascending order// assume elements to be unique in data structure a[]// assume elements to be unique in data structure a[]// there are MAX integers inside a[]// there are MAX integers inside a[]// .. Core of some bubble sort algorithm// .. Core of some bubble sort algorithmfor ( int outer = 0; outer < MAX-1; outer++ ) {for ( int outer = 0; outer < MAX-1; outer++ ) {

for ( int inner = outer+1; inner < MAX; inner++ ) {for ( int inner = outer+1; inner < MAX; inner++ ) {if ( a[ outer ] > a[ inner ] ) {if ( a[ outer ] > a[ inner ] ) {

// element at lower index outer is larger!// element at lower index outer is larger!ptr_swap( & a[ inner ], & a[ outer ] );ptr_swap( & a[ inner ], & a[ outer ] );

} //end if} //end if} //end for} //end for

} //end for} //end for

Page 10: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

10

Bubble Sort, ptr_swap()

// can be C or C++// can be C or C++

// val1 and val2 are *int parameters// val1 and val2 are *int parameters

// This is how C programmer can get around ref parameters// This is how C programmer can get around ref parameters

void void ptr_swapptr_swap( int * val1, int * val2 )( int * val1, int * val2 )

{ // swap{ // swap

int tempint temp = *val1;= *val1;

*val1*val1 = *val2;= *val2;

*val2*val2 = temp;= temp;

} //end ptr_swap} //end ptr_swap

Page 11: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

11

Insertion Sort Insertion sort is a sorting algorithm that is relatively

efficient for mostly sorted lists Elements from the list are removed, and then placed, one

at a time and inserted in their correct position in a new sorted list

The remaining list is moved up (or down) by one position, possible due to the place freed by the moved element

Ai > Ai Ai …Partially sorted Unsorted data

Ai Ai > Ai …Partially sorted Unsorted data

Insert

Page 12: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

12

Insertion Sort If the original list is largely unsorted, the If the original list is largely unsorted, the

cost for cost for insertion sortinsertion sort becomes similar, even becomes similar, even equal to the bubble sortequal to the bubble sort

For lists that are almost totally sorted, the For lists that are almost totally sorted, the cost for cost for insertion sortinsertion sort can be low, even can be low, even O(1)O(1) in Big-O notationin Big-O notation

Page 13: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

13

Insertion Sort, Method Goal is a list in ascending order:Goal is a list in ascending order: Start at indexStart at index i=1i=1, fetch , fetch value = list[i]value = list[i];;

then all the way up the last element then all the way up the last element i=MAX-1i=MAX-1 Set Set j = i-1j = i-1 and compare and compare valuevalue against against

list[j]list[j] As long as element As long as element list[j]list[j] is larger than is larger than

valuevalue, it is out of place, it must be shifted to a , it is out of place, it must be shifted to a higher index, up to where higher index, up to where valuevalue was fetched was fetched

In the end, In the end, valuevalue is placed into the slot freed is placed into the slot freed

Page 14: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

14

Insertion Sort. . .. . .// very clever// very cleverfor ( i = 1; i < MAX-1; i++ ) {for ( i = 1; i < MAX-1; i++ ) {

value = list[ i ];value = list[ i ];j = i - 1;j = i - 1;while( ( j >= 0 ) && ( list[ j ] > value ) ) {while( ( j >= 0 ) && ( list[ j ] > value ) ) {

list[ j+1 ] = list[ j ];list[ j+1 ] = list[ j ]; // push up// push up--j;--j; // check next// check next

} //end while} //end whilelist[ j + 1 ] = value;list[ j + 1 ] = value; // the right place// the right place

} //end for} //end for

Page 15: 1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015

15

Insertion Sort

The simplicity of the algorithm is strikingThe simplicity of the algorithm is striking The cost is not worse than that of the bubble The cost is not worse than that of the bubble

sortsort For lucky cases, the cost function can be For lucky cases, the cost function can be

way lower than the way lower than the O(nO(n22)) of the bubble sort of the bubble sort In rare cases it may be In rare cases it may be O(1)O(1), something not , something not

possible with the bubble sortpossible with the bubble sort