0array1234Bubble Sort Example734211585Spring 2022Sacramento State - Cook - CSc 13022Outer LoopInner Loop
0array1234Bubble Sort Example734258115Spring 2022Sacramento State - Cook - CSc 13023Outer LoopInner Loop
0array1234Bubble Sort Example731158425Spring 2022Sacramento State - Cook - CSc 13024Outer LoopInner Loop
0array1234Bubble Sort Example421158735Spring 2022Sacramento State - Cook - CSc 13025Outer LoopInner Loop
0array1234Bubble Sort Example421158735Spring 2022Sacramento State - Cook - CSc 13026Outer LoopInner Loop
0array1234Bubble Sort Example573584211Spring 2022Sacramento State - Cook - CSc 13027Outer LoopInner Loop
0array1234Bubble Sort Example542587311Spring 2022Sacramento State - Cook - CSc 13028Outer LoopInner Loop
0array1234Bubble Sort Example542581173Spring 2022Sacramento State - Cook - CSc 13029Outer LoopInner Loop
0array1234Bubble Sort Example511587342Spring 2022Sacramento State - Cook - CSc 13030Outer LoopInner Loop
0array1234Bubble Sort Example511427358Spring 2022Sacramento State - Cook - CSc 13031Outer LoopInner Loop
0array1234Bubble Sort Example511427358Spring 2022Sacramento State - Cook - CSc 13032Outer LoopInner Loop
The Bubble Sort isextremelyinefficient and onlygood for tiny arraysSince Bubble Sort uses two embedded loops•the outer loop looks at allnitems•the inner loop looks at basicallynitems•the resulting algorithm getsexponentiallyless efficientasnincreasesEfficiency of the Bubble SortSpring 2022Sacramento State - Cook - CSc 13033
Efficiency of the Bubble SortSpring 2022Sacramento State - Cook - CSc 13034The Bubble SortO(n2)… two embedded loops thatare based onn… and all that swappingdoesn't help either!
Bubble SortTime AverageO(n2)Time BestO(n2)Time WorstO(n2)Auxiliary spaceO(1)StableYes – Equal element order preservedOnline?No – Entire array in useSpring 2022Sacramento State - Cook - CSc 13035Bubble Sort Summary
Selection SortThe Human Way
TheSelection Sortis a similarto the Bubble SortHowever…•rather than "bubble up" smalleritems, it scans the entire array•it finds the smallest element•onlythendoes it swap thevaluesSelection SortSpring 2022Sacramento State - Cook - CSc 13037
Like the Bubble Sort, it consists of two For Loops –one outer and one innerOuter loop runs from the first to the lastInner loop …•starts at the position of the outer loop•scans down and finds thesmallestvalueThen, after the scan, do a single swapSelection SortSpring 2022Sacramento State - Cook - CSc 13038
for(i= 0;i< count-1;i++){best=i;for(j=i;j< count;j++){if (array[j] < array[best]){best=j;}}//swap array[i] and array[best]}The Selection SortSpring 2022Sacramento State - Cook - CSc 13039
Selection Sort ExampleSpring 2022Sacramento State - Cook - CSc 130400array1234734211585Outer LoopInner Loop
Selection Sort Example: New BestSpring 2022Sacramento State - Cook - CSc 130410array1234734211585Outer LoopInner Loop
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 203 pages?
Upload your study docs or become a
Course Hero member to access this document
Term
Fall
Professor
Nonya
Tags
Array data structure