Arrange the array into the smallest number.

Source: Internet
Author: User

68. Arrange the array into the smallest number.
Question: enter an array of Positive Integers to join them into a number and output the smallest of all the numbers that can be discharged.
For example, if the input array is {32,321}, the minimum numbers 32132 that can be arranged are output.
Provide an algorithm to solve the problem and prove the algorithm.
Analysis: this is an interview question from Baidu in September June,
From this question, we can see that Baidu has a high requirement on algorithms.

 

 


[Cpp]
/*
Name:
Copyright:
Author:
Date: 25-06-11 :19
Description: The minimum number of arrays (arrays and algorithms ).
Question: enter an array of Positive Integers to join them into a number and output all the numbers that can be discharged.
The smallest one.
For example, if the input array is {32,321}, the minimum numbers 32132 that can be arranged are output.
Provide an algorithm to solve the problem and prove the algorithm.
*/
# Include <iostream>
# Include <iomanip>
Using namespace std;
# Define N 10 // There are N integers in total
Bool cmp_min (char * s1, char * s2)
// Algorithm guarantee, s1 and s2 are not empty strings
{
Const int slen1 = strlen (s1 );
Const int slen2 = strlen (s2 );
Char * tc = (char *) malloc (slen1 + slen2 + 1); // only open up one space
Int I;
For (I = 0; I <slen2; ++ I)
Tc [I] = s2 [I];
For (; I <slen1 + slen2; ++ I)
Tc [I] = s1 [i-slen2];
Tc [I] = '\ 0 ';
For (I = 0; I <slen1; ++ I)
{
If (s1 [I] = tc [I])
Continue;
Else
{
If (s1 [I]> tc [I])
Return false;
Else
Return true;
}
}
For (; I <slen1 + slen2; ++ I)
{
If (s2 [i-slen1] = tc [I])
Continue;
Else
{
If (s1 [i-slen1]> tc [I])
Return false;
Else
Return true;
}
}
Return false;
}
 
Int sort_partition (char * s [], int p, int r)
{
Char * x = s [r];
Int I = p-1;
Int j;
Char * tem;
For (j = p; j <r; ++ j)
{
If (cmp_min (s [j], x ))
{
Tem = s [++ I];
S [I] = s [j];
S [j] = tem;
}
}
Tem = s [++ I];
S [I] = x;
S [r] = tem;
Return I;
}
 
Void quick_sort (char * s [], int p, int r)
{
Int m;
If (p <r)
{
M = sort_partition (s, p, r );
Quick_sort (s, p m-1 );
Quick_sort (s, m + 1, r );
}

}
Int main ()
{
// Z has N integers in total
Int a [N] = {67,234,901 };
Char * s [N]; // pointer Array
For (int I = 0; I <N; ++ I)
{// Convert to N strings
S [I] = (char *) malloc (sizeof (char) * 12 );
// An integer expressed by 11
Itoa (a [I], s [I], 10 );
// Contains '\ 0'
}
For (int I = 0; I <N; I ++)
{
Cout <s [I] <"";
}
Quick_sort (s, 0, 9 );
Cout <endl;
For (int I = 0; I <N; I ++)
{
Cout <s [I] <"";
}
System ("pause ");
Return 0;
}

/*
Name:
Copyright:
Author:
Date: 25-06-11 :19
Description: The minimum number of arrays (arrays and algorithms ).
Question: enter an array of Positive Integers to join them into a number and output all the numbers that can be discharged.
The smallest one.
For example, if the input array is {32,321}, the minimum numbers 32132 that can be arranged are output.
Provide an algorithm to solve the problem and prove the algorithm.
*/
# Include <iostream>
# Include <iomanip>
Using namespace std;
# Define N 10 // There are N integers in total
Bool cmp_min (char * s1, char * s2)
// Algorithm guarantee, s1 and s2 are not empty strings
{
Const int slen1 = strlen (s1 );
Const int slen2 = strlen (s2 );
Char * tc = (char *) malloc (slen1 + slen2 + 1); // only open up one space
Int I;
For (I = 0; I <slen2; ++ I)
Tc [I] = s2 [I];
For (; I <slen1 + slen2; ++ I)
Tc [I] = s1 [i-slen2];
Tc [I] = '\ 0 ';
For (I = 0; I <slen1; ++ I)
{
If (s1 [I] = tc [I])
Continue;
Else
{
If (s1 [I]> tc [I])
Return false;
Else
Return true;
}
}
For (; I <slen1 + slen2; ++ I)
{
If (s2 [i-slen1] = tc [I])
Continue;
Else
{
If (s1 [i-slen1]> tc [I])
Return false;
Else
Return true;
}
}
Return false;
}

Int sort_partition (char * s [], int p, int r)
{
Char * x = s [r];
Int I = p-1;
Int j;
Char * tem;
For (j = p; j <r; ++ j)
{
If (cmp_min (s [j], x ))
{
Tem = s [++ I];
S [I] = s [j];
S [j] = tem;
}
}
Tem = s [++ I];
S [I] = x;
S [r] = tem;
Return I;
}

Void quick_sort (char * s [], int p, int r)
{
Int m;
If (p <r)
{
M = sort_partition (s, p, r );
Quick_sort (s, p m-1 );
Quick_sort (s, m + 1, r );
}

}
Int main ()
{
// Z has N integers in total
Int a [N] = {67,234,901 };
Char * s [N]; // pointer Array
For (int I = 0; I <N; ++ I)
{// Convert to N strings
S [I] = (char *) malloc (sizeof (char) * 12 );
// An integer expressed by 11
Itoa (a [I], s [I], 10 );
// Contains '\ 0'
}
For (int I = 0; I <N; I ++)
{
Cout <s [I] <"";
}
Quick_sort (s, 0, 9 );
Cout <endl;
For (int I = 0; I <N; I ++)
{
Cout <s [I] <"";
}
System ("pause ");
Return 0;
}
It is a fast sorting, and the comparison operation adopts the symbol overload method. This question is still quite interesting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.