Skip to content

Commit

Permalink
String Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
anish9696 committed Oct 31, 2020
1 parent 30c0258 commit 5272508
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
64 changes: 64 additions & 0 deletions C/stringsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <algorithm>
using namespace std;


int compare(string s1, string s2) {

int i = 0;

while (i < s1.length() && i < s2.length()) {

if (s1[i] > s2[i]) {

return 1;
} else if (s1[i] < s2[i]) {
return -1;
}
i++;

}

if (s1.length() > s2.length()) {
return -1;
} else {
return 1;
}

}
/*
void sortfunc(string arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (compareTo(arr[j], arr[j + 1]) > 0) {
string temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
*/
int main()
{

int n;
cin>>n;
string* str = new string[n];
cin.ignore();
for(int i=0;i<n;i++)
{
cin>>str[i];
}
sort(str,str+n, compare);
for(int i=0;i<n;i++)
{
cout<<str[i]<<endl;
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,11 @@ The Stooge sort is a recursive sorting algorithm. It is defined for ascending or
Merge sort involves recursively splitting the array into 2 parts, sorting and finally merging them. A variant of merge sort is called 3-way merge sort where instead of splitting the array into 2 parts we split it into 3 parts.
Merge sort recursively breaks down the arrays to subarrays of size half. Similarly, 3-way Merge sort breaks down the arrays to subarrays of size one third.


## String sort
String sort the sort the string the accordint to the dictionary order according to their size and in lexicographical order



## Contributing
To start contributing, check out [CONTRIBUTING.md](https://github.com/diptangsu/Sorting-Algorithms/blob/master/CONTRIBUTING.md). New contributors are always welcome to support this project. Check out issues labelled as `Hacktoberfest` if you are up for some grabs! :)

0 comments on commit 5272508

Please sign in to comment.