Title: 30.%20Sorting%201-dimensional%20Arrays
130. Sorting 1-dimensional Arrays
- Bubble Sort
- Insertion Sort
2Searching for an item in an unorganized
collection?
- May need to look through the whole collection to
find the target item - E.g., find value x in vector v
- Linear search
3- f is index of first occurrence
- of value x in vector v.
- f is -1 if x not found.
- k 1while kltlength(v) v(k)x k k
1 - end
- if kgtlength(v) f -1 signal for x not
foundelse f k - end
4- Linear Search f is index of first occurrence
- of value x in vector v.
- f is -1 if x not found.
- k 1while kltlength(v) v(k)x k k
1 - end
- if kgtlength(v) f -1 signal for x not
foundelse f k - end
12
15
35
33
42
45
v
x
31
5- Linear Search f is index of first occurrence
- of value x in vector v.
- f is -1 if x not found.
- k 1while kltlength(v) v(k)x k k
1 - end
- if kgtlength(v) f -1 signal for x not
foundelse f k - end
6- Linear Search f is index of first occurrence
- of value x in vector v.
- f is -1 if x not found.
- k 1while kltlength(v) v(k)x k k
1 - end
- if kgtlength(v) f -1 signal for x not
foundelse f k - end
12
15
35
33
42
45
v
x
31
7- Linear Search f is index of first occurrence
- of value x in vector v.
- f is -1 if x not found.
- k 1while kltlength(v) v(k)x k k
1 - end
- if kgtlength(v) f -1 signal for x not
foundelse f k - end
45
v
x
31
What if v is sorted?
8Sorting data allows us to search more easily
2008 Boston Marathon Top Women Finishers
Place Bib Name Official Time State Country Ctz
1 F7 Tune, Dire 22525 Â ETH Â
2 F8 Biktimirova, Alevtina 22527 Â RUS Â
3 F4 Jeptoo, Rita 22634 Â KEN Â
4 F2 Prokopcuka, Jelena 22812 Â LAT Â
5 F5 Magarsa, Askale Tafa 22948 Â ETH Â
6 F9 Genovese, Bruna 23052 Â ITA Â
7 F12 Olaru, Nuta 23356 Â ROM Â
8 F6 Guta, Robe Tola 23437 Â ETH Â
9 F1 Grigoryeva, Lidiya 23537 Â RUS Â
10 F35 Hood, Stephanie A. 24444 IL USA CAN
11 F14 Robson, Denise C. 24554 NS CAN Â
12 F11 Chemjor, Magdaline 24625 Â KEN Â
13 F101 Sultanova-Zhdanova, Firaya 24717 FL USA RUS
14 F15 Mayger, Eliza M. 24736 Â AUS Â
15 F24 Anklam, Ashley A. 24843 MN USA
Name Score Grade
Jorge 92.1
Ahn 91.5
Oluban 90.6
Chi 88.9
Minale 88.1
Bell 87.3
9The bubble process
30
50
10
60
40
Compare adjacent values. Swap if out of order.
20
10The bubble process
30
50
10
60
Compare adjacent values. Swap if out of order.
20
40
11The bubble process
30
50
10
Compare adjacent values. Swap if out of order.
20
60
40
12The bubble process
30
50
Compare adjacent values. Swap if out of order.
10
20
60
40
13The bubble process
30
Compare adjacent values. Swap if out of order.
10
50
20
60
40
14The bubble process
10
The smallest (lightest) value bubbles to the
top Done in one pass through the vector
30
50
20
60
40
Bubble.m
15The second bubble process
10
30
50
20
60
Compare adjacent values. Swap if out of order.
40
16The second bubble process
10
30
50
20
Compare adjacent values. Swap if out of order.
40
60
17The second bubble process
10
30
50
Compare adjacent values. Swap if out of order.
20
40
60
18The second bubble process
10
30
Compare adjacent values. Swap if out of order.
20
50
40
60
19The second bubble process
10
After two bubble processes, the first two
components are sorted. Repeatedly apply the
bubble process to sort the whole array
20
30
50
40
60
20Sort vector x using the Bubble Sort algorithm
x
Apply Bubble to x x,C,S Bubble(x)
21Sort vector x using the Bubble Sort algorithm
x
Bubble x x,C,S Bubble(x)
Bubble x(26) x(26),C,S Bubble(x(26))
Bubble x(36) x(36),C,S Bubble(x(36))
Bubble x(46) x(46),C,S Bubble(x(46))
Bubble x(56) x(56),C,S Bubble(x(56))
BubbleSort1.m
22Possible to get a sorted vector before n-1
bubble processes
10
After 2 bubble processes Start 3rd bubble
process
20
30
50
40
60
23Possible to get a sorted vector before n-1
bubble processes
10
In the 3rd bubble process
20
30
50
40
60
24Possible to get a sorted vector before n-1
bubble processes
10
In the 3rd bubble process
20
30
40
50
60
25Possible to get a sorted vector before n-1
bubble processes
After the 3rd bubble process Vector is
completely sorted (in this example) How to
improve BubbleSort to quit early?
10
20
30
40
50
60
26Possible to get a sorted vector before n-1
bubble processes
After the 3rd bubble process Vector is
completely sorted (in this example) How to
improve BubbleSort to quit early?
10
20
30
40
50
60
Keep track of the swaps! No swap is done when
vector is sorted.
BubbleSort.m
27The Insertion Process
- Given a sorted array x, insert a number y such
that the result is sorted
2
3
6
9
8
28Insertion
2
3
6
9
8
Just swap 8 9
29Insertion
2
3
6
9
8
2
3
6
9
8
2
3
6
9
8
30Insertion
2
3
6
9
8
2
3
6
9
8
Compare adjacent components swap 9 4
4
2
3
6
9
8
31Insertion
2
3
6
9
8
2
3
6
9
8
4
2
3
6
9
8
Compare adjacent components swap 8 4
2
3
6
9
8
4
32Insertion
2
3
6
9
8
2
3
6
9
8
4
2
3
6
9
8
2
3
6
9
8
4
Compare adjacent components swap 6 4
2
3
6
9
8
4
33Insertion
2
3
6
9
8
2
3
6
9
8
4
2
3
6
9
8
2
3
6
9
8
4
2
3
6
9
8
4
Compare adjacent components DONE! No more swaps.
2
3
6
9
8
4
Insert.m
34Sort vector x using the Insertion Sort algorithm
Need to start with a sorted subvector. How do
you find one?
x
Length 1 subvector is sorted
Insert x(2) x(12),C,S Insert(x(12))
Insert x(3) x(13),C,S Insert(x(13))
Insert x(4) x(14),C,S Insert(x(14))
Insert x(5) x(15),C,S Insert(x(15))
Insert x(6) x(16),C,S Insert(x(16))
InsertionSort.m
35Bubble Sort vs. Insertion Sort
- Both involve comparing adjacent values and swaps
- On average, which is more efficient?
36Other efficiency considerations
- Worst case, best case, average case
- Use of subfunction incurs an overhead
- Memory use and access
- Example Rather than directing the insert
process to a subfunction, have it done in-line.
- Also, Insertion sort can be done in-place,
i.e., using only the memory space of the
original vector.
37- function x insertSort(x)
- Sort vector x in ascending order with insertion
sort -
- n length(x)
- for i 1n-1
- Sort x(1i1) given that x(1i) is sorted
-
-
-
-
-
-
-
-
-
- end
38- function x insertSort(x)
- Sort vector x in ascending order with insertion
sort -
- n length(x)
- for i 1n-1
- Sort x(1i1) given that x(1i) is sorted
- j i
- need2swap
- while need2swap
- swap x(j1) and x(j)
-
-
- j j-1
- need2swap
- end
- end
39- function x insertSort(x)
- Sort vector x in ascending order with insertion
sort -
- n length(x)
- for i 1n-1
- Sort x(1i1) given that x(1i) is sorted
- j i
- need2swap x(j1) lt x(j)
- while need2swap
- swap x(j1) and x(j)
-
-
- j j-1
- need2swap jgt0 x(j1)ltx(j)
- end
- end
40- function x insertSort(x)
- Sort vector x in ascending order with insertion
sort -
- n length(x)
- for i 1n-1
- Sort x(1i1) given that x(1i) is sorted
- j i
- need2swap x(j1) lt x(j)
- while need2swap
- swap x(j1) and x(j)
- temp x(j)
- x(j) x(j1)
- x(j1) temp
- j j-1
- need2swap jgt0 x(j1)ltx(j)
- end
- end