Poj1007 qsort quick sorting

Source: Internet
Author: User

This question is relatively simple, but I learned how to use the built-in qsort function in c ++! First, we will briefly introduce the qsort function. 1. It is fast sorting, so it is unstable. (Instability means that Zhang San and Li Si both scored 90, while Zhang San scored 90 in the first place. After sorting, they may be 90 in the first place.) 2. The header file must be included: cstdlib 3. prototype: void qsort (void * base, int nelem, int width, int (* fcmp) (const void *, const void *); 4. parameter: the first address of the array, the number of elements in the array, and the space occupied by each element. The pointer to the function indicates a string, the number of letters on the right of the string is smaller than that on the first letter of the string until the value of the string is obtained through the whole sequence. For example, "BAAED", B is greater than A (2) On the right and E is greater than D, so the value of the last line is 3. Then input several strings and output them in descending order of values. If the values are the same, they are output in the input order. In this case, you may think that the value must be output in the input order at the same time, so the quick sorting is definitely not feasible because it is unstable. This is a clever place for this question. I multiply the value of the string by 1000 and then add this string to the I of the first few inputs, the num [] number to be compared during sorting. Because the topic specifies that a maximum of 100 strings can be entered, the num value must be large. Even if the value is the same, the I value of the first input string must be small, so its num should be small. In this way, both the string and its value should be bound, and the size of the same number should be avoided. So there is no problem in the quick sorting!

/**   *poj1007   *@author monkeyduck   *@2013.9.21   */  #include<iostream>  #include<cstdlib>  using namespace std;  char str[110][55];  int num[100];  int cmp(const void* a,const void* b)  {      return *(int*)a-*(int*)b;  }  int main()  {      int n,m;      cin>>n>>m;      for (int i=0;i<m;i++)      {          cin>>str[i];          int count=0;          for (int j=0;j<n-1;j++)          {              for (int k=j+1;k<n;k++)              {                  if (str[i][j]>str[i][k]) count++;              }          }          num[i]=count*1000+i;      }      qsort(num,m,sizeof(num[0]),cmp);      for (int q=0;q<m;q++)      {          cout<<str[num[q]%1000]<<endl;      }      return 0;  }  

 


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.