-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmergeSort.html
190 lines (147 loc) · 16.2 KB
/
mergeSort.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- <title>Document</title> -->
<title>Merge Sort</title>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
crossorigin="anonymous"
/>
<link
href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="src/home/css/style.css" />
<link rel="stylesheet" href="src/home/css/morebutton.css" />
<link rel="stylesheet" href="src\seive_Of_Eratosthenes\style.css" />
<script src="https://use.fontawesome.com/7ade57373d.js"></script>
<!-- Page icon -->
<link rel="icon" href="./src/assets/logo.png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>
<header>
<div id="header-inner">
<a href="#" id="logo"></a>
<a class="navbar-brand1" rel="home" href="#" title="Structurex">
<img src="src/assets/nlogo.png" class="main-logo">
<href="#" class="name">Structurex</a>
<div class="container1 blue" id="navbarNav">
<!-- ------HAMBURGER MENU -------->
<!-- -----------NEW CHANGES----------- -->
<i class="fas fa-2x fa-bars" style="color:white;display: none;" onclick="display();"></i>
<ul class="heck" id="heck">
<a href="index.html" class="link">Home</a>
<a href="sort.html" class="link">Sorting</a>
<a href="search.html" class="link">Searching</a>
<a href="./Path Finding/index.html" class="link">Path Finding</a>
<a href="tree.html" class="link" class="link">Graph</a>
<div class="dropdown">
<button onclick="show_hide()" class="Morebutton" > MORE</button>
<center>
<div id="list-items">
<!-- Add more list items here if required -->
<a href="seiveErato.html" >Sieve Of Eratosthenes</a>
</div>
</center>
</div>
</ul>
</div>
</div>
</header>
<div class="algo">
<br>
<br>
<h1>Merge Sort</h1>
<p>Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
<br>
Merge sort first divides the array into equal halves and then combines them in a sorted manner.</p>
<br>
<br>
<div class="box1">
<div class="Process">
<h1>How Merge Sort Works?</h1>
<p>To understand merge sort, we take an unsorted array as the following −</p>
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/unsorted_array.jpg">
<p>We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4.</p>
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/merge_sort_divide_1.jpg" alt="">
<p>We further divide these arrays and we achieve atomic value which can no more be divided.</p>
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/merge_sort_divide_3.jpg" alt="">
<p>Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.</p>
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/merge_sort_combine_1.jpg" alt="">
<p>In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in a sorted order.</p>
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/merge_sort_combine_2.jpg" alt="">
<p>After the final merging, the list should look like this −</p>
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/merge_sort.jpg" alt="">
<p>Now we should learn some programming aspects of merge sorting.</p>
<div class ="algorithm">
<h3>Algorithm</h3>
<p>Merge sort keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller sorted lists keeping the new list sorted too.
</p>
<p> <b>Step 1 −</b> if it is only one element in the list it is already sorted, return.
<br><b>Step 2 −</b> divide the list recursively into two halves until it can no more be divided.
<br><b>Step 3 − </b>merge the smaller lists into new list in sorted order.</p>
</div>
</div>
<div class = "code">
<pre class="prettyprint notranslate prettyprinted" style=""><span class="com">#include</span><span class="pln"> </span><span class="str"><stdio.h></span><span class="pln">
</span><span class="com">#define</span><span class="pln"> max </span><span class="lit">10</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> a</span><span class="pun">[</span><span class="lit">11</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">14</span><span class="pun">,</span><span class="pln"> </span><span class="lit">19</span><span class="pun">,</span><span class="pln"> </span><span class="lit">26</span><span class="pun">,</span><span class="pln"> </span><span class="lit">27</span><span class="pun">,</span><span class="pln"> </span><span class="lit">31</span><span class="pun">,</span><span class="pln"> </span><span class="lit">33</span><span class="pun">,</span><span class="pln"> </span><span class="lit">35</span><span class="pun">,</span><span class="pln"> </span><span class="lit">42</span><span class="pun">,</span><span class="pln"> </span><span class="lit">44</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="pun">};</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> b</span><span class="pun">[</span><span class="lit">10</span><span class="pun">];</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> merging</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> low</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> mid</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> high</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> l1</span><span class="pun">,</span><span class="pln"> l2</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">for</span><span class="pun">(</span><span class="pln">l1 </span><span class="pun">=</span><span class="pln"> low</span><span class="pun">,</span><span class="pln"> l2 </span><span class="pun">=</span><span class="pln"> mid </span><span class="pun">+</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> low</span><span class="pun">;</span><span class="pln"> l1 </span><span class="pun"><=</span><span class="pln"> mid </span><span class="pun">&&</span><span class="pln"> l2 </span><span class="pun"><=</span><span class="pln"> high</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">if</span><span class="pun">(</span><span class="pln">a</span><span class="pun">[</span><span class="pln">l1</span><span class="pun">]</span><span class="pln"> </span><span class="pun"><=</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">l2</span><span class="pun">])</span><span class="pln">
b</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">l1</span><span class="pun">++];</span><span class="pln">
</span><span class="kwd">else</span><span class="pln">
b</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">l2</span><span class="pun">++];</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">while</span><span class="pun">(</span><span class="pln">l1 </span><span class="pun"><=</span><span class="pln"> mid</span><span class="pun">)</span><span class="pln">
b</span><span class="pun">[</span><span class="pln">i</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">l1</span><span class="pun">++];</span><span class="pln">
</span><span class="kwd">while</span><span class="pun">(</span><span class="pln">l2 </span><span class="pun"><=</span><span class="pln"> high</span><span class="pun">)</span><span class="pln">
b</span><span class="pun">[</span><span class="pln">i</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">l2</span><span class="pun">++];</span><span class="pln">
</span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span><span class="pln"> low</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><=</span><span class="pln"> high</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln">
a</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> b</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> sort</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> low</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> high</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> mid</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">if</span><span class="pun">(</span><span class="pln">low </span><span class="pun"><</span><span class="pln"> high</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
mid </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">low </span><span class="pun">+</span><span class="pln"> high</span><span class="pun">)</span><span class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
sort</span><span class="pun">(</span><span class="pln">low</span><span class="pun">,</span><span class="pln"> mid</span><span class="pun">);</span><span class="pln">
sort</span><span class="pun">(</span><span class="pln">mid</span><span class="pun">+</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> high</span><span class="pun">);</span><span class="pln">
merging</span><span class="pun">(</span><span class="pln">low</span><span class="pun">,</span><span class="pln"> mid</span><span class="pun">,</span><span class="pln"> high</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">return</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"List before sorting\n"</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><=</span><span class="pln"> max</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"%d "</span><span class="pun">,</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]);</span><span class="pln">
sort</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> max</span><span class="pun">);</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"\nList after sorting\n"</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><=</span><span class="pln"> max</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"%d "</span><span class="pun">,</span><span class="pln"> a</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]);</span><span class="pln">
</span><span class="pun">}</span></pre>
</div>
</div>
<p style=" margin-left: 50%;">If we compile and run the above program, it will produce the following result −</p>
<h2 style=" margin-left: 50%;">Output</h2>
<div class="output">
<pre class="result notranslate">List before sorting
10 14 19 26 27 31 33 35 42 44 0
List after sorting
0 10 14 19 26 27 31 33 35 42 44
</pre>
</div>
</div>
<script src="src/home/js/morebutton.js"></script>
<script src="src/home/js/script.js"></script>
</body>
</html>