Download presentation
Presentation is loading. Please wait.
Published bySamantha Leonard Modified over 9 years ago
1
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE
2
1 Sorting 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(n 2 ) Selection sort O(n 2 ) Insertion sort O(n 2 ) Shell sort O(nlog 2 n) Merge sort O(nlogn) Heap sort O(nlogn) Quick sort O(nlogn) See: http://en.wikipedia.org/wiki/Sorting_algorithmhttp://en.wikipedia.org/wiki/Sorting_algorithm
3
Syllabus Bubble Sort Sorting Algorithms Insertion Sort Pseudo Code
4
3 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
5
4 Bubble Sort #include #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
6
5 Bubble Sort1: Remember Where to exchange void sort() { // 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 ) { 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
7
6 Bubble Sort2; Exchange (swap()) on the Fly void swap( int * a_ptr, int * b_ptr ) { // 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( & a[ inner ], & a[ outer ] ); } //end if } //end for } //end sort
8
7 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. A i > A i AiAi … Partially sortedUnsorted data A i AiAi > A i … Partially sortedUnsorted data Insert
9
8 “… 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(n 2 ) Best case O(n) Average case O(n 2 ) Ascending insertion sort example from Wikipedia
10
9 Pseudocode: FUNCTION Insertion_Sort(array A) // zero index based array // Outer loop selects A i from unsorted side FOR i = 1 to size(A)-1 value = A i j = i-1 // Inner loop shifts elements on partially sorted side to make room for A i. WHILE j >= 0 and A j > value A j+1 = A j j = j-1 END WHILE A j+1 = value // Insert A i in its place on partially sorted side END FOR END FUNCTION AiAi 012…i-1ii+1… Size-1 01234567
11
10 Implementation for integer array: void Insertion_Sort( int A[], int size ) { // Insertion_Sort int value, i, j; for( i = 1; i = 0 && A[j] > value ) { A[j+1] = A[j]; j = j-1; } //end while A[j+1] = value; } //end for } //end Insertion_Sort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.