4.1 Exercises

1. Sort

Title Description

The number n of inputs and outputs sort.

Entry

Comprising a first line of input integer n (1 <= n <= 100). The next line comprises n integers.

Export

May be multiple sets of test data for each set of data, the n integers sorted output, has a space behind each number.
Each set of test data per line.

Sample input

5
5 4 3 1 2

Sample Output

1 2 3 4 5 


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] list = new int[n];
        for (int i = 0; i < n; i++) {
            list[i] = s.nextInt();
        }
        pop(list);
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
    }

    static void selectSort(int[] list) {
        for (int i = 0; i < list.length; i++) {
            int k = i;
            for (int j = i; j < list.length; j++) {
                if (list[i] < list[k]) {
                    k = j;
                }
            }
            int temp = list[i];
            list[i] = list[k];
            list[k] = temp;
        }
    }

    static void pop(int[] list) {
        int temp = 0;
        for (int i = 0; i < list.length; i++) {
            boolean isSorted = true;
            for (int j = 0; j < list.length-1-i; j++) {
                if (list[j] > list[j + 1]) {
                    temp = list[j];
                    list[j] = list[j+1];
                    list[j+1] = temp;
                    isSorted = false ; 
                } 
            } 
            Ow (isSorted)
                 break ; 
        } 
    } 
}

 



Guess you like

Origin www.cnblogs.com/xiaolan-/p/11813541.html