C bubble Sort and select sort

Source: Internet
Author: User

Bubble sort

Theory: Starting with the first number, compare the adjacent two numbers, compare the first number to the second number ...., if it is a small to large sort, if the next number is larger than the previous exchange two position, so that the first round of comparison base after the largest number to the last side, followed by a second trip to the comparison, Compared to the number of front N-1, the principle is also the front, large back row, until compared to the previous only a number of times, the completion of the sort, this is the principle of bubbling:

The following use this diagram to represent a pseudo-code, draw a process, to tell the truth, in school, I even do not understand this map!

Bubble-Sorted Code:

#include "stdio.h" #define N 10int Main () {      int a[n];      int i,j,t;      printf ("Please enter the%d number you want to sort \ n");      /* Enter 10 numbers *      /for (i=0;i<n;i++)        scanf ("%d", &a[i]);       /* bubbling */for      (j=0;j<n-1;j++) {for        (i=0;i<n-j;i++) {           if (A[i] > A[i+1]) {               t = a[i];               A[i] = a[i+1];               A[i+1] = t; }}}     printf ("The result of sorting is:");     for (i=0;i<n;i++) {        printf ("%d", A[i]);}     }

Here is the error analysis that occurred during the write process:

Select sort

Theory: This choice to sort together in school, the feeling is dead brain, is a little turn to bend, is not understand, said the feeling of college white, my story is the university white on, was playing four years out to find that they do not understand, ha haha ... Maybe it was stupid then, now talk about this principle, choose Sort, just like Daleitai, we first select the number of the first position as the base position, the following one a challenge, (such as we from small to large sort), if the second number is smaller than the first number, then change Leizhu, the third is smaller than the second, and then change, Until the end of the race, find the smallest in front, then we start the second round, the second round with the second number as the benchmark. The following comparison: Until the last number, so that the whole sort process came out, bored in drawing a picture:

Here's the Basic Code section:

#include "stdio.h" #define N 10int Main () {   int i,j,t,k;   int a[n];    printf ("Please enter%d integers you want to sort", N);   for (i=0;i<n;i++)      scanf ("%d", &a[i]);   /* Note that this cannot be written as N or the following J will cross the border *   /for (i=0;i<n-1;i++) {      k=i;      for (j=i+1;j<n;j++) {         if (A[k]>a[j])  k=j;      }      if (k!=i) {         t= a[i];         A[i] = a[k];         A[k] = t;      }   }  printf ("The result of the final sort is:");     for (i=0;i<n;i++)      printf ("%d", A[i]);}

It's OK, there's nothing wrong here, finally look at the results:

C bubble Sort and select sort

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.