Comparison-based inner Sorting Algorithm (2)

Source: Internet
Author: User

When I went to interview penguin yesterday, I was asked to write and sort for 10 minutes, and the algorithm was random. At that time, I felt that the sorting algorithm was not very skilled enough, and I was afraid of any problems. So when I came back, I wrote the sorting of the seven types several times, consolidated the foundation, and tried to write seven types of sorting in 30 minutes.

 

C/C ++ source code: sort. cpp

# Include <iostream> <br/> # include <cassert> <br/> # include <stack> <br/> # include <cstring> <br/> using namespace STD; </P> <p> void bubble (int A [], int N); <br/> void select (int A [], int N ); <br/> void insert (int A [], int N); <br/> void shell (int A [], int N ); <br/> void quick (int A [], int N); <br/> void heap (int A [], int N ); <br/> void Merge (int A [], int N); <br/> typedef void (* func) (int A [], int N ); <br/> func sort_f UNC [] = {bubble, select, insert, Shell, quick, heap, merge}; <br/> const int sort_num = sizeof (sort_func)/sizeof (func ); <br/> string sort_name [] = {"bubble", "select", "insert", "shell", "quick", "Heap", "merge "}; <br/> const int n = 1024*1024*1024; <br/> const int nums_per_line = 10; </P> <p> int main (INT argc, char * argv []) <br/>{< br/> string input ("data.txt"), output ("sort_output _"); <br/> If (argc = 2) {<br/> input = Argv [1]; <br/>} else if (argc = 3) {<br/> input = argv [1]; <br/> output = argv [2]; <br/>}< br/> file * fptr = NULL; <br/> If (fptr = fopen (input. c_str (), "R "))! = NULL) {<br/> int * arr1 = NULL, * arr2 = NULL; <br/> arr1 = new int [N]; <br/> arr2 = new int [N]; <br/> If (arr1 = NULL) {<br/> cout <"New arr1 failed" <Endl; <br/> fclose (fptr); <br/>} else if (arr2 = NULL) {<br/> cout <"New arr2 failed" <Endl; <br/> fclose (fptr); <br/>}else {<br/> int data, I = 0; <br/> while (fscanf (fptr, "% d", & Data )! = EOF) {<br/> arr1 [I ++] = data; <br/>}< br/> fclose (fptr ); <br/> for (INT c = Sort_num-1; C> = 0; c --) {<br/> memmove (arr2, arr1, sizeof (INT) * I ); <br/> clock_t S, F; <br/> double timeused = 0.0; <br/> S = clock (); <br/> (* sort_func [c]) (arr2, I); <br/> F = clock (); <br/> timeused = (double) (F-S)/clocks_per_sec; <br/> cout <sort_name [c] <":/t" <timeused <"S" <Endl; <br/> assert (C >=0 & C <= 9); <br/> If (FP Tr = fopen (output + (char) (C + '0'). c_str (), "W "))! = NULL) {<br/> for (Int J = 0; j <I; j ++) {<br/> fprintf (fptr, "% d/T ", arr2 [J]); <br/> If (J % nums_per_line = NUMS_PER_LINE-1) <br/> fprintf (fptr, "/N "); <br/>}< br/> fclose (fptr ); <br/>} else {<br/> cout <"can't write OUTFILE/" "<output <"/"" <Endl; <br/>}< br/>}} else {<br/> cout <"can't open input file/" <<input <"/" "<Endl; <br/>}< br/> return 0; <br/>}</P> <p> void swap (I NT & A, Int & B) {<br/> int TMP = A; <br/> A = B; <br/> B = TMP; <br/>}< br/> void bubble (int A [], int N) {<br/> for (INT I = 1; I <n; I ++) <br/> for (Int J = 1; j <= n-I; j ++) <br/> if (a [J] <A [J-1]) <br/> swap (A [J], a [J-1]); <br/>}< br/> void select (int A [], int N) {<br/> for (INT I = 0; I <n-1; I ++) {<br/> int min = I; <br/> for (Int J = I + 1; j <n; j ++) {<br/> if (a [J] <A [Min]) <br/> min = J; <br/>}< br/> swap (A [I], a [Min]); <br/>} <Br/>}< br/> void insert (int A [], int N) {<br/> for (INT I = 1, J; I <N; I ++) {<br/> int TMP = A [I]; <br/> for (j = 0; j <I & A [J] <= TMP; j ++) <br/>; <br/> for (int K = I-1; k> = J; k --) <br/> A [k + 1] = A [k]; <br/> A [J] = TMP; <br/>}< br/> void shell (int A [], int N) {<br/> int h; <br/> for (H = 1; H <N/9; H = 3 * H + 1); <br/> for (; h> 0; h/= 3) {<br/> for (INT I = H, J; I <n; I + = H) {<br/> int TMP = A [I]; <br/> for (j = 0; j <I & A [J] <= TMP; j + = H); <br/> for (int K = I-H; k> = J; k-= H) <br/> A [K + H] = A [k]; <br/> A [J] = TMP; <br/>}</P> <p> struct node {<br/> node (int A, int B ): l (a), R (B) {}< br/> int L, R; <br/>}; <br/> void quick (int A [], int N) {<br/> stack <node *> S; <br/> S. push (new node (0, n-1); <br/> while (! S. empty () {<br/> int L = S. top ()-> L; <br/> int r = S. top ()-> r; <br/> S. pop (); <br/> If (L <r) {<br/> int I = L, j = r; <br/> int occurrence = A [l]; <br/> while (I <j) {<br/> while (A [I] <= cursor & I <= J) I ++; <br/> while (A [J]> = callback & I <= J) j --; <br/> if (I <j) Swap (A [I], A [J]); <br/>}< br/> swap (A [L], a [J]); <br/> If (j-1-l> 0) s. push (new node (L, J-1); <br/> If (r-j-1> 0) s. push (new node (J + 1, R); <br/>}< br /> Void heapify (int A [], int I, int N) {<br/> # define LC (I) (2 * I + 1) <br/> # define RC (I) (2 * I + 2) <br/> # define PAR (I) (I-1)/2) <br/> if (I <n/2) {<br/> int max = A [I], f = 0; <br/> If (max <A [LC (I)] & lc (I) <n) max = A [LC (I)], F = 1; <br/> If (max <A [RC (I)] & rc (I) <n) F = 2; <br/> if (1 = f) {<br/> swap (A [I], a [LC (I)]); <br/> heapify (A, LC (I), N ); <br/>} else if (2 = f) {<br/> swap (A [I], a [RC (I)]); <br/> H Eapify (A, RC (I), n); <br/>}< br/> void heap (int A [], int N) {<br/> If (n <= 1) return; <br/> for (INT I = n/2-1; I> = 0; I --) <br/> heapify (A, I, n); <br/> swap (A [0], a [n-1]); <br/> for (INT I = n-2; I> = 1; I --) {<br/> heapify (A, 0, I + 1 ); <br/> swap (A [0], a [I]); <br/>}< br/> void merge1 (int A [], int L, int R) {<br/> If (L <r) {<br/> int mid = (R-l)/2 + L; <br/> merge1 (A, L, mid); <br/> merge1 (A, Mid + 1, R); <br /> Int * Bak = new int [R-l + 1]; <br/> int I = 0, j = L, K = Mid + 1; <br/> while (j <= Mid & K <= r) <br/> Bak [I ++] = (a [J] <A [k]? A [J ++]: A [k ++]); <br/> while (j <= mid) Bak [I ++] = A [J ++]; <br/> while (k <= r) Bak [I ++] = A [k ++]; <br/> memmove (a + L, Bak, sizeof (INT) * I); <br/>}< br/> void Merge (int A [], int N) {<br/> merge1 (A, 0, n-1); <br/>}</P> <p>

 

 

