Data structure Common sort (Java)

Source: Internet
Author: User

The concept of the data structure sorting algorithm is transcribed from the Internet:

Basic concepts:

1, sort: According to a certain keyword, a sequence is arranged into a new sequence you want to get.

2, internal sorting and external sorting: The whole sort process is done in memory, called internal sorting. Large amounts of data require external storage devices to complete, called external sorting.

3. Primary keyword and this keyword:

4, sequencing Stability: For the same element, before and after the newsletter is the same, then this sort is a stable sort, if the order has changed, then it is unstable sort.

Insert class Sort:

(a) Thought: In an already sequenced sequence, the non-queued elements are inserted into the specified position in accordance with the original rules.

(ii) Classification:

1. Direct Insert Sort:

① thought: The most basic insertion sort, insert the first I into the proper position of the former i-1.

② time Complexity: T (n) = O (n).

③ Space Complexity: S (n) = O (1).

④ Stability: Stable sequencing. The Loop condition while (R[0].key < R[j].key) is guaranteed.

⑤ Program:

 /**  *  Direct Insertion Sort:   * 1. Starting with the first element, the element can be thought to have been sorted    * 2. Takes the next element and scans the   * 3 from behind in the sequenced sequence of elements. If the element is smaller than the preceding element (sorted), then compare to the previous element if it is less than the interchange until it is found that is greater than the element, then stop;   * 4. If the element is larger than the previous element (sorted), repeat step 2 5. Repeat steps 2~4  until all elements are ordered  .   *   *  @param  a  */ public static void  Insert (Int[] a)  {  for  (int i = 1; i < a.length;  i++)  {   int key = a[i];   for  (int j =  i - 1; j >= 0; j--)  {    if  (key  &LT;&NBSP;A[J])  {     int temp = a[j];      a[j] = key;     a[i] = temp;     i  = j;    }   } &nbsP;}  }

2. Binary Insert Sort:

① thought: Because it has been determined that the first part is ordered sequence, so in the search for the insertion position can be found by the binary method to find, improve efficiency.

② time Complexity: The time to compare is reduced to O (n㏒n), but the time spent on moving elements remains the same, so it is always time complexity or O (n).

③ Space Complexity: S (n) = O (1).

④ Stability: Stable sequencing.

