Various sorting source programs (with drivers, can be directly tested)

Source: Internet
Author: User

# Include <iostream> <br/> # include <ctime> <br/> using namespace STD; </P> <p> void arraycopy (int A [], int B [], int N) <br/>{< br/> for (INT I = 0; I <n; I ++) <br/> B [I] = A [I]; <br/>}</P> <p> void insertsort (int A [], int B [], int N) <br/>{< br/> arraycopy (A, B, n); // restore the original data </P> <p> int temp; <br/> for (INT I = 1; I <n; I ++) <br/>{< br/> temp = A [I]; <br/> Int J = I-1; <br/> while (j> = 0 & A [J]> temp) <br/>{< br/> A [J + 1] = A [J]; <br/> J --; <br/>}< br/> A [J + 1] = temp; <br/>}< br/> cout <"insertsort: "<Endl; <br/> for (INT I = 0; I <n; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; <br/>}</P> <p> void insertsortbin (int A [], int B [], int N) <br/>{< br/> arraycopy (A, B, n); <br/> Int J, low, high, mid; <br/> int temp; <br/> for (INT I = 1; I <n; I ++) <br/>{< br/> temp = A [I]; <br/> low = 0; <br/> high = I-1; <br/> while (low <= high) <br/> {<br/> mid = low + (high-Lo W)/2; // obtain the intermediate position <br/> If (temp <A [Mid]) <br/> high = mid-1; // insert point in the left half <br/> else <br/> low = Mid + 1; // insert point in the right half <br/>}< br/> J = I-1; <br/> while (j> = high) <br/> {<br/> A [J + 1] = A [J]; // record move back <br/> j --; <br/>}< br/> A [high + 1] = temp; // insert <br/>}< br/> cout <"binaryinsertsort:" <Endl; <br/> for (j = 0; j <N; j ++) <br/> cout <A [J] <""; <br/> cout <Endl; <br/>}</P> <p> void shellsort (int A [], int B [], int N) <br/>{< br/> Array Copy (A, B, n); <br/> int I, j; <br/> int temp; <br/> int d = n/2; // D obtain the initial value n/2 <br/> while (D> 0) <br/>{< br/> for (I = D; I <N; I ++) // convert R [D .. n-1] Insert the current ordered area of each group <br/>{< br/> J = I-d; <br/> while (j> = 0 & A [J]> A [J + D]) <br/> {<br/> temp = A [J]; <br/> A [J] = A [J + D]; <br/> A [J + D] = temp; <br/> J = J-D; <br/>}< br/> D/= 2; // decrease incremental d <br/>}< br/> cout <"shellsort: "<Endl; <br/> for (I = 0; I <n; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; <br/>}</P> <p> void bubblesort (int A [], int B [], int N) <br/>{< br/> arraycopy (A, B, n); <br/> int temp; <br/> for (INT I = 0; I <n-1; I ++) <br/>{< br/> for (Int J = n-1; j> I; j --) <br/> {<br/> if (a [J] <A [J-1]) <br/> {<br/> temp = A [J]; <br/> A [J] = A [J-1]; <br/> A [J-1] = temp; <br/>}< br/> cout <"bubblesort:" <Endl; <br/> for (INT I = 0; I <n; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; <br/>}</P> <p> void bubbl Esortadv (int A [], int B [], int N) <br/>{< br/> arraycopy (A, B, n); <br/> int temp; <br/> int exchange; <br/> for (INT I = 0; I <n-1; I ++) <br/>{< br/> exchange = 0; <br/> for (Int J = n-1; j> I; j --) <br/> {<br/> if (a [J] <A [J-1]) <br/>{< br/> temp = A [J]; <br/> A [J] = A [J-1]; <br/> A [J-1] = temp; <br/> exchange = 1; <br/>}< br/> // No exchange occurred, it indicates that the ordered area is already globally ordered, and the loop is exited halfway <br/> If (exchange = 0) <br/> break; <br/>}< br/> cout <"Advanced Bubblesort: "<Endl; <br/> for (INT I = 0; I <n; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; <br/>}</P> <p> void quicksort (int A [], int S, int t) <br/>{< br/> int I = s, j = T; <br/> int temp; <br/> If (S <t) <br/> {<br/> temp = A [I]; <br/> while (I! = J) <br/>{< br/> while (j> I & A [J]> = temp) <br/> j --; <br/> A [I] = A [J]; <br/> while (I <J & A [I] <= temp) <br/> I ++; <br/> A [J] = A [I]; <br/>}< br/> A [I] = temp; <br/> quicksort (A, S, I-1); <br/> quicksort (A, I + 1, t ); <br/>}</P> <p> void selectsort (int A [], int B [], int N) <br/>{< br/> arraycopy (A, B, n); <br/> int temp, K; <br/> for (INT I = 0; I <n-1; I ++) // sort by I <br/> {<br/> K = I; <br/> for (Int J = I + 1; j <n; j ++) // in [I... N-1] R [k] with the smallest key <br/>{< br/> if (a [J] <A [k]) <br/> K = J; <br/>}< br/> If (K! = I) <br/>{< br/> temp = A [I]; <br/> A [I] = A [k]; <br/> A [k] = temp; <br/>}< br/> cout <"select sort:" <Endl; <br/> for (INT I = 0; I <n; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; <br/>}</P> <p> // heap sorting <br/> // create the maximum heap: complexity: O (logn) <br/> void sift (int A [], int low, int high) <br/>{< br/> int I = low; <br/> Int J = 2 * I; // left child <br/> int temp = A [I]; <br/> while (j <= high) <br/>{< br/> // The right child is large, point to right child <br/> If (j <High & A [J] <A [J + 1]) <br/> J ++; <br/> If (temp <A [J]) <br/>{< br/> A [I] = A [J]; // adjust a [J] to the position of the parent node <br/> I = J; <br/> J = 2 * I; <br/>}< br/> else <br/> break; <br/>}< br/> A [I] = temp; // put the value of the filtered node to the final position <br/>}</P> <p> void heapsort (int A [], int B [], int N) <br/>{< br/> arraycopy (A, B, n); <br/> int I, temp; </P> <p> // cyclically build the initialization heap: the complexity here is O (n) <br/> for (I = n/2-1; i> = 0; I --) <br/> sift (A, I, n-1); </P> <p> // executes n-1 cycles, complete heap sorting <br/> for (I = n-1; I> = 1; I --) <br/> {<br/> // exchange the value of the first element with a [I] in the current zone <br/> temp = A [0]; <br/> A [0] = A [I]; <br/> A [I] = temp; </P> <p> // filter a [0] node (subscript from 0 to i-1) to obtain a heap of an I node <br/> sift (A, 0, i-1); <br/>}< br/> cout <"Heap Sort:" <Endl; <br/> for (I = 0; I <N; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; <br/>}</P> <p> void Merge (int A [], int C [], int left, int M, int right) <br/>{< br/> int I = left; <br/> Int J = m + 1; <br/> int K = left; <br/> while (I <= M & J <= right) <br/> {<br/> if (a [I] <= A [J]) <br/> C [k ++] = A [I ++]; <br/> else <br/> C [k ++] = A [J ++]; <br/>}</P> <p> while (I <= m) <br/> C [k ++] = A [I ++]; <br/> while (j <= right) <br/> C [k ++] = A [J ++]; </P> <p >}</P> <p> void mergesort (int A [], int C [], int left, int right) <br/>{< br/> If (left <right) <br/>{< br/> int I = left + (right-left)/2; // take two-way split Points </P> <p> mergesort (A, C, left, I); <br/> mergesort (A, C, I + 1, right); <br/> // merge the two halves (separated by I) in the sorted order of a into array B <br/> Merge (a, c, left, I, right ); </P> <p> // copy the sorted part of array C to original array A (here, the space complexity is O (n )) <br/> for (INT I = left; I <= right; I ++) <br/> A [I] = C [I]; <br/>}</P> <p> int main () <br/>{< br/> int N; <br/> cout <"input count:"; <br/> CIN> N; <br/> int * A = new int [N]; <br/> int * B = new int [N]; <br/> srand (unsigned) Time (null); <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> A [I] = rand (); <br/> cout <A [I] <""; <br/>}< br/> cout <Endl; </P> <p> // insert sorting <br/> insertsort (A, B, n); <br/> insertsortbin (B, A, N ); <br/> shellsort (A, B, n); <br/> // exchange sorting <br/> bubblesort (B, A, N ); <br/> bubblesortadv (A, B, n); </P> <p> arraycopy (B, A, n); <br/> quicksort (A, 0, n-1); <br/> cout <"Quick Sort:" <Endl; <br/> for (INT I = 0; I <n; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; </P> <p> // select sorting <br/> selectsort (A, B, n); <br/> heapsort (B, A, N ); </P> <p> // merge sort <br/> int * c = new int [N]; <br/> arraycopy (A, B, n ); <br/> mergesort (A, C, 0, N-1); <br/> cout <"merge sort:" <Endl; <br/> for (INT I = 0; I <n; I ++) <br/> cout <A [I] <""; <br/> cout <Endl; <br/> Delete [] C; </P> <p> return 0; <br/>}

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.