-
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
866e1e8
commit e6c36da
Showing
1 changed file
with
50 additions
and
0 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,50 @@ | ||
/* | ||
* This program takes an array from the user | ||
* sorts it using counting sort | ||
* Prints the sorted array | ||
* For more information about Counting Sort Algorithm: https://en.wikipedia.org/wiki/Counting_sort | ||
* | ||
* Coded by: Abdurrezak EFE | ||
* | ||
* */ | ||
#include <iostream> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
void counting_sort(int arr[], int k, int maxx) | ||
{ | ||
int counts[maxx+1]; | ||
for(int i=0;i<maxx+1;i++) //initializing count array | ||
counts[i] = 0; | ||
|
||
for(int i=0; i<k; i++) //we find out how many times a character occurs | ||
counts[arr[i]]++; | ||
|
||
for(int i=1;i<=maxx; i++) //we find out the actual positions of the elements | ||
counts[i] += counts[i-1]; | ||
|
||
//we copy our array | ||
int arr2[k]; | ||
for(int i=0;i<k;i++) | ||
arr2[i] = arr[i]; | ||
|
||
for(int i=0; i<k; i++) //change the order of the original array | ||
arr[counts[arr2[i]]-1] = arr2[i], counts[arr2[i]]--; | ||
} | ||
|
||
int main() | ||
{ | ||
int k; | ||
cout << "Enter the number of the integers you want to construct the array from: "; | ||
cin >> k; | ||
|
||
int arr[k]; | ||
int maxx = -1; | ||
for(int i=0;i<k;i++) | ||
cin >> arr[i], maxx = max(maxx,arr[i]); | ||
|
||
counting_sort(arr, k, maxx); //sorting | ||
|
||
for(int i=0;i<k;i++) //printing the array | ||
cout << arr[i] << " "; | ||
} |