Three sort methods (C language) and three sort C Language

Source: Internet
Author: User

Three sort methods (C language) and three sort C Language

1 # include "stdio. h "2 void main () 3 {void read_data (int a [], int n); 4 void write_data (int a [], int n ); 5 void comp (int a [], int n); 6 void insert (int a [], int n); 7 void bubble (int a [], int n ); 8 int I, n = 0, a [200]; 9 printf ("Enter the number of elements:"); 10 scanf ("% d", & n ); 11 printf ("select the sorting method (input 1 select method, input 2 insert method, input 3 bits Bubble Method):"); 12 scanf ("% d ", & I); 13 read_data (a, n); 14 if (I = 1) 15 comp (a, n); 16 if (I = 2) 17 insert (a, n); 18 if (I = 3) 19 bubble (a, n); 20 write_data (a, n ); 21} 22 23 void read_data (int a [], int n) 24 {int I = 0; 25 26 printf ("Enter the element:"); 27 (; I <n; I ++) 28 scanf ("% d", & a [I]); 29} 30 31 void comp (int a [], int n) 32 {int I, j, t; 33 for (I = 0; I <(n-1); I ++) 34 for (j = I + 1; j <n; j ++) 35 if (a [I]> a [j]) 36 {t = a [I]; 37 a [I] = a [j]; 38 a [j] = t; 39} 40} 41 42 void insert (int a [], int n) 43 {int I = 1, j = 0, k, t; 44 for (; I <n; I ++) 45 {for (j = 0; j <I; j ++) 46 if (a [I] <a [j]) 47 {48 t = a [I]; 49 for (k = I; k> j; k --) 50 a [k] = a [k-1]; 51 a [j] = t; 52} 53} 54} 55 56 void bubble (int a [], int n) 57 {int I, j, t; 58 for (I = 0; I <n; I ++) 59 for (j = 0; j <(n-1 ); j ++) 60 if (a [j]> a [j + 1]) 61 {t = a [j + 1]; 62 a [j + 1] = a [j]; 63 a [j] = t; 64} 65} 66 67 void write_data (int a [], int n) 68 {int I = 0; 69 for (; I <n; I ++) 70 printf ("% d \ n", a [I]); 71}


For sorting by selection method, an element is fixed, and then compared with other elements in sequence, the large (or small) is placed after the latter, and the smallest (large) then the first element of the first element is fixed, and the second small (large) element is placed in the second place after the loop.
For sorting by insertion method, the elements to be inserted are compared one by one with the existing sequence, and the elements are inserted in ascending (or descending) Order, then, the elements after the inserted elements of the original sequence are moved one by one, and the number of elements to be inserted is repeated several times.
For the bubble sort method, the two adjacent elements are compared in sequence for sorting. After a loop, the largest (or smallest) is reached to the end; and then re-loop, initialize the assignment values of all previous loops, and then rank the second largest number in the second-to-last order.

Related Article

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.