Eight internal sorting algorithms (top)-bubbling, direct insertion, simple selection, fast

Source: Internet
Author: User

Eight internal sorting algorithms (top) bubbling, direct insertion, simple selection, fast
Sorting is divided into internal and external sorting, the internal sort is the data records are sorted in memory, and the external sort is because the sorted data is large, cannot hold all the sort records at once, and requires access to external memory during the sorting process.

Here we talk about eight sorts of sort are internal sort.


1. Direct Insert Sort

Insert a record into the sorted ordered table to get a new, sequential table with a 1 increase in the number of records. That is, the 1th record of the sequence is considered an ordered subsequence, and then inserted from the 2nd record one after the other until the entire sequence is ordered.

Important: Set up Sentinel as a temporary storage and judgment array boundary.

The direct insert implementation is as follows:

        */* 1. Insert sort * thought: Each traversal, the array of the table below is the current number of numbers inserted into the order in the row to be inserted into the sorting, always first look for the insertion position, and then in the implementation of the move and insertion process, find the insertion position in order to find the way (forward or backward), * Since the array that needs to be inserted is already ordered, it is possible to find the insertion position using the binary search method, and improve the efficiency of the algorithm, but the time complexity of the algorithm is still O (N2). */public void Insertsort (Integer []array) {int Size=array.length;int temp; System.out.println ("Insert sort result:"); for (int i=1;i<size;i++) {if (Array[i]<array[i-1]) {if (int j=i;j>0;j--) {if ( Array[j]<array[j-1]) {temp = array[j-1];array[j-1] = array[j];array[j] = temp;}} ========= Output Sort Details System.out.print ("n" + (i) + "Times:"); for (int j = 0; J < Array.Length; J + +) {System.out.print (Array[j] + "");} System.out.println ();}}}


2. Simple selection of sortingIn the set of numbers to sort, select the smallest (or largest)athe number is exchanged with the 1th position ;then, in the remaining number, find the smallest (or largest) number exchanged with the 2nd position , and so on, until the first N-1 element (the penultimate number) and the nth element (the lastanumber) is compared. The specific implementation is as follows
<span style= "White-space:pre" ></span>/* * 2. Simple selection Sort * Idea: iterate over the remaining digits of the array, selecting the smallest number of times and the current number of traversal positions to replace the */public void Choicesort (Integer []array) {int size=array.length; System.out.println ("\ n Select sort:"); for (int i = 0; i < size; i++) {int temp = array[i],min=i;for (int j = size-1; J >=i; j--) {if (Array[j] <temp) {temp = array[j];min=j;}} if (min!=i) {array[min]=array[i];array[i]=temp;} ========= Output Sort Details System.out.print ("n" + (i+1) + "Times:"); for (int j = 0; J < Array.Length; J + +) {System.out.print (Array[j] + "");} System.out.println ();}}

3. Bubble sortIn the set of numbers to be sorted, all the numbers in the range that are not currently in sequence are compared and adjusted from top to bottom for the next two numbers, allowing largerthe number to sink, and the smaller ones go up. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.
3. Bubble sort public      void Bubble_sort (Integer []array) {          int size = Array.Length;          int tmp;          System.out.println ("bubble sort Result:");          for (int i=0;i<size;i++) {for              (int j=size-1;j>i;j--) {                  if (Array[j]<array[j-1]) {                      tmp=array[j-1] ;                      ARRAY[J-1]=ARRAY[J];                      array[j]=tmp;                  }              }              ========= output Sort details              System.out.print ("+ (i+1) +" Times: ");              for (int j = 0; J < Array.Length; J + +) {                  System.out.print (Array[j] + "");              }              System.out.println ();          }      }

4. Quick Sort

Basic idea:

1) Select a Datum element , usually the first element or the last element is selected ,

2) The records to be sorted are divided into two separate parts, and some of the recorded elements are smaller than the datum element values. The element value of another part of the record is larger than the base value.

3) At this point the datum element is in the correct position after it is sorted

4) Then proceed to sort the two parts of the record in the same way until the entire sequence is ordered.

The specific implementation is as follows:
      /* * 4. Quick Sort *      /public void QuickSort (Integer []array, Int. Low, Int. high) {          int i=low,j=high;          if (i<j) {              int po = array[low];              while (I<J) {                  while (i<j && po<array[j]) {                      j--;                  }                  if (i<j) {                      int temp = array[i];                      Array[i] = array[j];                      ARRAY[J] = temp;                      i++;                  }                  while (I<j && po>array[i]) {                      i++;                  }                  if (i<j) {                      int temp = array[i];                      Array[i] = array[j];                      ARRAY[J] = temp;                      j--;                  }              }              QuickSort (array,low,j-1);              QuickSort (Array,j+1,high);          }      }

Test:
Package com.devin.sequence;/** *  * @JavaTest * @author  geot  * @date    created: April 14, 2015 PM 3:19:35  * @ Version April 14, 2015   * */public class Test {public static void main (string[] args) {Integer [] array = {1,33,3,14,5,71,6 , 88}; Test T = new test ();//t.bubble_sort (array); System.out.println ();//t.insertsort (array);//t.choicesort (array);//Quick sort T. QuickSort (array, 0, array.length-1); System.out.println (); for (int j2 = 0; J2 < array.length; j2++) {System.err.print (array[j2]+ "");}}


Eight internal sorting algorithms (top)-bubbling, direct insertion, simple selection, fast

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.