প্রদত্ত স্ট্রিংগুলির একটি তালিকা আলফানিউমেরিক ক্রম বা অভিধান ক্রম অনুসারে সাজানো হয়েছে৷ এই শব্দগুলির জন্য পছন্দ করুন:Apple, Book, Aim, এগুলি Aim, Apple, Book হিসাবে সাজানো হবে৷ যদি কিছু সংখ্যা থাকে তবে সেগুলিকে বর্ণানুক্রমিক স্ট্রিংগুলির আগে স্থাপন করা যেতে পারে৷
ইনপুট এবং আউটপুট
Input: A list of strings: Ball Apple Data Area 517 April Man 506 Output: Strings after sort: 506 517 Apple April Area Ball Data Man
অ্যালগরিদম
sortStr(strArr, n)
ইনপুট: সমস্ত স্ট্রিংয়ের তালিকা, উপাদানের সংখ্যা।
আউটপুট - আলফানিউমেরিক সাজানো ক্রমে স্ট্রিং।
Begin for round := 1 to n-1, do for i := 0 to n-round, do res := compare str[i] and str[i+1] //either +ve, or –ve or 0 if res > 0, then swap str[i] and str[i+1] done done End
উদাহরণ
#include<iostream>
#define N 8
using namespace std;
void display(int n, string str[]) {
for(int i = 0; i<n; i++)
cout << str[i] << " "; //print the string from array
cout << endl;
}
void sortStr(int n, string str[]) {
int i, round, res;
for(round = 1; round<n; round++)
for(i = 0; i<n-round; i++) {
res = str[i].compare(str[i+1]);
if(res > 0)
swap(str[i], str[i+1]);//swap strings
}
}
main() {
string str[N] = {"Ball", "Apple", "Data", "Area", "517", "April", "Man", "506"};
cout << "Strings before sort:"<< endl;
display(N, str);
sortStr(N, str);
cout << "Strings after sort:"<<endl;
display(N, str);
} আউটপুট
Strings before sort: Ball Apple Data Area 517 April Man 506 Strings after sort: 506 517 Apple April Area Ball Data Man