আগামী ৩০ সেপ্টেম্বর থেকে Professional web Design and Development with HTML, CSS, JAVASCRIPT, PHP, MySQL, JQUERY and AJAX কোর্স এর ৯৪ তম ব্যাচ এবং ২৭ সেপ্টেম্বর থেকে Zend (PHP 7.1) Certified PHP Engineering (ZCPE) কোর্স এর ৪৪ তম ব্যাচ শুরু হতে যাচ্ছে। সীমিত সংখ্যক আসন বাকি আছে। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করার জন্য অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল নম্বর : ০১৭২২ ৮১ ৭৫ ৯১

Big Sorting, Algorithm problem

Consider an array of numeric strings, , where each string is a positive number with anywhere from to digits. Sort the array’s elements in non-decreasing (i.e., ascending) order of their real-world integer values and print each element of the sorted array on a new line.

Input Format

The first line contains an integer, , denoting the number of strings in .
Each of the subsequent lines contains a string of integers describing an element of the array.

Constraints

  • Each string is guaranteed to represent a positive integer without leading zeros.
  • The total number of digits across all strings in is between and (inclusive).

Output Format

Print each element of the sorted array on a new line.

Sample Input 0

6
31415926535897932384626433832795
1
3
10
3
5

Sample Output 0

1
3
3
5
10
31415926535897932384626433832795

Explanation 0

The initial array of strings is . When we order each string by the real-world integer value it represents, we get:

We then print each value on a new line, from smallest to largest.

Solution in PHP




$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$unsorted = array();
for($unsorted_i = 0; $unsorted_i < $n; $unsorted_i++){
fscanf($handle,"%s",$unsorted[]);
}

//make sure user define sorting comparison to sort big string
function comp($a, $b){
if( strlen($a) == strlen($b)){
return $a > $b;
}else{
return strlen($a) > strlen($b);
}
}
usort($unsorted, "comp");

foreach ($unsorted as $v) {
echo $v,"\n";
}


 

Hacker rank problem link

Click here

Leave a Reply

Your email address will not be published. Required fields are marked *