Java 8 Large sorting algorithm

Source: Internet
Author: User



Direct Insert Sort

Insert sort directly    int[] Insertsort (int[] a) {for        (int i = 1; i < a.length; i++) {            int j = i-1;            for (; J >= 0 && A[j] > A[i]; j--) {                a[j + 1] = a[j];//move Backward            }            a[j + 1] = A[i];        }        return A;    }


Hill Sort
Hill sort public    int[] Shellsort (int[] a) {        int n = a.length;        int gap = n;//array number while        (Gap > 0) {            gap = gap/2;//reduces the array            by half for (int x = 0; x < gap; + +) {//inserts each group of numbers directly into the row Order                //Direct Insert sort for                (int i = x + gap; i < n; i + = gap) {                    int j = i-gap;                    for (; J >= 0 && A[j] > A[i]; J-= Gap) {                        a[j + gap] = A[j];                    }                    A[j + gap] = A[i];}}        }        return A;    }


Simple Selection Sorting
Simple Select Sort public    int[] Simplesort (int[] a) {        int n = a.length;        for (int i = 0, i < n; i++) {for            (int j = i + 1; j <= N; j + +) {                if (A[j] < A[i]) {                    int temp = a[i];< C6/>a[i] = a[j];                    A[J] = temp;}}        }        return A;    }



Heap Sort
 Heap sort public void heapsort (int[] a) {System.out.println ("start sort");        int arraylength = A.length; Loop build heap, each time put the maximum value at the end, do not participate in the next build heap for (int i = 0; i < arrayLength-1; i++) {//Jian Yu buildmaxheap (            A, arrayLength-1-i);            Swap heap top and last element swap (a, 0, arrayLength-1-i);        System.out.println (Arrays.tostring (a));        }} private void Swap (int[] data, int i, int j) {int tmp = Data[i];        Data[i] = Data[j];    DATA[J] = tmp; }//to the data array from 0 to LastIndex build a large top heap private void Buildmaxheap (int[] data, int lastIndex) {//From the LastIndex node (the last node) of the parent section            Point start, (lastIndex-1)/2 is the number of nodes for (int i = (lastIndex-1)/2; I >= 0; i--) {//k Saves the node being judged            int k = i;  If the child node of the current K node is present, the largest number in the heap is placed on the top while (K * 2 + 1 <= lastIndex) {//k The index of the left child node of the node int                Biggerindex = 2 * k + 1; If Biggerindex is less than lastindex, that is, biggerindex+1 represents the KThe right child node of the node exists if (Biggerindex < LastIndex) {//If the value of the right child node is larger if (data[big Gerindex] < Data[biggerindex + 1] {//biggerindex always records the index of the larger child nodes Biggerinde                    x + +;                    }}//If the value of the K-node is less than the value of its larger child node if (Data[k] < Data[biggerindex]) {                    Swap them swap (data, k, biggerindex);                The Biggerindex is given a K, starting the next loop of the while loop, and re-guaranteeing that the value of the K-node is greater than the value of its left and right child nodes k = Biggerindex;                } else {break; }            }        }    }



Bubble Sort
Bubble sort public    int[] Bubblesort (int[] a) {        int temp = 0;        for (int i = 0, i < a.length-1; i++) {for            (int j = 0; J < a.length-1-I; + j) {                if (A[j] > a[j + 1] ) {                    temp = a[j];                    A[J] = a[j + 1];                    A[j + 1] = temp;}}        }        return A;    }



Quick Sort
 Quick sort public void QuickSort () {quick (a);        for (int i = 0; i < a.length; i++) {System.out.println (a[i]);    }} public int getmiddle (int[] list, int. Low, int.) {int tmp = List[low];                The first of the array is used as a medium while (low < high) {while (Low < high && List[high] >= tmp) {            high--;   } List[low] = List[high];            A record smaller than the middle axis is moved to the low side while (lower < high && List[low] <= tmp) {low++;   } List[high] = List[low];              A record larger than the middle axis moved to the high end} List[low] = tmp;                   The middle axis is recorded to the tail return low; Returns the position of the middle axis} public void _quicksort (int[] list, int. Low, int.) {if (Low < high) {int MIDDL  E = Getmiddle (list, low, high);       Divides the list array into _quicksort (list, low, middle-1);       Recursive ordering of low-word tables _quicksort (list, middle + 1, high);   Recursive ordering of high-character tables}} public void Quick (int[] A2) {if (A2.length > 0) {//See if the array is empty _quicksort (A2, 0, A2.length-1); }    }



Merge Sort
Merge sort public void MergeSort (int[] arr) {int[] temp = new Int[arr.length];    Internalmergesort (arr, temp, 0, arr.length-1); } private void Internalmergesort (int[] A, int[] b, int left, int. right) {//When left==right, there is no need to divide the if (            Left < right) {int middle = (left + right)/2; Internalmergesort (A, B, left, middle);//Left Dial hand array internalmergesort (A, B, middle + 1, right);  Sortedarray (A, B, left, middle, right);//Merge two sub-arrays}}//Merge two ordered sub-sequences arr[left, ..., middle] and arr[middle+1, ...,    Right],temp is an auxiliary array.        private void Mergesortedarray (int arr[], int temp[], int left, int middle, int. right) {int i = left;        Int J = middle + 1;        int k = 0;                Remove the smallest of the two arrays into the temporary array while (I <= Middle && J <= right) {if (Arr[i] <= arr[j]) {            temp[k++] = arr[i++];            } else {temp[k++] = arr[j++];  }        }      Copy the remaining data from the left array to the temporary array while (I <= middle) {temp[k++] = arr[i++];        }//Copy the remaining data from the right array to the temporary array while (J <= R) {temp[k++] = arr[j++];        }//Copy the data back to the original array for (i = 0; i < K; ++i) {arr[left + i] = temp[i]; }    }



Base Sort
Radix sort public void radixsort () {sort (a);        for (int i = 0; i < a.length; i++) {System.out.println (a[i]);        }} public void sort (int[] array) {//First determine the number of times to sort;        int max = array[0];            for (int i = 1; i < Array.Length; i++) {if (Array[i] > max) {max = array[i];        }} int time = 0;        Determine the number of digits;            while (Max > 0) {max/= 10;        time++;        }//Set up 10 queues;        list<arraylist> queue = new arraylist<arraylist> ();            for (int i = 0; i < i++) {arraylist<integer> queue1 = new arraylist<integer> ();        Queue.add (queue1);        }//For time allocation and collection;            for (int i = 0; i < time; i++) {//allocate array elements;                for (int j = 0; J < Array.Length; J + +) {//Get the number of time+1 digits;        int x = array[j]% (int) Math.pow (ten, i + 1)/(int) Math.pow (ten, I);        arraylist<integer> queue2 = queue.get (x);                Queue2.add (Array[j]);            Queue.set (x, queue2);            } int count = 0;//element counter;            Collect queue elements; for (int k = 0, K < k++) {while (Queue.get (k). Size () > 0) {arraylist<inte                    ger> Queue3 = Queue.get (k);                    Array[count] = queue3.get (0);                    Queue3.remove (0);                count++; }            }        }    }




Original link: Java Common sorting algorithm/programmer must master the 8 big sorting algorithm

Java 8 Large sorting algorithm

Related Article

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.