C/C ++ source code: Data. cpp

Used to generate random data of a certain scale and test the sorting

# Include <iostream> <br/> # include <cstring> <br/> # include <ctime> <br/> # include <cstdlib> <br/> using namespace STD; </P> <p> long CAL (char s []) {<br/> long sum = 0; <br/> int Len = strlen (s ); <br/> for (INT I = 0; I <Len; I ++) {<br/> sum + = sum * 9 + (s [I]-'0'); <br/>}< br/> return sum; <br/>}</P> <p> const int name_len = 128; <br/> const int n = 1024*1024; <br/> int main (INT argc, char * argv []) <br/> {< Br/> srand (Time (null); <br/> long scale = 10000; <br/> char filename [name_len] = "data.txt "; </P> <p> If (argc = 2) {<br/> scale = CAL (argv [1]); <br/>} else if (argc = 3) {<br/> scale = CAL (argv [1]); <br/> strcpy (filename, argv [2]); <br/>}< br/> file * fptr = NULL; <br/> If (fptr = fopen (filename, "W "))! = NULL) {<br/> for (long I = 0; I <scale; I ++) {<br/> fprintf (fptr, "% d/T ", rand () % N); <br/> if (I % 10 = 9) <br/> fprintf (fptr, "/N "); <br/>}< br/>}else {<br/> cout <"can't Write File" <Endl; <br/>}< br/> return 0; <br/>}< br/>

 

Use data to generate a random data of 10 million. The running time is as follows:

 

The running time is as follows:

 

When million random data sets are run, the running time is as follows:

 

Run 100 random datasets and each algorithm runs 1024*16 times. The running time is as follows:

 

Run 10 random data and each algorithm runs 1024*128 times. The running time is as follows:

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.