Bubble sort and simple selection sort

Source: Internet
Author: User

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" j_0057.gif "/> bubble sort: (e.g. ascending)

In the first order, starting with the first element, the entire sequence of elements to be sorted is scanned, and if the adjacent two elements are reversed, the position is swapped. Until the last element, at this point, the last element must be the largest element.

The second order is still scanned from the first element until the second element is the penultimate one.

The third order, from the first scan to the penultimate one.

......

Until only one element remains to be scanned.

The procedure is as follows:


#include <stdio.h>int main () {int arr[]={1,2,3,9,8,7,5,6,4};int i=0;int j=0;int len=sizeof (arr)/sizeof (arr[0]); for (i=0;i<len;i++) {for (j=0;j<len-i;j++) {if (arr[j+1]<arr[j]) {int temp=arr[j];arr[j]=arr[j+1];arr[j+1]= temp;}}} for (i=0;i<len;i++) printf ("%d", Arr[i]); return 0;}

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0059.gif "alt=" J_0059.gif "/> Simple and quick sort:

The first order, starting from the first element, selects the smallest element in the entire sequence, and the first element exchanges the position.

The second order, starting from the second element, selects the smallest, and second, swap position.

......

The procedure is as follows:

#include <stdio.h>int main () {int arr[]={1,2,3,9,8,7,5,6,4};int i=0;int j=0;int k=0;for (i=0;i<sizeof (arr)/ sizeof (Arr[0]) -1;i++) {k=i;for (j=i+1;j<sizeof (arr)/sizeof (Arr[0]); j + +) {if (arr[j]<arr[k]) k=j;} if (k!=i) {int temp=arr[i];arr[i]=arr[k];arr[k]=temp;}} For (i=0;i<sizeof (arr)/sizeof (arr[0]); i++) printf ("%d", Arr[i]); return 0;}


Bubble sort and simple selection 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.