05 Shell
05 Shell
Spatial locality
– If a particular storage location is referenced at a particular
time, then it is likely that nearby memory locations will be
referenced in the near future.
Fast
For i in 0..n
For j in 0.. m
sum += A[i][j]
Fast
End goal:
1) All cards are sorted by suit
2) Within each suit, cards are sorted by number
–"Sorting stability playing cards" by User:Dcoetzee, User:WDGraham - Own work, based on File:Cards-2-Heart.svg, File:Cards-7-Spade.svg, File:Cards-5-Spade.svg, File:Cards-5-Heart.svg.
–Licensed under Creative Commons Zero, Public Domain Dedication via Wikimedia Commons –
– http://commons.wikimedia.org/wiki/File:Sorting_stability_playing_cards.svg#mediaviewer/File:Sorting_stability_playing_cards.svg
–"Sorting stability playing cards" by User:Dcoetzee, User:WDGraham - Own work, based on File:Cards-2-Heart.svg, File:Cards-7-Spade.svg, File:Cards-5-Spade.svg, File:Cards-5-Heart.svg.
–Licensed under Creative Commons Zero, Public Domain Dedication via Wikimedia Commons –
– http://commons.wikimedia.org/wiki/File:Sorting_stability_playing_cards.svg#mediaviewer/File:Sorting_stability_playing_cards.svg
= (n-1)*((n-1)+1)/2
= n2/2 - n/2
Result: O(n2) (both copy and compare)
Pass 1, k = 5
3 5 6 3 4 5 1 2 j=5 no
3 5 6 3 4 5 1 2 j=6 yes
3 1 6 3 4 5 5 2 j=7 yes
3 1 2 3 4 5 5 6
Pass 2, k = 1
3 1 2 3 4 5 5 6 j = 1, yes
1 3 2 3 4 5 5 6 j = 2, yes
1 2 3 3 4 5 5 6 no
1 2 3 3 4 5 5 6 j = 3, no
for j ← 1 to n-1
temp_r ← r[j]
i ← j
while i > 0 and r[i-1] > temp_r
r[i] ← r[i-1]
i ← i-1
r[i] ← temp_r
while (stride) {
max = gidsetsize - stride;
for (base = 0; base < max; base++) {
int left = base;
int right = left + stride;
int tmp = g[right];