Eight basic sort (direct insert sort, hill sort, bubble sort, quick sort, merge sort, simple select sort)

Source: Internet
Author: User

Package Com.wang.sort;import Java.util.arrays;public class Sort {/** * 1. Direct insertion Sort * Idea: The current number is compared with the number of rows already in the previous order, and inserted into the appropriate position * @param Arra */public void Simplesort (int[] arra) {for (int i = 1; i < arra.length; i++) {int temp = Arra[i];int J = i-1;for (; J >= 0 && arra[j] > temp; j--) {arra[j + 1] = arra[j];} ARRA[J+1] = temp;}} /** * 2. Bubble sort * Thought: Compare two numbers adjacent, find the largest number to sink * @param args */public void Bubblesort (int[] arra) {int temp;for (int i = 0; I &l T Arra.length-1; i++) {for (int j = 0; J < arra.length-1-I; j + +) {if (Arra[j] > arra[j+1]) {temp = Arra[j];arra[j] = Arra[j+1];ar RA[J+1] = temp;}}}} /** * Find Middle Value * @param arra * @param low * @param hight */public static int getmiddle (int[] arra, int. low, int hight) {int Te MP = Arra[low];while (Low < hight) {when (Low < hight && Arra[hight] > Temp) {hight--;} Arra[low] = arra[hight];while (Low < hight && Arra[low] <= temp) {low++;} Arra[hight] = Arra[low];} Arra[low] = Temp;return low;} /** * 3. Quick Sort * thinkThink: Find an intermediate value, will be larger than the median to the right of the middle value, small put on the left, according to the recursive * @param arra * @param low * @param hight */public static void Qicksort (int[] Arra, int low, int. hight) {if (Low > Hight) {return;} int middle = getmiddle (Arra, Low, hight), Qicksort (ARRA, Low, middle-1), Qicksort (ARRA, Middle + 1, hight);}  /** * Merge Sort * @param arra * @param left * @param center * @param right */public static void merge (int[] Arra, int. left, int Center, int right) {int[] Temparr = new Int[right-left + 1];int temp = left;int mid = center + 1;int third = 0;while (te MP <= Center && Mid <= right) {if (Arra[temp] < Arra[mid]) {temparr[third++] = arra[temp++];} else {Tempa rr[third++] = arra[mid++];}} while (temp <= center) {temparr[third++] = arra[temp++];} While (mid <= right) {temparr[third++] = arra[mid++];} for (int i = 0; i < Temparr.length, i++) {arra[i + left] = Temparr[i];}} /** * 4. Merge sort * @param arra * @param left * @param right */public void Mergingsort (int. arra[], int left, int. right) {if (Le FT < Right{int center = (left + right)/2;mergingsort (ARRA, left, center), Mergingsort (ARRA, center + 1, right), merge (Arra, left, Center, right);}}      /** * 5. Hill sort * @param args */public static void Shellsort (int[] array) {int i;      Int J;      int temp;      int gap = 1;      int len = Array.Length;     while (Gap < LEN/3) {gap = Gap * 3 + 1;              } for (Gap > 0, Gap/= 3) {for (i = gap; i < Len; i++) {temp = Array[i];              for (j = i-gap; J >= 0 && array[j] > temp; J-= Gap) {array[j + gap] = array[j];          } Array[j + gap] = temp;  }} System.out.println (Arrays.tostring (array) + "Shellsort"); }/** * 5. Hill sort * @param arra */public void Shelllsort (int[] arra) {int J = 0;int temp = 0;for (int step = ARRA.LENGTH/2; Step > 0; Step/= 2) {for (int i = step; i < arra.length; i++) {temp = Arra[i];for (j = i-step; J >= 0 && Arra[j] &G T Temp J-=Step) {arra[j + step] = Arra[j];} Arra[j + step] = temp;}}} /** * 6. Simple selection Sort * Idea: find the smallest place in the first place, the second small one in the second place, and so on * @param args */public void Selectsort (int[] arra) {int position = 0;for (in t i = 0; i < arra.length; i++) {Int J = i + 1;position = i;int temp = arra[i];for (; j < Arra.length; J + +) {if (Arra[j] < temp) {temp = arra[ J];p osition = j;}} Arra[position] = arra[i];arra[i] = temp;}} /** * 7. Base sort * @param args *//** * 8. Heap sort * @param args */public static void main (string[] args) {sort sort = new sort (); in  T[] Arra = {1,2,3,9,6,8,5,10};//sort.simplesort (ARRA);  Simple sort//sort.bubblesort (ARRA);  Bubble sort//sort.qicksort (arra, 0, arra.length-1); Quick Sort//sort.mergingsort (arra, 0, Arra.length-1), Sort.shelllsort (ARRA), for (int i = 0; i < arra.length; i++) {System. Out.println (Arra[i]);}}


  

Eight basic sort (direct insert sort, hill sort, bubble sort, quick sort, merge sort, simple select sort)

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.