Skip to content

Commit cb266c2

Browse files
committed
Sorting
1 parent 17bd41f commit cb266c2

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed
62.8 KB
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="heapSort" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="Debug">
10+
<Option output="bin/Debug/heapSort" prefix_auto="1" extension_auto="1" />
11+
<Option object_output="obj/Debug/" />
12+
<Option type="1" />
13+
<Option compiler="gcc" />
14+
<Compiler>
15+
<Add option="-g" />
16+
</Compiler>
17+
</Target>
18+
<Target title="Release">
19+
<Option output="bin/Release/heapSort" prefix_auto="1" extension_auto="1" />
20+
<Option object_output="obj/Release/" />
21+
<Option type="1" />
22+
<Option compiler="gcc" />
23+
<Compiler>
24+
<Add option="-O2" />
25+
</Compiler>
26+
<Linker>
27+
<Add option="-s" />
28+
</Linker>
29+
</Target>
30+
</Build>
31+
<Compiler>
32+
<Add option="-Wall" />
33+
<Add option="-fexceptions" />
34+
</Compiler>
35+
<Unit filename="main.cpp" />
36+
<Extensions>
37+
<code_completion />
38+
<envvars />
39+
<debugger />
40+
</Extensions>
41+
</Project>
42+
</CodeBlocks_project_file>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* This algorithm is based on the sorting technique called head sort
2+
Time complexity of heapify is O(Log n).
3+
Time complexity of createAndBuildHeap() is O(n) and
4+
overall time complexity of Heap Sort is O(n Log n).
5+
*/
6+
#include <iostream>
7+
#include <cstdlib>
8+
using namespace std;
9+
10+
void heapify(int arr[], int n, int i)
11+
{
12+
int largest = i; // Initialize largest as root
13+
int l = 2*i + 1;
14+
int r = 2*i + 2;
15+
16+
// If left child is larger than root
17+
if (l < n && arr[l] > arr[largest])
18+
largest = l;
19+
20+
// If right child is larger than largest so far
21+
if (r < n && arr[r] > arr[largest])
22+
largest = r;
23+
24+
if (largest != i)
25+
{
26+
swap(arr[i], arr[largest]);
27+
28+
// Recursively heapify the affected sub-tree
29+
heapify(arr, n, largest);
30+
}
31+
}
32+
33+
34+
void heapSort(int arr[], int n) {
35+
// to create a heap call heapify
36+
for(int i = n/2 - 1; i >= 0; i--) {
37+
heapify(arr, n, i);
38+
}
39+
for(int i = n - 1; i >= 0; i--) {
40+
// Move current root to end
41+
swap(arr[0], arr[i]); // swap function is built-in
42+
43+
// call max heapify on the reduced heap
44+
heapify(arr, i, 0);
45+
}
46+
}
47+
48+
int main() {
49+
int arr[] = {10, 9, 8, 7, 6, 5, 4, 4, 3, 3, 3, 1};
50+
int n = sizeof(arr)/sizeof(arr[0]);
51+
52+
heapSort(arr, n);
53+
cout<<"After Sorting: ";
54+
for(int i = 0; i < n; i++) {
55+
cout<<arr[i]<<" "<<endl;
56+
}
57+
return 0;
58+
}
22.5 KB
Binary file not shown.

SortingAlgorithms/mergeSort/mergeSort.layout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ActiveTarget name="Debug" />
55
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
66
<Cursor>
7-
<Cursor1 position="0" topLine="0" />
7+
<Cursor1 position="1705" topLine="47" />
88
</Cursor>
99
</File>
1010
</CodeBlocks_layout_file>

0 commit comments

Comments
 (0)