Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String Sort #186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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! :)