Simple selection and sorting

Source: Internet
Author: User

# Include <stdio. h> # include <assert. h> void display (int * a, int n) {assert (a); for (int I = 0; I <n; I ++) {printf ("% d, ", a [I]);} printf (" \ n ");} void swap (int * a, int * B) {assert (a); assert (B ); int temp; temp = * a; * a = * B; * B = temp;} void select_sort (int * a, int n) {assert (a); int I, j, min; for (I = 0; I <n-1; I ++) // I 0 ~ N-2 {min = a [I]; // each time you select a minimum value for (j = I; j <n; j ++) // j I ~ N-1 {if (a [j] <min) {swap (& (a [j]), & (a [I]) ;}}} int main () {int a [10] = {2, 1, 3, 4, 5, 7, 2, 3, 6, 1}; int num = sizeof (a)/sizeof (int ); printf ("before_sort:"); display (a, num); select_sort (a, num); printf ("after_sort:"); display (a, num ); return 0 ;}
# Include <stdio. h> # include <time. h> // exchange two data void swap (int * a, int * B) {int temp; temp = * a; * a = * B; * B = temp ;} // display the exchanged array void display_array (int a [], int n) {int I; for (I = 0; I <n; I ++) printf ("% d", a [I]);} // select the sort void select_sort (int a [], int n) {int I, j; for (I = 0; I <n-1; I ++) {int min = a [I]; // set the minimum number for each time, I 0 ~ 8for (j = I + 1; j <n; j ++) // convert a [I] ~ Compare the number of a [9] values with the current minimum number. if the number is smaller than the current minimum number, {if (a [j] <min) {swap (& (a [j]), & (a [I]) ;}}} int main () {clock_t start, finish; start = clock (); int n = 10; int a [] = {2, 1, 3, 4, 5, 7, 6, 8, 2, 4}; printf ("Before sorting :"); display_array (a, n); select_sort (a, n); printf ("After sorting:"); display_array (a, n); finish = clock (); printf ("\ n total computing time: % f seconds \ n", (double) (finish-start)/CLOCKS_PER_SEC); return 0 ;}

 

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.