search
HomeJavaJavaBaseExamples of sorting methods in java

Examples of sorting methods in java

Nov 12, 2019 pm 03:36 PM
javaExample

Examples of sorting methods in java

Bubble sort

Features: low efficiency, simple implementation

Thoughts (from small to large): each Move the largest element in the sequence to be sorted to the end, and the remainder is the new sequence to be sorted. Repeat the above steps until all elements are sorted. This is just a type of bubble sorting, of course it can also be sorted from back to front.

public void bubbleSort(int array[]) {
        int t = 0;        
        for (int i = 0; i < array.length - 1; i++)            
        for (int j = 0; j < array.length - 1 - i; j++)                
        if (array[j] > array[j + 1]) {
                    t = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = t;
                }
    }

Selection sort

Features: low efficiency, easy to implement

Idea: select the smallest one from the sequence to be sorted in each pass The elements are placed at the end of the sorted sequence, and the remaining bits are left to be sorted. Repeat the above steps until the sorting is completed.

public void selectSort(int array[]) {
        int t = 0;        
        for (int i = 0; i < array.length - 1; i++)            
        for (int j = i + 1; j < array.length; j++)                
        if (array[i] > array[j]) {
                    t = array[i];
                    array[i] = array[j];
                    array[j] = t;
                }
    }

Insertion sort

Features: low efficiency, easy to implement

Idea: Divide the array into two parts, and divide the last part of the elements Compare it with the previous elements one by one. If the current element array[i] is small, replace it. Find a reasonable position to insert array[i]

public void insertionSort(int array[]) {
        int i, j, t = 0;        
        for (i = 1; i < array.length; i++) {
            t = array[i];            
            for (j = i - 1; j >= 0 && t < array[j]; j--)
                array[j + 1] = array[j];
            array[j + 1] = t;
        }
    }

Quick sort

Features: high efficiency, time complexity is nlogn.

Adopt the idea of ​​​​the divide and conquer method: first set an axis value pivot, and then use this axis value as the dividing basis to divide the sequence to be sorted into two parts larger than the pivot and smaller than the pivot, and then divide the divided subdivisions. The sequence is quick sorted until the subsequence contains one element.

public void quickSort(int array[], int low, int high) {// 传入low=0,high=array.length-1;
        int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。        
        if (low < high) {
            p_pos = low;
            pivot = array[p_pos];            
            for (i = low + 1; i <= high; i++)                
            if (array[i] > pivot) {
                    p_pos++;
                    t = array[p_pos];
                    array[p_pos] = array[i];
                    array[i] = t;
                }
            t = array[low];
            array[low] = array[p_pos];
            array[p_pos] = t;            // 分而治之
            quickSort(array, low, p_pos - 1);// 排序左半部分
            quickSort(array, p_pos + 1, high);// 排序右半部分
        }

Recommended tutorial: Java tutorial

The above is the detailed content of Examples of sorting methods in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools