Java implementations of several large sorting algorithms (original)

Source: Internet
Author: User

Java implementation of several large sorting algorithms

Update in ...

Note:

    1. This class is accompanied by a random generation [min, max) range of non-repeating integers, if you crossing any better suggestions for this method, welcome to communicate.
    2. The ideas of each algorithm are written in this class of comments, but also convenient for you to verify the local environment.
    3. The currently completed sorting algorithms are: bubble sort, select sort, insert sort.
    4. Only the basic version of these sorting algorithms is provided for the time being, and the improvement of the algorithm is not considered.
    5. If you want to see more intuitively, welcome to a visual sort of website
Import static Org.junit.assert.*;import Java.util.arraylist;import Java.util.arrays;import Java.util.list;import Org.junit.test;public class Sorttest {/** * is used to store all integers in a given range */private static list<integer> Numlis        t = new arraylist<> (); /** * Returns an array of integers between randomly generated [min, max] @param min minimum * @param max Max (not included) * @return */public static in    Teger[] Generaterandomnum (int min, int max) {return generaterandomnum (min, Max, (Max-min)); }/** * Returns an array of integers between randomly generated [min, Max], number of num * @param min * @param max * @param number of random numbers to be generated by NUM * @re Turn */public static integer[] Generaterandomnum (int min, int max, int num) {Long start = System.currentti        Memillis ();        list<integer> result = new arraylist<> ();        for (int i = min; i < Max; i++) {numlist.add (i);        } for (int i = 0; i < num; i++) {Result.add (generateonenum (min, max));      }  Long end = System.currenttimemillis (); System.out.println ("generated random array range is: [" + min + "," + Max + "), Number:" + num + ", time consuming:" + (End-start)/1000.0 + "s.\n the array to be sorted as:            "+ result);    Return Result.toarray (New integer[]{}); }//reads any element in numlist private static int generateonenum (int min, int max) {return (int) numlist.remove ((int)    (Math.random () * numlist.size ())); }/** * Bubble sorting algorithm, time complexity is O (n^2) * @throws Exception */@Test public void Testbubblesort () throws Excepti        on {//Integer nums[] = {67, 1, 69, 43, 7, 56, 34, 111, 87, 62, 89, 90, 31, 99, 100};        integer[] Nums = generaterandomnum (1, 100, 10); The first-level for loop controls the number of sort trips. Reason:1 Digit->0 sequencing, 2 numbers->1 sorted, 3 digits->2 sort for (int i = 0; i < nums.length-1; i++) {//Second layer F The or loop controls the comparison process for this order (int j = 0; J < nums.length-1-I; j + +) {//22 comparison, if the back is smaller than the front, then the swap position, then each Row by line, and the last number is the largest number in the order (Nums[j] > Nums[j+ 1]) {int temp = nums[j];                    NUMS[J] = nums[j + 1];                Nums[j + 1] = temp;        }} System.out.println ("No." + (i + 1) + "The result of the sequencing is:" + arrays.tostring (nums));         }}/** * Select sort * @throws Exception */@Test public void Testselectionsort () throws Exception {        integer[] Nums = generaterandomnum (1, 100, 10);            for (int i = 0; i < nums.length-1; i++) {//is still the number of times the control is sorted int minindex = i; This layer for loop is to find the smallest number and exchange it with the first of all the unsorted numbers for (int j = i + 1; j < Nums.length; J + +) {if (nums[                J] < Nums[minindex]) {minindex = J; }} if (Minindex! =) {//swaps the smallest number in the trip sort and the first digit in the unsorted number int tmp = Nums[mini                Ndex];                Nums[minindex] = nums[i];            Nums[i] = tmp; } System.out.println ("The" + (i + 1) + "Order result is:" +Arrays.tostring (nums));     }}/** * Insert sort: To the number to be inserted, compared to the number that has been ordered, if the number is less than the previous number, then the previous number is moved backward one bit, if the * is greater than a certain number, then the number of inserts in that position * @throws Exception        */@Test public void Testinsertsort () throws Exception {integer[] nums = generaterandomnum (1, 100, 10);            for (int i = 1; i < nums.length; i++) {//First layer for loop resolve number of trips int j = i; int target = nums[i];//Gets the number to insert//Because when the number of insertions stops compared when the number of inserts is larger than the one already in the sequence, the while while (J > 0 && t            Arget < nums[j-1]) {Nums[j] = nums[j-1];//J Position place j-1 value, equivalent to the value of moving a j--;                        }//End While loop when Target > Nums[j-1] nums[j] = target;        System.out.println ("No." + (i + 1) + "The result of the sequencing is:" + arrays.tostring (nums));        }}/** * Quick sort * @throws Exception */@Test public void Testquicksort () throws Exception {    integer[] Nums = generaterandomnum (1, 100, 10); }/** * About bit operations: 1 ^ positive odd = positiveOdd-1 * @throws Exception */@Test public void testname () throws Exception {for (int i = 0; i < 101;        i++) {System.out.println (1 ^ (2*i + 1)); }    }    }

Java implementation of several large sorting algorithms (original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.