Sequencing of data structures and algorithms (Summary II)

Source: Internet
Author: User

Exchange class sorting is mainly through 22 comparison of the keywords to be ordered, if the discovery and sorting requirements inverse, then "exchange". In this sort of sorting method the most common is the bubble sort and the fast sorting, in which the fast sort is a kind of algorithm which has the very good performance in the actual application.

1. Bubble sort A. Algorithm description

The idea of bubble sorting is very simple. First, compare the first and second of the n elements, and if the position of the two elements is reversed, the position of the two elements is exchanged, and then the second and third element keywords are compared, and so on, until the n-1 and nth elements are compared; The above procedure describes the first sequencing process for bubbling sorting, During the first sequencing, we put the most important element of the keyword in the most position of the sequence with n elements through the swap operation. The second order is then followed by the same operation of the first n-1 elements of the sequence of elements during the second sequencing, with the result that the key-size elements are placed in the n-1 position by swapping. In general, the ordering of the first n-i+1 of the sequence of elements is ordered, which causes the most important elements of the first n-i+1 elements to be placed in the n-i+1 position. Sort by n-1 the sequence of elements, so that the element sequences are ordered by keyword.

B. Algorithm implementation
public void Bubblesort (int[] r, Int. Low, int.) {int n = high-low + 1;for (int i = 1; i < n; i++) for (int j = low; J <= High-i; J + +) if (!compare (R[j], r[j + 1])) {int temp = R[j];r[j] = r[j + 1];r[j + 1] = temp;}}

  

"Efficiency analysis"
Space efficiency: Only one sub-cell is used.
Time efficiency: Assuming that the number of elements to be sorted n, then a total of n-1 order, the sub-sequence of j elements to make a trip to the bubble sort needs to be j-1 sub-keyword comparison, bubble sorting time complexity of 0 (n²).

C. Algorithm examples

Bubblesort.java

Package Com.test.sort.exchange;public class Bubblesort {/** * @param args */public static void main (string[] args) {//TOD O auto-generated method StubSystem.out.println ("bubble sort sort Function implementation" ""); int[] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; Bubblesort sort = new Bubblesort (); SYSTEM.OUT.PRINTLN ("Sequence before sequencing:"); Sort.printarr (arr); Sort.bubblesort (arr, 0, arr.length-1); System.out.println ("Sorted after sequence:");; Sort.printarr (arr);}  public void Bubblesort (int[] r, Int. Low, int.) {int n = high-low + 1;for (int i = 1; i < n; i++) for (int j = low; J <= High-i; J + +) if (!compare (R[j], r[j + 1])) {int temp = R[j];r[j] = r[j + 1];r[j + 1] = temp;}} End of Bubblesortpublic boolean compare (int parama, int paramb) {if (Parama < Paramb) {return true;} else {return FA LSE;}} /** * Prints an array of elements */public void Printarr (int[] arr) {if (arr! = null) {for (int temp:arr) {System.out.print (temp + "   " );} System.out.println ();}}}

  

D. Results output

2. Quick sort A. Algorithm description

Fast sorting is a typical example of applying the divide-and-conquer method to the sorting problem, and the basic idea of the fast ordering is that the sequence of n elements is divided into left and right two subsequence ll and LR through a PIVOT element, where the elements in the subsequence ll are smaller than the pivot elements. While the elements in the subsequence LR are larger than the pivot elements, and then the left and right subsequence are sorted quickly, the whole sequence is ordered after the left and right sub-sequences are sorted, and the ordering process of left-to-right sub-sequences ends when the subsequence contains only one element, while the left and right subsequence are naturally ordered because only one element is included. The process of describing the quick sort using the three steps of the divide and conquer method is as follows:

1. Step: Divide the sequence by the pivot element x, and the elements of the left subsequence are less than x, and the elements of the right sub-sequence are greater than x;
2. Governance steps: Recursive to the left and right sub-sequence sequencing;
3. Combination steps: None
From the description of the quick sort algorithm above, we see that the implementation of the fast sorting algorithm relies on the process of dividing the sort sequence by the pivot element x. The sorting sequence is treated by using two pointers, low and high, respectively, pointing to the range of the sequence R to be divided, taking the element of low as the pivot, or pivot = R[low]. The Division first searches from the elements of the high position to the first element that is smaller than the pivot, sets it to the position indicated by low, and then searches backwards from the element in the position of low to the first element that is larger than the pivot, and sets it to the position indicated by high. Repeat these two steps until Low = high, and finally set the pivot to the position where low and high are pointing together. Using the partitioning method described above, the ordered sequence can be divided into two sub-sequences by pivot element Pivot, although the choice of pivot must not necessarily be r[low], but may be r[low. Any data element between high and high].

B. Algorithm implementation
Based on the partitioning algorithm, the recursive implementation of the fast sorting algorithm
Public voidQuickSort (int[] R,intLowintHigh ) { if(Low <High ) { intPA =partition (R, Low, high); QuickSort (R, Low, PA-1); QuickSort (R, PA+ 1, high); }}//divides the sequence into two sub-sequences and returns the position of the pivot elementPrivate intPartitionint[] R,intLowintHigh ) { intPivot = R[low];//use R[low] as the pivot element while(Low < High) {//alternating inward scanning from both ends while(Low < high &&!)Compare (R[high], pivot)) High--; R[low]= R[high];//move elements smaller than pivot to the low end while(Low < High &&Compare (R[low], pivot)) Low++; R[high]= R[low];//move elements that are larger than pivot to the high end} R[low]= pivot;//Set Pivot returnLow//return pivot element position}

"Efficiency analysis"
Space efficiency: Not sure, worst case is n.
Time efficiency: Assuming that the number of elements to be sorted n, then a total of n-1 order, the sub-sequence of j elements to make a trip to the bubble sort needs to be j-1 sub-keyword comparison, bubble sorting time complexity of 0 (n log n).

C. Algorithm examples

Quicksort.java

Package Com.test.sort.exchange;public class QuickSort {/** * @param args */public static void main (string[] args) {//TODO Auto-generated method StubSystem.out.println ("Quick sort sort Function implementation" ""); int[] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; QuickSort sort = new QuickSort (); SYSTEM.OUT.PRINTLN ("Sequence before sequencing:"); Sort.printarr (arr); Sort.quicksort (arr, 0, arr.length-1); System.out.println ("Sorted after sequence:");; Sort.printarr (arr);} public void QuickSort (int[] r, Int. Low, int.) {if (Low < high) {int PA = partition (R, Low, High), QuickSort (R, Low, Pa-1); QuickSort (R, PA + 1, high);}} private int partition (int[] r, int low, int. high) {int pivot = R[low];//using r[low] as pivot element while (Low < high) {//alternating inward from both ends Scan while (Low < high &&!compare (R[high], pivot)) High--;r[low] = R[high]; Move elements smaller than pivot to low-end while (lower < high && compare (R[low], pivot)) Low++;r[high] = R[low]; Move elements larger than pivot to high-end}r[low] = pivot; Set pivot return low; Returns the Pivot element position}public Boolean compare (int parama, int paramb) {if (Parama &Lt PARAMB) {return true;} else {return false;}} /** * Prints an array of elements */public void Printarr (int[] arr) {if (arr! = null) {for (int temp:arr) {System.out.print (temp + ""); }system.out.println ();}}}

  

D. Results output

Sequencing of data structures and algorithms (Summary II)

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.