forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
bubblesort.cpp.html
80 lines (70 loc) · 8.83 KB
/
bubblesort.cpp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>bubblesort.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">// Bubblesort of an array of 5 integers.</span></i>
<i><span style="color:#9A1900">//</span></i>
<i><span style="color:#9A1900">// Pass through the array right to left. On each pass through the</span></i>
<i><span style="color:#9A1900">// outer loop, the smallest remaining value will "bubble" over to its</span></i>
<i><span style="color:#9A1900">// appropriate final location in the left side of the array.</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<b><span style="color:#0000FF">const</span></b> <span style="color:#009900">int</span> MAX <span style="color:#990000">=</span> <span style="color:#993399">5</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> A<span style="color:#990000">[</span>MAX<span style="color:#990000">];</span>
<i><span style="color:#9A1900">// The following output line should NOT be encoded into IBCM -- it</span></i>
<i><span style="color:#9A1900">// is just to make the C++ implementation easier to understand</span></i>
<i><span style="color:#9A1900">// when run</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Enter "</span> <span style="color:#990000"><<</span> MAX <span style="color:#990000"><<</span> <span style="color:#FF0000">" numbers to sort, one per line."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// Read in the array values. For IBCM implementation, assume that</span></i>
<i><span style="color:#9A1900">// array is hard coded AFTER the end of the program to perform</span></i>
<i><span style="color:#9A1900">// bubblesort</span></i>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> k <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> k <span style="color:#990000"><</span> MAX<span style="color:#990000">;</span> k<span style="color:#990000">++)</span> <span style="color:#FF0000">{</span>
cin <span style="color:#990000">>></span> A<span style="color:#990000">[</span>k<span style="color:#990000">];</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// print out the user input to the screen</span></i>
cout <span style="color:#990000"><<</span> endl <span style="color:#990000"><<</span> <span style="color:#FF0000">"Original:"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> k <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> k <span style="color:#990000"><</span> MAX<span style="color:#990000">;</span> k<span style="color:#990000">++)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" "</span> <span style="color:#990000"><<</span> A<span style="color:#990000">[</span>k<span style="color:#990000">];</span>
<span style="color:#FF0000">}</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// Your IBCM program should be these 2 nested for loops plus some</span></i>
<i><span style="color:#9A1900">// additional variable declarations to store the information you</span></i>
<i><span style="color:#9A1900">// need. This is what you have to implement in IBCM.</span></i>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> MAX<span style="color:#990000">;</span> i<span style="color:#990000">++)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> j <span style="color:#990000">=</span> MAX <span style="color:#990000">-</span> <span style="color:#993399">1</span><span style="color:#990000">;</span> j <span style="color:#990000">></span> i<span style="color:#990000">;</span> j<span style="color:#990000">--)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>A<span style="color:#990000">[</span>j<span style="color:#990000">]</span> <span style="color:#990000"><</span> A<span style="color:#990000">[</span>j <span style="color:#990000">-</span> <span style="color:#993399">1</span><span style="color:#990000">])</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// swap A[j] and A[j-1]</span></i>
<span style="color:#009900">int</span> temp <span style="color:#990000">=</span> A<span style="color:#990000">[</span>j<span style="color:#990000">];</span>
A<span style="color:#990000">[</span>j<span style="color:#990000">]</span> <span style="color:#990000">=</span> A<span style="color:#990000">[</span>j <span style="color:#990000">-</span> <span style="color:#993399">1</span><span style="color:#990000">];</span>
A<span style="color:#990000">[</span>j <span style="color:#990000">-</span> <span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> temp<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// You don't have to worry about all this output for the IBCM version.</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"After Pass "</span> <span style="color:#990000"><<</span> i <span style="color:#990000"><<</span> <span style="color:#FF0000">":"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> k <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> k <span style="color:#990000"><</span> MAX<span style="color:#990000">;</span> k<span style="color:#990000">++)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" "</span> <span style="color:#990000"><<</span> A<span style="color:#990000">[</span>k<span style="color:#990000">];</span>
<span style="color:#FF0000">}</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// You DO NOT have to implement this. It is only provided so that</span></i>
<i><span style="color:#9A1900">// you may compile and run your program to understand how the</span></i>
<i><span style="color:#9A1900">// algorithm works.</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Sorted:"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> k <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> k <span style="color:#990000"><</span> MAX<span style="color:#990000">;</span> k<span style="color:#990000">++)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" "</span> <span style="color:#990000"><<</span> A<span style="color:#990000">[</span>k<span style="color:#990000">];</span>
<span style="color:#FF0000">}</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>