COSC 236 Lecture Notes - Lecture 10: Linear Search, Quicksort, Selection Sort
Document Summary
20 25 30 40 55: example of one not: There are many strategies for sorting arrays: among them: Let a[] be an array of length n. Let last be an index in the range of the arrary: 0 < last < n-1 //last is an index that keeps track of where you are in the array. A[(cid:1004)last] de(cid:374)otes the portio(cid:374) of the array (cid:272)o(cid:374)sisti(cid:374)g of a[(cid:1004)], a[(cid:1005)], ,a[last] For (last = n-1; last>= 1; last--) Mo(cid:448)e the largest e(cid:374)try i(cid:374) a[(cid:1004) last] to a[last] They are similar: both use the simple sorting strategy. Differ in how they implement the step: move the largest of a[(cid:1004) last] to a[last] Bubble sort: mo(cid:448)es the largest e(cid:374)try to the e(cid:374)d of a[(cid:1004) last] (cid:271)y (cid:272)o(cid:373)pari(cid:374)g a(cid:374)d s(cid:449)appi(cid:374)g the adjacent elements as an index sweeps through the unsorted portion of the array: 15 35 20 10 25 //compare a[0], a[1], no swap. 15 35 20 10 25 //compare a[1], a[2], swap.