A summary of common sorting algorithms

Source: Internet
Author: User
Tags getmessage

Before the time was asked to sort algorithm, today to summarize, bubble, select, fast, merge, insert the five sorting algorithm.

 1. Select sort

Select Sort is the overall comparison, each time you choose the value you most need, such as the following code, the first to select the smallest value, from small to small to large order, outside the loop once, the internal loop record the smallest coordinates, only one exchange, it is a stable sorting algorithm, time complexity of O (N2), the core code is as follows:

Int[] Numberarray = {1, 5,};int arraylength = Numberarray.length;int temp = 0;for (int i = 0; i < ARRA Ylength-1; i++) {int k = i;for (int j = i + 1; j < Arraylength; J + +) {if (Numberarray[k] > Numberarray[j]) {k = j;}} if (i! = k) {temp = Numberarray[i];numberarray[i] = numberarray[k];numberarray[k] = temp;}} System.out.println (arrays.tostring (Numberarray));

2. Bubble sort

 The core idea of bubble sorting is to compare the adjacent elements, move the large number back, the outer loop once, the internal loop may occur many times exchange, it is a stable algorithm, the time complexity is O (N2), the core code is as follows:

int[] Numberarray = {22, 32, 12, 1, 5, 66 }; intArraylength =numberarray.length; inttemp = 0;  for(inti = 0; i < arraylength-1; i++) {             for(intj = i + 1; J < Arraylength; J + +) {                if(Numberarray[i] >Numberarray[j]) {Temp=Numberarray[i]; Numberarray[i]=Numberarray[j]; NUMBERARRAY[J]=temp; }}} System.out.println (Arrays.tostring (Numberarray));

 3. Insert Sort

 Insert sort By default to a correct location, and then look for the insertion position, a bit like when we finished the landlord, will be placed next to the size of the card, it is a stable sorting algorithm, time complexity O (N2), the core code is as follows:

Int[] Numberarray = {1, 5,};for (int i = 1; i < numberarray.length; i++) {////regards J as a cursor int j = I;int Ta Rget = Numberarray[i];while (J > 0 && Target < numberarray[j-1]) {////description has greater, move backward numberarray[j] = Numberar ray[j-1];j--;} NUMBERARRAY[J] = target;} System.out.println (arrays.tostring (Numberarray));

4. Quick Sort

The core of fast sorting is bubble + binary + recursive division, the actual application is the best sorting algorithm, but it is an unstable sorting algorithm, the average time complexity is O (NLGN), the worst case is O (N2), and the difference between the bubble algorithm is to find both large and small number, the core code is as follows:

  

public static void Main (string[] args) {try {int[] Numberarray = new int[] {4, 3, 6, 1, 2, 5};recursionsort (Numberarray, 0, Numberarray.length-1); System.out.println (arrays.tostring (Numberarray));} catch (Exception ex) {System.out.println (Ex.getmessage ());}} private static void Recursionsort (int[] numberarray, int low, int.) {if (Low < high) {int basevalueindex = Partitio Ngroup (Numberarray, Low, high),////left interval recursive recursionsort (Numberarray, Low, basevalueIndex-1),////right interval recursive recursionsort ( Numberarray, Basevalueindex + 1, high);}} private static int Partitiongroup (int[] numberarray, int low, int.) {int basevalue = Numberarray[low];while (Low < High) {////first right-to-left while (Low < high && Numberarray[high] >= basevalue) {--high;} The interchange is smaller than the reference value to the left Numberarray[low] = Numberarray[high];while (Low < high && Numberarray[low] <= basevalue) {+ +low;} The interaction is larger than the benchmark value to the right Numberarray[high] = Numberarray[low];} Scan complete, left and right subscript, i.e. low=highnumberarray[low] = basevalue;////Returns the bottom of the base valueMark return low;} 

 5. Merge sort

In theory, the efficiency of merge sorting should be higher than that of fast sorting, but because of the number of assignments and the allocation of temporary arrays, the efficiency should be comparable to that of the fast rows. Its idea is to keep the original array sequence 22 blocks, until each block only one element, and then use the principle of recursion to merge, it is a stable sorting algorithm, time complexity O (N2), the core code is as follows:

  

public static void Main (string[] args) {try {int[] Numberarray = new int[] {4, 3, 6, 1, 2, 5};p artition (Numberarray, 0, NUMBERARRAY.LENGTH-1);} catch (Exception ex) {System.out.println (Ex.getmessage ());}}  public static void partition (Int[] A, int. low, int.) {int mid = (low + high)/2;if (Low < High) {//left partition (A, Low, mid);//Right Partition (A, mid + 1, high);//Merging merge (A, low, mid, high); System.out.println (Arrays.tostring (a));}} public static void Merge (int[] Numberarray, int. Low, int mid, Int. high) {int[] temp = new Int[high-low + 1];////left pointer int i = low;//right pointer int j = mid + 1;int k = 0;//Move the smaller number first to the new array while (I <= mid && J <= High) {if (Numberarray[i] &lt ; Numberarray[j]) {temp[k++] = numberarray[i++];} else {temp[k++] = numberarray[j++];}} Move the left remaining number into the array while (I <= mid) {temp[k++] = numberarray[i++];} Move the remaining number in the right side into the array while (J <= High) {temp[k++] = numberarray[j++];} Overwrite the number in the new array for the nums array for (int k2 = 0; K2 < temp.length; k2++) {numberarray[k2 + low] = temP[K2];}} 

  

  

A summary of common sorting algorithms

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.