CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer,...
Transcript of CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer,...
![Page 1: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/1.jpg)
CS 161Introduction to Programming
and Problem Solving
Chapter 22Sorting Methods
Herbert G. Mayer, PSUStatus 9/8/2014
Initial content copied verbatim fromECE 103 material by prof. Phillip Wong @ PSU
![Page 2: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/2.jpg)
Syllabus Sorting Bubble Sort Sorting Algorithms Insertion Sort Pseudo Code
![Page 3: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/3.jpg)
3
Sorting is the process of arranging a collection of items in a specified order
Each sorting algorithm has its own unique advantages and disadvantages
Popular sorting algorithms:(computational time complexity – average case)
• Bubble sort O(n2)• Selection sort O(n2)• Insertion sort O(n2)• Shell sort O(nlog2n)
• Merge sortO(nlogn)
• Heap sortO(nlogn)
• Quick sortO(nlogn)
See: http://en.wikipedia.org/wiki/Sorting_algorithm
Sorting
![Page 4: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/4.jpg)
4
Bubble Sort
In a double nested loop across all elements: Look at all pairs of elements Pick the larger of the 2 in a pair And place the larger into the lower indexed array element, if
increasing order In the end the array is sorted in increasing (or non-
descending) order For decreasing order, reverse the logic
![Page 5: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/5.jpg)
5
Bubble Sort
#include <stdio.h>
#define LIMIT 12
int a[ LIMIT ] ={ 1, -1, 2, -2, 100, -100, 55, -55, 7, 77, 777, 7777 };
void print( char * msg ){ // print int i; printf( "%s array a{%d] = ", msg, LIMIT ); for( i = 0; i < LIMIT; i++ ) { printf( "%d ", a[ i ] ); } //end for printf( "\n" );} //end print
![Page 6: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/6.jpg)
6
Bubble Sort1: Remember Where to exchangevoid sort() // sort in non-descending order{ // sort int inner, outer, index, max, temp;
for( outer = 0; outer < LIMIT-1; outer++ ) { max = a[ outer ]; index = outer; for( inner = outer+1; inner < LIMIT; inner++ ) { if( a[ inner ] > max ) { // found larger index = inner; max = a[ inner ]; } //end if } //end for if( a[ outer ] < max ) { // then swap them out temp = a[ outer ]; a[ outer ] = max; a[ index ] = temp; } //end if } //end for} //end sort
![Page 7: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/7.jpg)
7
Bubble Sort2; Exchange (swap()) on the Flyvoid swap( int * a_ptr, int * b_ptr ) //sort non-descending{ // swap int temp = *a_ptr; // remember what a_ptr points to *a_ptr = *b_ptr; // overwrite it *b_ptr = temp; // completed the swap} //end swap void sort(){ // sort int inner, outer;
for( outer = 0; outer < LIMIT-1; outer++ ) { for( inner = outer+1; inner < LIMIT; inner++ ) { if( a[ inner ] > a[ outer ] ) {
// swap right away swap( & a[ inner ], & a[ outer ] ); } //end if } //end for } //end for} //end sort
![Page 8: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/8.jpg)
8
Insertion Sort (from Wikipedia article)
“Insertion sort is a sorting algorithm that is relatively efficient for small lists and mostly sorted lists.”
Elements from the list are taken one at a time and inserted in their correct position in a new sorted list.
Ai > Ai Ai …
Partially sorted Unsorted data
Ai Ai > Ai …
Partially sorted Unsorted data
Insert
![Page 9: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/9.jpg)
9
“… the new list and the remaining elements can share the array's space, but insertion is expensive, requiring shifting all following elements over by one”
Computational complexity:Worst case O(n2)
Best case O(n)
Average case O(n2)
Ascending insertion sort example from Wikipedia
![Page 10: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/10.jpg)
10
Pseudocode:
FUNCTION Insertion_Sort(array A) // zero index based array// Outer loop selects Ai from unsorted sideFOR i = 1 to size(A)-1
value = Ai
j = i-1// Inner loop shifts elements on partially sorted side to make room for Ai.
WHILE j >= 0 and Aj > valueAj+1 = Aj
j = j-1END WHILEAj+1 = value // Insert Ai in its place on partially sorted side
END FOREND FUNCTION
Ai
0 1 2 … i-1 i i+1 … Size-1
0 1 2 3 4 5 6 7
![Page 11: CS 161 Introduction to Programming and Problem Solving Chapter 22 Sorting Methods Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim.](https://reader036.fdocuments.in/reader036/viewer/2022083008/56649f1e5503460f94c362fd/html5/thumbnails/11.jpg)
11
Implementation for integer array:
void Insertion_Sort( int A[], int size ){ // Insertion_Sort
int value, i, j;for( i = 1; i <= size-1; i++ ) {
value = A[i];j = i-1;while( j >= 0 && A[j] > value ) {
A[j+1] = A[j];j = j-1;
} //end whileA[j+1] = value;
} //end for} //end Insertion_Sort