-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
107b84c
commit 14d4105
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
void mergeFunction(int arr[],int low,int mid,int high){ | ||
|
||
|
||
int n1 = mid-low+1; | ||
int n2 = high-mid; | ||
|
||
|
||
|
||
|
||
int left [n1],right[n2]; | ||
|
||
//Store the elements before middle | ||
for(int i=0;i<n1;i++){ | ||
|
||
left[i] = arr[low+i]; | ||
|
||
} | ||
//Store the elements after middle | ||
for(int i=0;i<n2;i++){ | ||
right[i] = arr[mid+i+1]; | ||
} | ||
|
||
//Now next part------> | ||
int i=0,j=0,k=low; | ||
|
||
|
||
while (i<n1 && j<n2) | ||
{ | ||
if(left[i]<=right[j]){ | ||
arr[k] = left[i]; | ||
i++; | ||
k++; | ||
}else{ | ||
arr[k] = right[j]; | ||
k++; | ||
j++; | ||
} | ||
} | ||
//for the remianing elements of the array | ||
while(i<n1){ | ||
arr[k] = left[i]; | ||
i++; | ||
k++; | ||
} | ||
while(j<n2){ | ||
arr[k] = right[j]; | ||
j++; | ||
k++; | ||
} | ||
|
||
} | ||
|
||
//Merge Sort | ||
void mergeSort(int arr[],int l,int r){ | ||
|
||
if(r>l){ | ||
int m = l+(r-l)/2; | ||
|
||
mergeSort(arr,l,m); | ||
mergeSort(arr,m+1,r); | ||
mergeFunction(arr,l,m,r); | ||
} | ||
} | ||
|
||
int main(){ | ||
|
||
|
||
|
||
int n; | ||
cin>>n; | ||
cout<<"Enter the number of elements "<<endl; | ||
|
||
int arr[n]; | ||
for(int i=0; i<n; i++){ | ||
cin>>arr[i]; | ||
} | ||
|
||
|
||
cout<<"Elements before Sorting"<<endl; | ||
for(int i=0; i<n; i++){ | ||
cout<<arr[i]<<" "; | ||
} | ||
|
||
cout<<"Elements after Sorting"<<endl; | ||
|
||
mergeSort(arr,0,n-1); | ||
|
||
for(int i=0; i<n; i++){ | ||
cout<<arr[i]<<" "; | ||
} | ||
|
||
|
||
} | ||
//Time complexity theta(log n); | ||
//Space complexity theta(logn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
r-l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Here in this Repo u can access all sorting Algorithms. | ||
! This is written in java Not in C++ | ||
Soon i can add all the sorting algorithms...🐑 | ||
|
||
|