String-03. Bubble sort of string (20)

Source: Internet
Author: User

We already know the bubbling sort method of sorting n integers from small to large. This method is required to be used for string sequences, and for any given K (<n), the output is scanned after the K-pass intermediate result sequence.

Input format:

The input gives n and K (1<=k<n<=100) in line 1th, followed by n rows, each containing a non-empty string of less than 10 in length, consisting only of lowercase English letters.

Output format:

The output bubbling Sort method scans the intermediate result sequence after the K-pass, with each line containing a string.

Input Sample:

6 2bestcateastafreeday

Sample output:

Bestacatdayeastfree

#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include < Stdlib.h>using namespace::std;  int main () {   char a[100][12]; int n,k; scanf ("%d%d", &n,&k), GetChar (); for (int i=0;i<n;i++) {gets (A[i]);} int b; Char temp[12]; for (int i=0;i<k;i++) {for (int j=0;j<n-1;j++) {b=strcmp (a[j],a[j+1]);  if (b>0) {strcpy (temp,a[j]); strcpy (a[j],a[j+1]); strcpy (a[j+1],temp);}} } for (int i=0;i<n;i++) {puts (a[i]);}      return 0;}

  

String-03. Bubble sort of string (20)

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.