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)