0% found this document useful (0 votes)
34 views4 pages

Common Algorithm Exercise

Uploaded by

tokomoderntech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

Common Algorithm Exercise

Uploaded by

tokomoderntech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd

NumberPasses Changes

0 TRUE
NumberPasses ← 0 FALSE
Changes ← TRUE 0
//continue until one pass has no changes (changes = false) 1
//or it has looped array length - 1 times 1
WHILE Changes = TRUE And NumberPasses < LENGTH(DataArray) - 1 DO 1
//reset changes each time a new pass starts 1
Changes ← FALSE 1
//loop through each element in the array 1
//check if the data is in the correct order 1
FOR Index ← 0 to LENGTH(DataArray) - NumberPasses - 2 1
IF DataArray[Index] > DataArray[Index + 1] THEN 1 FALSE
//if not swap the items 0
Temp ← DataArray[Index] 0
DataArray[Index] ← DataArray[Index + 1] 1
DataArray[Index + 1] ← Temp 1
Changes ← TRUE 1
ENDIF 1
NEXT Index 1
NumberPasses ← NumberPasses + 1 1
ENDWHILE 2 FALSE
1
1
1
1
1
1
1
3 FALSE
1
1
1
1
1
1
4 FALSE
1
1
1
1
1
5 FALSE
0
1
1
1
6 FALSE
0
0
0
7
DataArray
Index DA[Index] DA[Index+1] Temp 0 1 2 3 4 5 6 7 8 9
10 20 13 5 4 0 19 1 9 7

0 10 20
1 20 13 20 13 20
2 20 5 20 5 20
3 20 4 20 4 20
4 20 0 20 0 20
5 20 19 20 19 20
6 20 1 20 1 20
7 20 9 20 9 20 20
8 20 7 20 7 20
10 13 5 4 0 19 1 9 7
0 10 13
1 13 5 13 5 13
2 13 4 13 4 13
3 13 0 13 0 13
4 13 19
5 19 1 19 1 19
6 19 9 19 9 19
7 19 7 19 7 19 20
10 5 4 0 13 1 9 7 19
0 10 5 10 5 10
1 10 4 10 4 10
2 10 0 10 0 10
3 10 13
4 13 1 13 1 13
5 13 9 13 9 13
6 13 7 13 7 13
5 4 0 10 1 9 7 13 19 20
0 5 4 5 4 5
1 5 0 5 0 5
2 5 10
3 10 1 10 1 10
4 10 9 10 9 10
5 10 7 10 7 10
4 0 5 1 9 7 10 13 19 20
0 4 0 4 0 4
1 4 5
2 5 1 5 1 5
3 5 9
4 9 7 9 7 9
0 4 1 5 7 9 10 13 19 20
0 0 4
1 4 1 4 1 4
2 4 5
3 5 7
0 1 4 5 7 9 10 13 19 20
0 0 1
1 1 4
2 4 5
0 1 4 5 7 9 10 13 19 20

You might also like