Bubble sort, select sort

Source: Internet
Author: User

Bubble Sort :

Ideas:

In a group of numbers, the adjacent two numbers are compared, exchanged, and the maximum (small) number is exchanged to the tail (first) part, that is, a bubble sort is completed.

To sort n numbers, loop n times.

Implementation code:

 voidSortintAintlen)//receive the array address of the main function, and the length {inttemp;  for(intI=0; I <= len-i;i++)//need to compare the number of times, if there are 4 numbers, you need to comparison 3 times { for(intj =0; J <= len-i-1; j + +)//number of times to compare per trip {if(A[j] > a[j+1]) {temp=A[j]; A[J]= a[j+1]; A[j+1] =temp; }             }         } }

Select sort

/*December 10, 2017 11:08:52 Select sort * Idea: first the minimum number of 10 numbers and a[0] to swap, and then the a[1]~a[9] * The smallest number and a[1] swap ... For each comparison round, find the smallest one in an unordered number. A total of 9 rounds * A[0] a[1] a[2] a[3] a[4] * 3 6 1 9 4 unsorted case * 1 6 3 9 4 will 5 The smallest number of 1 and a[1] Swap * 1 3 6 9 4 The smallest of the 4 digits of the remainder 3 and a[1] Swap * 1 3 4 9 6 will be the smallest of the remaining 3 numbers Number 4 and a[2] Swap * 1 3 4 6 9 The smallest of the remaining 2 numbers 6 and a[3] Swap * sort complete! */#include<stdio.h>voidSortintA[],intn);intMainvoid){    inti,a[Ten]; printf ("Please enter 10 number:");  for(i=0;i<Ten; i++) scanf ("%d",&A[i]); printf ("before sorting:");  for(i =0;i<Ten; i++) printf ("%4d", A[i]); printf ("\ n"); Sort (A,Ten); printf ("after sorting:");  for(i =0;i<Ten; i++) printf ("%4d", A[i]); return 0; }voidSortintArray[],intN) {    inti,k,j,t;  for(i=0; i<n-1; i++)//number of times to compare{k=i;  for(j = i+1; j<n;j++)        {            if(Array[j] < array[k])//If the last digit is smaller than the previous one, the number is exchanged .K =J; } t=Array[k]; ARRAY[K]=Array[i]; Array[i]=T; }    }

  

  

Bubble sort, 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.