Skip to content

Commit c2c3d1a

Browse files
committed
Merge Sort
1 parent 814f5fc commit c2c3d1a

File tree

5 files changed

+172
-2
lines changed

5 files changed

+172
-2
lines changed

.vscode/c_cpp_properties.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "windows-gcc-x86",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"compilerPath": "C:/MinGW/bin/gcc.exe",
9+
"cStandard": "${default}",
10+
"cppStandard": "${default}",
11+
"intelliSenseMode": "windows-gcc-x86",
12+
"compilerArgs": [
13+
""
14+
]
15+
}
16+
],
17+
"version": 4
18+
}

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "C/C++ Runner: Debug Session",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"args": [],
9+
"stopAtEntry": false,
10+
"externalConsole": true,
11+
"cwd": "d:/CODING/Codding/ADA",
12+
"program": "d:/CODING/Codding/ADA/build/Debug/outDebug",
13+
"MIMode": "gdb",
14+
"miDebuggerPath": "gdb",
15+
"setupCommands": [
16+
{
17+
"description": "Enable pretty-printing for gdb",
18+
"text": "-enable-pretty-printing",
19+
"ignoreFailures": true
20+
}
21+
]
22+
}
23+
]
24+
}

.vscode/settings.json

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,60 @@
11
{
2-
"java.dependency.packagePresentation": "hierarchical"
3-
}
2+
"java.dependency.packagePresentation": "hierarchical",
3+
"C_Cpp_Runner.cCompilerPath": "gcc",
4+
"C_Cpp_Runner.cppCompilerPath": "g++",
5+
"C_Cpp_Runner.debuggerPath": "gdb",
6+
"C_Cpp_Runner.cStandard": "",
7+
"C_Cpp_Runner.cppStandard": "",
8+
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
9+
"C_Cpp_Runner.useMsvc": false,
10+
"C_Cpp_Runner.warnings": [
11+
"-Wall",
12+
"-Wextra",
13+
"-Wpedantic",
14+
"-Wshadow",
15+
"-Wformat=2",
16+
"-Wcast-align",
17+
"-Wconversion",
18+
"-Wsign-conversion",
19+
"-Wnull-dereference"
20+
],
21+
"C_Cpp_Runner.msvcWarnings": [
22+
"/W4",
23+
"/permissive-",
24+
"/w14242",
25+
"/w14287",
26+
"/w14296",
27+
"/w14311",
28+
"/w14826",
29+
"/w44062",
30+
"/w44242",
31+
"/w14905",
32+
"/w14906",
33+
"/w14263",
34+
"/w44265",
35+
"/w14928"
36+
],
37+
"C_Cpp_Runner.enableWarnings": true,
38+
"C_Cpp_Runner.warningsAsError": false,
39+
"C_Cpp_Runner.compilerArgs": [],
40+
"C_Cpp_Runner.linkerArgs": [],
41+
"C_Cpp_Runner.includePaths": [],
42+
"C_Cpp_Runner.includeSearch": [
43+
"*",
44+
"**/*"
45+
],
46+
"C_Cpp_Runner.excludeSearch": [
47+
"**/build",
48+
"**/build/**",
49+
"**/.*",
50+
"**/.*/**",
51+
"**/.vscode",
52+
"**/.vscode/**"
53+
],
54+
"C_Cpp_Runner.useAddressSanitizer": false,
55+
"C_Cpp_Runner.useUndefinedSanitizer": false,
56+
"C_Cpp_Runner.useLeakSanitizer": false,
57+
"C_Cpp_Runner.showCompilationTime": false,
58+
"C_Cpp_Runner.useLinkTimeOptimization": false,
59+
"C_Cpp_Runner.msvcSecureNoWarnings": false
60+
}
1.59 KB
Binary file not shown.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
public class MergeSort {
2+
public static void mergeSort(int arr[], int lowerbound, int upperbound) {
3+
// Base Condition
4+
if (lowerbound >= upperbound)
5+
return;
6+
int mid = lowerbound + (upperbound - lowerbound) / 2;
7+
8+
mergeSort(arr, lowerbound, mid);
9+
mergeSort(arr, mid + 1, upperbound);
10+
merge(arr, lowerbound, mid, upperbound);
11+
}
12+
13+
public static void merge(int arr[], int lowerbound, int mid, int upperbound) {
14+
// Creating a temporary array
15+
int temp[] = new int[upperbound - lowerbound + 1];
16+
// Iterator for left part;
17+
int i = lowerbound;
18+
// Iterator for right part;
19+
int j = mid + 1;
20+
// Iterator for temp array;
21+
int k = 0;
22+
23+
while (i <= mid && j <= upperbound) {
24+
if (arr[i] <= arr[j]) {
25+
temp[k] = arr[i];
26+
i++;
27+
28+
} else {
29+
temp[k] = arr[j];
30+
j++;
31+
}
32+
k++;
33+
}
34+
35+
36+
// Copying the remaining element of the left half if any
37+
38+
while (i <= mid) {
39+
temp[k++] = arr[i++];
40+
}
41+
42+
// Copying the remaining element of the right half ,if any
43+
while (j <= upperbound) {
44+
temp[k++] = arr[j++];
45+
}
46+
// Copying temporary Array to Original Array
47+
for (k = 0, i = lowerbound; k < temp.length; k++, i++) {
48+
arr[i] = temp[k];
49+
50+
}
51+
52+
}
53+
// Time Complexity: O(nlogn);
54+
// Space Complexity: O(n)
55+
public static void printArr(int arr[]) {
56+
for (int num : arr) {
57+
System.out.print(num+" ");
58+
}
59+
System.out.println();
60+
}
61+
62+
public static void main(String[] args) {
63+
int arr[] = { 6, 3, 9, 5, 2, 8 };
64+
System.out.println("Array before sorting");
65+
printArr(arr);
66+
mergeSort(arr, 0, arr.length - 1);
67+
System.out.println("Array after sorting");
68+
printArr(arr);
69+
70+
}
71+
}

0 commit comments

Comments
 (0)