C language: how to optimize the sorting of a group by the bubble method?

Source: Internet
Author: User

C language: how to optimize the sorting of a group by the bubble method?

# Define _ CRT_SECURE_NO_WARNINGS 1 # include <stdio. h> # include <stdlib. h> int main () {int arr [] = {5, 6, 7, 8, 9, 1, 2, 3, 4}; int I = 0; int j = 0; int flag; int size = sizeof (arr)/sizeof (arr [0]); for (I = 0; I <size-1; I ++) {flag = 1; // set the flag to optimize the bubble for (j = 0; j <size-1-I; j ++) {if (arr [j] <arr [j + 1]) {int tmp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = tmp; flag = 0 ;}} if (flag) // indicates that it is not in the if internal state, that is, it has not been exchanged as the required output sequence break ;} for (I = 0; I <size; I ++) {printf ("% d", arr [I]) ;}system ("pause"); return 0 ;}

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.