Sorting algorithm clock in

Keywords: Algorithm

Quick row

Based on the idea of divide and conquer

Template questions:

Give you a sequence of integers of length n.

Please use quick sort to sort this sequence from small to large.

And output the ordered sequence in order.

Input format

The input consists of two lines. The first line contains the integer n.

The second row contains n integers (all integers are in the range of 1 ∼ 109), representing the whole sequence.

Output format

The output is in one line, including n integers, representing the ordered sequence.

Data range

1≤n≤100000

Input example:

5
3 1 2 4 5

Output example:

1 2 3 4 5

Input example:

2
2 1

Output example:

1 2

answer:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void quick_sort(int[] a, int begin, int end){
        if(begin>=end)    return;
        int x = a[begin], t = 0;
        int i = begin-1, j = end+1;
        while (i<j){
            while (a[++i]<x);
            while (a[--j]>x);
            if(i<j){
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
        quick_sort(a,begin,j);
        quick_sort(a,j+1,end);
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.valueOf(bufferedReader.readLine());
        int[] a = new int[n];
        String[] str = bufferedReader.readLine().split(" ");
        for (int i=0;i<n;i++){
            a[i] = Integer.parseInt(str[i]);
        }
        quick_sort(a,0,n-1);
        for (int i=0;i<n;i++){
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }
}

Example: number k:

Given an integer sequence of length n and an integer k, please use the fast selection algorithm to find the k-th number after sorting the sequence from small to large.

Input format

The first line contains two integers n and k.

The second row contains n integers (all integers are in the range of 1 ∼ 109), representing the integer sequence.

Output format

Output an integer representing the k-th decimal of the sequence.

Data range

1≤n≤100000,
1≤k≤n

Input example:

5 3
2 4 1 5 3

Output example:

3

answer:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void quick_sort(int[] a, int begin, int end){
        if(begin>=end) return;
        int x = a[begin], t = 0;
        int i = begin-1, j = end+1;
        while (i<j){
            while (a[++i]<x);
            while (a[--j]>x);
            if(i<j){
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
        quick_sort(a,begin,j);
        quick_sort(a,j+1,end);
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.valueOf(str[0]);
        int k = Integer.valueOf(str[1]);
        int[] a = new int[n];
        str = bufferedReader.readLine().split(" ");
        for (int i=0;i<n;i++){
            a[i] = Integer.parseInt(str[i]);
        }
        quick_sort(a,0,n-1);
        System.out.println(a[k-1]);
    }
}

Merge sort

Based on the idea of divide and conquer

Template questions:

Give you a sequence of integers of length n.

Please use merge sort to sort this sequence from small to large.

And output the ordered sequence in order.

Input format

The input consists of two lines. The first line contains the integer n.

The second row contains n integers (all integers are in the range of 1 ∼ 109), representing the whole sequence.

Output format

The output is in one line, including n integers, representing the ordered sequence.

Data range

1≤n≤100000

Input example:

5
3 1 2 4 5

Output example:

1 2 3 4 5

answer:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void mergeSort(int[] a, int begin, int end){
        if(begin>=end) return;
        int mid = (begin+end)/2;
        mergeSort(a,begin,mid);
        mergeSort(a,mid+1,end);
        merge(a,begin,end,mid);
    }

    public static void merge(int[] a, int i, int j, int k){
        int n = j-i+1;
        int[] c = new int[n];
        int x = i, y = k+1, t = 0;
        while (x<=k&&y<=j){
            if(a[x]<a[y])    c[t++] = a[x++];
            else    c[t++] = a[y++];
        }
        while (x<=k)     c[t++] = a[x++];
        while (y<=j)    c[t++] = a[y++];
        for (t=0;t<n;t++){
            a[i++] = c[t];
        }
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.valueOf(str[0]);
        int[] a = new int[n];
        str = bufferedReader.readLine().split(" ");
        for (int i=0;i<n;i++){
            a[i] = Integer.parseInt(str[i]);
        }
        mergeSort(a,0,n-1);
        for (int i=0;i<n;i++){
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }
}

Example: number of pairs in reverse order:

Given an integer sequence of length n, please calculate the number of reverse pairs in the sequence.

The definition of reverse order pair is as follows: for the ith and jth elements of the sequence, if I < j and a [i] > a [j], it is a reverse order pair; Otherwise not.

Input format

The first row contains the integer n, which represents the length of the sequence.

The second row contains n integers, representing the entire sequence.

Output format

Output an integer indicating the number of pairs in reverse order.

Data range

1≤n≤100000,
Value range of elements in the sequence [1109].

Input example:

6
2 3 4 5 6 1

Output example:

5

answer:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    static long res = 0;

    public static void mergeSort(int[] a, int begin, int end) {
        if (begin >= end) return;
        int mid = (begin + end) / 2;
        mergeSort(a, begin, mid);
        mergeSort(a, mid + 1, end);
        merge(a, begin, end, mid);
    }

    public static void merge(int[] a, int i, int j, int k) {
        int n = j - i + 1;
        int[] c = new int[n];
        int x = i, y = k + 1, t = 0;
        while (x <= k && y <= j) {
            if (a[x] <= a[y]) c[t++] = a[x++];
            else {
                c[t++] = a[y++];
                res += (k - x + 1);
            }
        }
        while (x <= k) c[t++] = a[x++];
        while (y <= j) c[t++] = a[y++];
        for (t = 0; t < n; t++) {
            a[i++] = c[t];
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.valueOf(str[0]);
        int[] a = new int[n];
        str = bufferedReader.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(str[i]);
        }
        mergeSort(a, 0, n - 1);
        System.out.println(res);
    }
}

Posted by corsc on Sun, 24 Oct 2021 08:26:45 -0700