1. Select sort
(1), the algorithm thought: the Subscript is (0,1,2,.... N) and then the number is compared with all subsequent figures, each round of comparison results: the first is to determine the smallest number;
(2), code implementation
#include <stdio.h>void sort (Int *a, int count); Void showarray (int *a, Int count); Void showarray (Int *a, int count) { int i; for (i = 0; i < count; i++) { printf ("%d ", a[i]); } printf ("\ n");} Void sort (Int *a, int count) { int i; Int j; int tmp; for (i = 0; i < count; i++) { for (j = i+1; j < count; j++) { if (a[i] > a[j]) { tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } }}void main ( void) { int a[] = {3 ,5 ,6, 1, 7, 2, 9, 8}; int count = sizeof (a)/sizeof (int); sort (A, count); showarray (A, count);}
(3), result printing
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8D/AB/wKioL1iluzagS8axAAAUHPuSkW4217.png-wh_500x0-wm_ 3-wmp_4-s_2255375564.png "title=" Qq20170216224547.png "alt=" Wkiol1iluzags8axaaauhpuskw4217.png-wh_50 "/>
(4), algorithm analysis
The complexity of time is:O (n^2);
2. Exchange (bubbling) sorting
(1), algorithm ideas: adjacent to the 2 numbers, 22 to compare, each round of sorting results: The largest number in the last position;
(2), code implementation
#include <stdio.h>void swapsort (Int *a, int count); Void showarray (Int *a, int count); Void showarray (int *a, int count) { int i ; for (i = 0; i < count; i++) { printf ("%d ", a[i]); } printf ( "\ n");} Void swapsort (Int *a, int count) { int i; int j; int tmp; for (i = 0; i < count; i++) { for (j = 0; j < count-i; j++) { if ( A[j] > a[j+1] { //put large numbers on the last side tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } }}void Main (void) { int a[] = {3, 5, 7, 9, 1, 6, 10}; int count = sizeof (a)/sizeof (int); swapsort ( A, count); showarray (A, count);}
(3), results
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8D/AE/wKiom1ilvo_zo522AAAUd1lMzJA027.png-wh_500x0-wm_ 3-wmp_4-s_1017117261.png "title=" Qq20170216230017.png "alt=" Wkiom1ilvo_zo522aaaud1lmzja027.png-wh_50 "/>
(4), algorithm analysis
Time complexity:O (n^2);
This article is from the "wait0804" blog, make sure to keep this source http://wait0804.blog.51cto.com/11586096/1898654
Select Bubble Sort