Exchange class Sort: bubbling, fast (recursive vs. non-recursive)

Source: Internet
Author: User

<pre name= "code" class= "CPP" > Exchange class Sort: 1: Bubble sort O (n^2), Space complexity O (1) 2: Quick sort O (n multiplied by log with 2 base, n logarithm), spatial complexity O (log with 2 base, n logarithm)// Bubble sort void Bubblesort (int r[],int n) {int i,j,tmp,flag;for (i=0;i<n-1;i++) {flag=0;for (j=n-1;j>i;j--) {if (R[j]< R[j-1]) {tmp=r[j]; R[J]=R[J-1]; R[j-1]=tmp;flag=1;}} if (flag==0) return;}} Quick sort void QuickSort (int r[],int s,int t) {int i=s,j=t;int tmp;if (s<t) {tmp=r[s];while (i!=j) {while (J>i && R[J]&GT;TMP) j--; R[i]=r[j];while (i<j && r[i]<tmp) i++; R[j]=r[i];} r[i]=tmp;//the last [i] gave to others, the value here is not used, to hold the benchmark value quicksort (R,S,I-1); QuickSort (r,i+1,t);}} Fast sort non-recursive algorithm void QuickSort (int r[],int n) {int i,j,low,high,top=-1;int temp;struck{int low;int High;} st[maxitem];top++; st[top].low=0; St[top].high=n-1;while (top>-1) {//Out of Stack low=st[top].low;high=st[top].high;top--;i=low;j=high;if (Low

Exchange class Sort: bubbling, fast (recursive vs. non-recursive)

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.