-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.html
89 lines (74 loc) · 4.91 KB
/
bubble.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
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html>
<head>
<title>Algorithm and Complexity</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="links">
<a class="social facebook" href="https://web.facebook.com/profile.php?id=100011177262846&_rdr"><img src="images/facebook.png"></a>
<a class="social vk" href="#"><img src="images/vk.png"></a>
<a class="social twitter" href="#"><img src="images/twitter.png"></a>
</div>
<div id="main">
<div id="nav">
<ul>
<li><a href="index.html">Main •</a></li>
<li><a href="myvector.html">MyVector •</a></li>
<li><a href="bubble.html" style="color:red;">Bubble Sort •</a></li>
<li><a href="merge.html">Merge Sort •</a></li>
<li><a href="insert.html">Insert Sort •</a></li>
<li><a href="quick.html">Quick Sort •</a></li>
<li><a href="graphs.html" >Graphs •</a></li>
<li><a href="olsen.html">Olsen Gang</a></li>
</ul>
</div>
<div id="border"></div>
<h1>Bubble sort</h1>
<div id="main_explanation">
<p>Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.[1] It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.</p>
<p>Worst-case perfomance: O(n<sup>2</sup>)</p>
<p>Best-case perfomance: O(n);</p>
<p>Avarage perfomance: O(n<sup>2</sup>);</p>
</div>
<div id="algorithmGif">
<img id="bubble_gif" src="images/bubble.gif">
</div>
<div id="algorithm_Window">
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #557799">#include <iostream></span>
<span style="color: #557799">#include <ctime></span>
<span style="color: #557799">#include <cstdlib></span>
<span style="color: #008800; font-weight: bold">using</span> <span style="color: #008800; font-weight: bold">namespace</span> std;
<span style="color: #333399; font-weight: bold">void</span> <span style="color: #0066BB; font-weight: bold">bubble_sort</span>(<span style="color: #333399; font-weight: bold">int</span> ar[],<span style="color: #333399; font-weight: bold">int</span> length){
<span style="color: #333399; font-weight: bold">bool</span> ready<span style="color: #333333">=</span><span style="color: #007020">false</span>;
<span style="color: #008800; font-weight: bold">while</span>(ready<span style="color: #333333">==</span><span style="color: #007020">false</span>){
ready<span style="color: #333333">=</span><span style="color: #007020">true</span>;
<span style="color: #008800; font-weight: bold">for</span>(<span style="color: #333399; font-weight: bold">int</span> x<span style="color: #333333">=</span><span style="color: #0000DD; font-weight: bold">0</span>;x<span style="color: #333333"><</span>length<span style="color: #333333">-</span><span style="color: #0000DD; font-weight: bold">1</span>;x<span style="color: #333333">++</span>){
<span style="color: #008800; font-weight: bold">if</span>(ar[x]<span style="color: #333333">></span>ar[x<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span>]){
ready<span style="color: #333333">=</span><span style="color: #007020">false</span>;
<span style="color: #333399; font-weight: bold">int</span> temp<span style="color: #333333">=</span>ar[x];
ar[x]<span style="color: #333333">=</span>ar[x<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span>];
ar[x<span style="color: #333333">+</span><span style="color: #0000DD; font-weight: bold">1</span>]<span style="color: #333333">=</span>temp;
}
}
}
}
</div>
</pre>
</div>
<div id="bottom">
<ul>
<li><a href="index.html">Main </a></li>
<li><a href="myvector.html">MyVector </a></li>
<li><a href="bubble.html">Bubble Sort </a></li>
<li><a href="merge.html">Merge Sort </a></li>
<li><a href="insert.html">Insert Sort </a></li>
<li><a href="quick.html">Quick Sort </a></li>
<li><a href="olsen.html">Olsen Gang</a></li>
</ul>
</div>
</div>
</body>
</html>