-
Notifications
You must be signed in to change notification settings - Fork 108
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
67175bb
commit 615f036
Showing
2 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
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,41 @@ | ||
#include<iostream> | ||
#include<vector> | ||
using namespace std; | ||
|
||
void pigeonhole_sort(int arr[],int n) | ||
{ | ||
int minm=arr[0],maxm=arr[0]; | ||
for(int i=1;i<n;i++) | ||
{ | ||
if(arr[i]<minm) | ||
minm=arr[i]; | ||
if(arr[i]>maxm) | ||
maxm=arr[i]; | ||
} | ||
int range=maxm-minm+1; | ||
vector<int>holes[range]; | ||
for(int i=0;i<n;i++) | ||
holes[arr[i]-minm].push_back(arr[i]); | ||
int index=0; | ||
for(int i=0;i<range;i++) | ||
{ | ||
for(auto it:holes[i]) | ||
arr[index++]=it; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cout<<"Enter the size of array : "; | ||
cin>>n; | ||
int arr[n]; | ||
cout<<"Enter elements of the array : "; | ||
for(int i=0;i<n;i++) | ||
cin>>arr[i]; | ||
pigeonhole_sort(arr,n); | ||
cout<<"Sorted array is : "<<endl; | ||
for(int i=0;i<n;i++) | ||
cout<<arr[i]<<" "; | ||
return 0; | ||
} |
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