Partial preview of the text
Download cosc 2436 homework assignment and more Assignments Computer Science in PDF only on Docsity!
ddd A A A A A A DATA STRUCTURES MUD TERM RIEWILEW/ 1. Sorting Algorithms ~~ > —” Code Example: bubbleSort ( arr(l, size) { ( i= 0; i< size - 1; i++) { j=; j < size-1- i; j++ (arr[j] > arr[j+1]) { temp = arr[jl; arr[j] = arr[j+1]; arr[j+1] = temp; = —— N SS - “NN x _ . x “XN Let’s sort the following array: [5, 7, 3, 1, 6, 8] —— + Time Complexity: Best Case: O(n“*2) Worst Case: O(n*2) 7 —— SE What is Enhanced Bubble Sort? ~~ 4 ais iN ——vKe€—_—_———___—— — > — P| ————— —_ . Ll ‘“ : Pa : a bubbleSortV2( arr{], size) { sorted; ( i= 0; i< size - 1; i++) { sorted = A j =; j < size-1- i; j++ (arr[j] > arr{j+1]) { temp = arr[jl; arr(j] = arr[j+1]; arr[j+1] = temp; sorted = H sorted F = —— N SS - ~,™ x _ . x “NN Let’s sort the following array: [5, 7, 3, 1, 2, 6, 8] oe * Time Complexity: Best Case: O(n) (array is already sorted) Worst Case: O(n*2) (array is in descending order) + ———— > _” Code Example: selectionSort( arr{], size) { t min_index; ( i= 0; i< size -1; i++) { min_index = i; j=iti1; j < size; j++ (arr[j] < arr[min_index]) { min_index = j; } temp = arr[il; arr[i] = arr[min_index]; arr[min_index] = temp; = —— N SS - ~,™ x _ . x “NN Let’s sort the following array: [5, 7, 3, 1, 6, 8]