⑤ Program:

 /**  *  binary Insert Sort:  *  in the process of inserting a new element into an ordered array,,  sets the first element of the area to be inserted to A[low] when the insertion point is found. The last element is set to a[high],  *  when the element is to be inserted and a[m], where m= (Low+high)/2 is compared, if larger than the reference element,  *  "A[low" to a[ M-1] for the new insert area (i.e. high=m-1),  otherwise select a[m+1] to A[high] for the new insert area (i.e. low=m+1),  *  so until Low<=high is not established, After this position, all elements move back one,  and insert the new element into the a[high+1].   *   *  @param  a  */ public static void  Binaryinsert (Int[] a)  {  // {1, 5, 5, 3, 4, 3,2}   for  (int i = 1; i < a.length; i++)  {   int  key = a[i];   int low = 0;   int high =  i - 1;   while  (Low <= high)  {     int mid =  (Low + high)  / 2;    if  (key  < a[mid])  {     high = mid - 1;     } else {     low = mid + 1;     }   }   system.out.println ("Low:"  + low +  ", High:"  + high +  ", I:"  + i     +  ", Key:"  + key) ;   for  (int j = i; j > high + 1; j--)  {    a[j] = a[j - 1];   }   a[high  + 1] = key;  } }

3. Shell sequencing

Exchange class Sort:

(a) Thought: The method of ordering by exchanging reverse order elements.

(ii) Classification:

1. Bubble Sort:

① thought: Repeated scanning to sort sequence, in the process of scanning in order to compare the size of the adjacent two elements, if reverse exchange position. The first trip, starting with the first data, compares the next two data, (in ascending order), if the large exchange, get a maximum data at the end, and then the second trip, only scan the first n-1 elements, get the second largest placed in the penultimate position. And so on, the ascending sequence is finally obtained. If there is no exchange during the scan, the sequence is queued and the scan is terminated directly. So up to n-1 scan.

② time Complexity: T (n) = O (n).

③ Space Complexity: S (n) = O (1).

④ Stability: Stable sequencing.

⑤ Program:

 /**  *  Bubble Sort:  *  compares adjacent two numbers in turn, places decimals in front, and large numbers on the back.   is on the first trip: Compare the 1th and 2nd numbers first, put the decimals before the decimal, and put the large number behind.   *  then compares the 2nd and 3rd numbers, puts the decimal before the large number, and then continues,  until the last two digits are compared, and the decimal is placed before the large number.   At the end of the first trip, put the biggest number in the end.   *  perform the above steps a.leng-1 times, sorting complete.   *   *  @param  a  */ public static void  Bubble (Int[] a)  {  // {1, 5, 5, 3, 4, 3,2}  for   (int i = 1; i < a.length; i++)  {   for  ( int j = 0; j < a.length - i; j++)  {     if  (A[j + 1] < a[j])  {     int temp  = a[j + 1];     a[j + 1] = a[j];      a[j] = temp;    }   }  } } 

2. Quick sort:

① thought: Bubble sort can only eliminate one reverse order at a time, so as to eliminate multiple reverse orders at once, using quick sorting. With a keyword as the axis, from left to right to compare with, and then exchange, the first trip after the end of the sequence can be divided into two sub-sequences, and then segmented for fast sorting, to achieve efficiency.

② time complexity: Average t (n) = O (n㏒n), worst O (n).

③ Space Complexity: S (n) = O (㏒n).

④ Stability: unstable sorting. {3, 2, 2}

⑤ Program:

 /**  *  Quick Sort:  *  A quick sort of algorithm is:    * 1) set two variables L, h, when the sort starts: l= 0,H=N-1;&NBSP;&NBSP;*&NBSP;2) takes the first array element as the key data, assigns a value to key, i.e. key=a[0];  * 3) to search forward from J, that is, from the beginning to the forward search, Find the first value less than key a[j], swap a[j] and a[i], and then h--;  * 4) from I start backward search, that is, from the beginning to search backward, find the first one greater than the key A[i], will a[i] and A[j] interchange, and then l++ &NBSP;&NBSP;*&NBSP;&NBSP;5) Repeat steps 3rd, 4, until l==h  *   *  @param  a  */  public static void quick (int[] a, int l, int h)  {   int low = l;  int high = h;  int key =  a[low];  while  (Low < high)  {   //looking forward    while   (Low < high && key < a[high])  {     high--;   }   if  (Low < high)  {     int temp = a[high];    a[high] = a[low];    a[low] = temp;     low++;   }   //to    while  (low  < high && key > a[low])  {    low++;    }   if  (Low < high)  {    int  Temp = a[high];    a[high] = a[low];    a[low]  = temp;    high--;   }  }  if  (Low  > l)  {   quick (a, l, low-1);  }  if  (high &NBSP;&LT;&NBSP;H)  {   quick (a, high+1, h);   } }

Select Class Sort:

(a) thought: each trip in N–i + 1 (i =, ..., n-1) the record of the lowest keyword selected as the first record in the ordered sequence.

(ii) Classification:

1, simple selection of sort:

① thought: The first time, starting from the first record, through the comparison of the n–1 sub-keyword, the record from N records to select the minimum number of records, and exchange with the first record. The second trip starts with the second record, selecting the minimum and second record exchange. And so on until all sorts are finished.

② time Complexity: T (n) = O (n).

③ Space Complexity: S (n) = O (1).

④ Stability: Unstable sort, {3, 3, 2}.

⑤ Program:

 /**  *  Select Sort:  *  to traverse N data on the first trip, find out the smallest of the values in exchange with the first element,  *  The second trip traverses the remaining N-1 data to find out the smallest of the values exchanged with the second element ......  *  N-1 traversal of the remaining 2 data, find the smallest of the values with the N-1 element Exchange,   *   Select sort complete.   *  @param  a  */ public static void selectsort (int[] a)  {  //{ 1, 5, 5, 3, 4, 3, 2 };  for (int  i=1; i<a.length; i++)  {   int flag = i-1;   for (int j=i; j<a.length; j++)  {    if (A[flag] > a[j])  {     flag = j;    }   }    if (flag != i-1)  {    int temp = a[flag];     a[flag] = a[i-1];    a[i-1] = temp;    }   ToString (a);   } } 

3. Heap sequencing:

① thought: The keyword to be sorted in the array R[1...N], the R as a moment full binary tree of the order of the expression, each node represents a record, the first record r[1] as the root of the binary tree, a bit of records R[2...N] sequentially from left to right order, any node r[i] The left child is r[2i], the right child is r[2i+1], and the parents are r[i/2 downward rounding]. Then the complete binary tree is adjusted to build the heap.

② time Complexity: T (n) = O (n㏒n).

③ Space Complexity: S (n) = O (1).

④ Stability: unstable sorting. {5, 5, 3}

Data structure Common sort (Java)

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.