【PTA】sort of English words

Please add image description
Personal business card:

blogger: Alcoholics ᝰ.
Personal profile: Indulge in wine, and strive for a future with a drink.
column: PTA exercises and analysis
introduce: Recorded the blogger's practice questions in pta

Please add image description

foreword

1 Introduction

"PTA programming experimental auxiliary teaching platform" is an auxiliary teaching platform for programming courses led by Zhejiang University, managed and operated by Hangzhou Baiteng Education Technology Co., Ltd., and jointly constructed by teachers in colleges and universities across the country. At present, 538 colleges and universities across the country have participated, and 3,152 teachers have jointly constructed 66,095 high-quality topics, and the number of registered students has reached 1.36 million; there are 14 fixed topic sets (covering C language, JAVA language, Python language, data structure, database system) and China University Computer Competition Question Bank) and 55 professional course topic sets (covering computer, electronics, literature, foreign language and Huawei certification). There are 10 types of questions including true and false questions, fill-in-the-blank questions, multiple-choice questions, multiple-choice questions, program fill-in-the-blank questions, function questions, programming questions, subjective questions, multi-file programming questions and SQL programming questions.

2. Advantages

  • Support 200,000 people online at the same time
  • Support multiple question types
  • Support a variety of judgment modes
  • Provide comprehensive guarantee for teaching quality
  • Instant question answering system

1. The topic

This question requires to write a program, input a number of English words, sort these words according to the length from small to large and then output. If the lengths are the same, the order of input is unchanged.

Input format:
The input is a number of English words, one per line, with # as the input end mark. The total number of English words is no more than 20, and English words are strings with length less than 10 consisting of lowercase English letters only.

Output format:
The output is the sorted result, with an extra space after each word.

Input sample:

blue
red
yellow
green
purple
#

Sample output:

red blue green yellow purple

Code Length Limit 16 KB
Time Limit 400 ms
Memory Limit 64 MB

2. Code

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String num;
        ArrayList<String> list = new ArrayList<>();
        int n = 0;
        while (true){
    
    
            num = sc.next();
            if (!num.equals("#")){
    
    
                list.add(num);
            }else {
    
    
                break;
            }
            n++;
        }
        String[] str = new String[list.size()];
        for (int i = 0; i < str.length; i++) {
    
    
            str[i] = list.get(i);
        }
        for (int i = 0; i < list.size()-1; i++) {
    
    
            for (int j = i + 1; j < list.size(); j++) {
    
    
                if (str[i].length() > str[j].length()){
    
    
                    num = str[i];
                    str[i] = str[j];
                    str[j] = num;
                }
            }
        }
        for (int i = 0; i < str.length; i++) {
    
    
            System.out.print(str[i] + " ");
        }
    }
}

3. Notes

There are three common sorting methods: bubble sort, insertion sort, and selection sort.

  1. Bubble Sort
    Compares adjacent elements. If the first is bigger than the second, swap the two of them.
    Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.
    Repeat the above steps for all elements except the last one.
    Keep repeating the above steps for fewer and fewer elements each time, until there are no pairs of numbers to compare.
  2. Insertion Sort
    Insertion sort means that in the elements to be sorted, assuming that the previous n-1 (where n>=2) numbers are already sorted, now insert the nth number into the previously sorted sequence, Then find a suitable position for yourself, so that the sequence of inserting the nth number is also in order. The process of inserting all elements according to this method until the entire sequence is sorted is called insertion sort
  3. Selection sort
    The first layer of the selection sort method selects the second-to-last element from the starting element, mainly by assigning the subscript of the outer loop to a temporary variable before entering the second-layer loop each time, and then In the second-level loop of , if it is found that there is an element smaller than the element at the minimum position, the subscript of the smaller element is assigned to the temporary variable. Finally, after the second-level loop exits, if the temporary variable changes , it means that there are elements smaller than the current outer loop position, and these two elements need to be exchanged

Guess you like

Origin blog.csdn.net/m0_65144570/article/details/127098680