Sorting algorithm of data structure and algorithm (II.): Exchange sort

Source: Internet
Author: User

interchange sorting can be divided into: bubble sort and quick sort. Where quick sort is an improvement to the bubbling sort1. Bubble sort

principle : Compare the adjacent two numbers, in order to exchange from small to large, so after a trip past, the largest (smallest) number is exchanged to the last one, and then from the beginning of the 22 comparison exchange, until the end of the first bit.

Code implementation:
 for (int i=0;i<a.length-1;i++) {    for (int j=i + 1;j<a.length;j++) {         if (A[j] < a[j-1]) {            = a[j] ^ a[j-1];            A[j-1] = A[j] ^ a[j-1];             = A[j] ^ a[j-1];     }}}

Analysis : Stability, Space complexity O (1), Time complexity "best, worst, average is O (n*n)".

Improvement: If the current order is correct, there is no need to bubble again. You can define a flag variable to flag whether the current order is correct. Code implementation:
 for  (int  i=0;i<a.length-1;i++< Span style= "color: #000000") {bool Swap  = false  ; Define a flag variable swap  for  (int  j=1;j< A.length-i;j++ if  (A[j] < A[j-1 = a[j] ^ a[j-1];            A[j -1] = A[j] ^ a[j-1 = a[j] ^ a[j-1 = true  ; }}  if  (!swap) {return  ;}}  

Analysis: Stability, Space complexity O (1), time complexity [best O (N), worst, average O (n*n)].

2. Quick sort (improvement of bubbling sort)

principle : The data to be sorted is divided into two separate parts by a single trip, and all of the data in one part is smaller than the data in the other part. Then the two parts of the data are sorted quickly by this method, and the whole sort process can be recursive.

Implementation ideas:

    1. The [K1,k2,.... kn] is divided into two sub-regions with the first keyword K1 as the control word (pivot). Make all the keywords in the left area less than or equal to the control word, all the keywords in the right area are greater than or equal to the control word, and the last control is the appropriate position in the middle of two sub-regions.
    2. The left and right areas are recursively treated separately.

Code implementation:

 Public Static void Main (string[] args) {        intnewint[]{50,10,90,20,40,60,80,70};        QuickSort (arr,0,arr.length-1);        Print (arr);    }
Private Static voidPrintint[] arr) {         for(intI:arr) {System.out.println (i+ " "); }    }Private Static voidQuickSort (int[] arr,intLeftintRight ) {        //defining the keyword pivot        intPivot = 0; if(Left <Right ) {           //divides a sequence into two sub-extents to calculate the key valuePivot =partition (Arr,left,right); //Recursive left dial hand zoneQuickSort (arr,left,pivot-1); //Recursive right sub-regionQuickSort (Arr,pivot + 1, right); }}Private Static intPartitionint[] arr,intLeftintRight ) {       //caches the first element of the sequence as a keyword.        intPivotKey =Arr[left]; //loop condition, two pointer left,right, pointing to each end of the sequence, until two pointers coincide         while(Left <Right ) {        //compares the value of the right-most end of the sequence to the keyword, if it is greater than the keyword, and right minus 1 (one on the left) until the condition is not met             while(Left < right && Arr[right] >=PivotKey) { Right--; }            //When right encounters the first less-than-keyword or two-pointer coincidence, it assigns the value to the position pointed to by the left hand. Right Stop movingarr[left++] =Arr[right]; //Move left now             while(Left < right && Arr[left] <=PivotKey) { Left++; }            //When left encounters the first greater-than keyword or two-pointer coincidence, the value is assigned to the position pointed to by the right pointer, and lright moves to the other. Left Stop Movingarr[right--] =Arr[left]; } Arr[left]= PivotKey;//Assign a keyword to left        returnLeft//returns the location of the keyword    }}
分析:算法不稳定,空间代价【最坏O(n)、最好和平均O(logn)】,时间代价【最坏O(n*n)、最好和平均O(nlogn)】.

Sorting algorithm of data structure and algorithm (II.): Exchange 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.