Skip to content

Commit

Permalink
Merge pull request AdarshAddee#161 from atinder11/main
Browse files Browse the repository at this point in the history
Update the CONTRIBUTORS.md and add the counting sort.cpp file
  • Loading branch information
AdarshAddee committed Oct 4, 2022
2 parents 460adeb + 6a12fd5 commit 7e104ad
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
65 changes: 65 additions & 0 deletions C++/Counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// C++ Program for counting sort
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define RANGE 200

// The main function that sort
// the given string arr[] in
// alphabetical order
void countSort(char arr[])
{
// The output character array
// that will have sorted arr
char output[strlen(arr)];

// Create a count array to store count of individual
// characters and initialize count array as 0
int count[RANGE + 1], i;
memset(count, 0, sizeof(count));

// Store count of each character
for (i = 0; arr[i]; ++i)
++count[arr[i]];

// Change count[i] so that count[i] now contains actual
// position of this character in output array
for (i = 1; i <= RANGE; ++i)
count[i] += count[i - 1];

// Build the output character array
for (i = 0; arr[i]; ++i) {
output[count[arr[i]] - 1] = arr[i];
--count[arr[i]];
}

/*
For Stable algorithm
for (i = sizeof(arr)-1; i>=0; --i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
For Logic : See implementation
*/

// Copy the output array to arr, so that arr now
// contains sorted characters
for (i = 0; arr[i]; ++i)
arr[i] = output[i];
}

// Driver code
int main()
{
char arr[] = "countingsortincpp";

countSort(arr);

cout << "Sorted character array is " << arr;
return 0;
}
//github : atinder11


3 changes: 3 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
| Rahul Jangle | <a href="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/Rronny01/">Ronny</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Anurag Vats | <a href="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/AnuragVats007">Anurag Vats</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Daksh Kesarwani | <a href="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/InnocentDaksh63">Daksh kesaarwani</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Atinder Kumar | <a href="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/atinder11">Atinder Kumar</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Daksh Kesarwani | <a href="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/InnocentDaksh63">Daksh kesaarwani</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Shobhit Tiwari | <a href="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/shobhitt8">Shobhit Tiwari</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Daksh Kesarwani | <a href="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/InnocentDaksh63">Daksh kesaarwani</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Aritroo Chowdhury | <a href="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/aritroo">Aritroo Chowdhury</a> | <a href="mailto:[email protected]">E-Mail</a> |
Expand Down Expand Up @@ -64,6 +66,7 @@




<br>
<h1>
Happy Hacking ❤💻
Expand Down

0 comments on commit 7e104ad

Please sign in to comment.