C ++ implementation of various sorting algorithms

Source: Internet
Author: User
Template <class T> inline void swap (T * x, t * Y) {T temp; temp = * X; * x = * Y; * Y = temp ;} template <class T> void quicksort (t list [], int M, int N) {If (M <n) {T key; int I, J, K; k = (m + n)/2)/* choose_pivot */; swap (& list [m], & list [k]); Key = list [m]; I = m + 1; j = N; while (I <= J) {While (I <= N) & (list [I] <= Key )) I ++; while (j> = m) & (list [J]> key) j --; if (I <j) Swap (& list [I], & list [J]);} // swap two elementsswap (& List [m], & list [J]); // recursively sort the lesser listquicksort (list, M, J-1); quicksort (list, J + 1, n) ;}} template <class T> void bubblesort (t r [], int N) // bubble sort {t I, j; t temp; for (I = 0; I <n-1; I ++) for (j = n-1; j> I; j --) if (R [J] <R [J-1]) {temp = R [J]; R [J] = R [J-1]; R [J-1] = temp;} template <class T> void quicksort (t r [], t s, T) // sort R from S to T quickly {t I = s, j = T; t temp; If (S <t) // if at least one element exists in the interval {temp = R [S]; // use the first record in the interval as the benchmark while (I! = J) // perform a quick sorting of R from S to T (scanning from both ends of the interval to intermediate) {While (I <J & R [J] <temp) // scan left to find the first r less than temp [J] J --; if (I <j) // find such R [J], R [I], R [J] interchange {R [I] = R [J]; I ++;} while (I <J & R [I]> temp) // scan the right to find the first R [I] I ++ greater than temp; if (I <j) // find such R [I], R [I], R [J] exchange {R [J] = R [I]; j --;} R [I] = temp; quicksort (R, S, i-1); // recursive sorting of the Left interval quicksort (R, I + 1, t); // recursive sorting of the right interval} void merge (INT R [], int low, int mid, int high) {int * R1; int I = low, j = Mid + 1, K = 0;/* k is the subscript of R1, I and j are respectively the subscript */R1 = (int *) malloc (high-low + 1) * sizeof (INT) of segments 1st and 2 )); while (I <= Mid & J <= high) // cycles when neither segment 1st nor segment 2nd is completely scanned {If (R [I] <= R [J]) // put a relatively small record in section 1st into R1 {R1 [k] = R [I]; I ++; k ++ ;} else // put a relatively small record in section 2nd into R1 {R1 [k] = R [J]; j ++; k ++ ;}} while (I <= mid) // copy the unfinished records in section 1st to R1 {R1 [k] = R [I]; I ++; k ++ ;} while (j <= high) // copy the unfinished records in section 2nd to R1 {R1 [k] = R [J]; j ++; k ++ ;} for (k = 0, I = low; I <= high; k ++, I ++) // copy R1 back to r {R [I] = R1 [k];} inline void mergesortdc (INT R [], int low, int high) /* for R [low... high] perform two-way merge */{int mid; If (low 

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.