Bubble sort optimization and insert sort (this note is less suitable for a certain basis of viewing)

Source: Internet
Author: User
#include <stdio.h> #define CNT 6  //define sort number//bubble sort optimization void bubble_sort (int a[],int N) {for (int P = N-1; P >= 0; p--) {int flag = 0;                  Defines whether the sequence after the flag is ordered for (int i = 0;i < p;i++)/       /A trip bubbling {if (A[i] > a[i+1])          //bubbling {int temp = a[i+1]; A[I+1] = A[i]; A[i] = temp;} flag = 1;} if (flag = = 1)                  //description followed by sequence and ordered not in row break;}} Insert sort void insertion_sort (int a[],int N) {for (int P = 1; P < N; p++)///Initial number take P "0" as the basic number and then insert n card for  loop execution order first execute 1 and then determine if the condition {int Tmp = a[p] is satisfied;      Store the number I need to insert int i;               Record the move position for (i = p;i>0&&a[i-1]>tmp;i--)     //Move the number that needs to be inserted {a[i] = a[i-1];} A[i] = Tmp;}} int main () {int a[cnt] = {5,7,4,1,9,6};insertion_sort (a,cnt); for (int i=0;i< cnt;i++) {printf ("%d", A[i]);} GetChar ();}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bubble sort optimization and insert sort (this note is less suitable for a certain basis of viewing)

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.