# Using C + + basic algorithm to achieve 10-digit ordered _c language

Source: Internet
Author: User

Bubble Sort Method
Principle:
It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted.

The bubble sort algorithm works as follows:
1. Compare adjacent elements. If the first one is bigger than the second one, swap them both.

2. For each pair of adjacent elements to do the same work, from the beginning to the end of the first pair. At this point, the final element should be the largest number.

3. Repeat the above steps for all elements except the last one.

4. Continue to repeat the steps above for fewer elements at a time until no pair of digits need to be compared.

Sample code:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
In ascending order
int a[10]={15,13,2,3,6,5,88,-3,30,40};
int i,j,t;
for (i=0;i<9;i++) {
For (j=0;j< (9-i); j + +) {
if (A[j]>a[j+1]) {
T=A[J+1];
A[J+1]=A[J];
a[j]=t;
}
}//through each cycle, sinking a maximum number of
}//a 10-digit number, sinking the 9 maximum number, you can sort the
for (i=0;i<10;i++) {
cout<<a[i]<< ' t ';
}
cout<<endl;
return 0;
}

Analysis: Through 22 comparison, the first order, will be the largest number of 88 to the last a[9] .... Nineth trip, a[1]=2, and then sort of finished

Select a Sort method
Principle:
The basic idea of choosing a sort is to select the smallest key in a n-i+1 (i=1,2,...n-1) record as the first record in an ordered sequence.

The I-trip simple choice sort refers to the smallest record in the n-i+1 record, and the exchange with the first record, by comparing the N-i key word. A i-1 comparison is required until all records are sorted. For example, when I go to the first selection, select the minimum number of keywords in the current candidate record and exchange it with the first record.

Sample code:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
In ascending order
int a[10]={15,13,2,3,6,5,88,-3,30,40};
int i,j,t,k=0;
for (i=0;i<9;i++) {
K=i;
for (j=i+1;j<10;j++) {
if (A[j]<a[k]) {
K=j;
}
}
T=A[K];
A[k]=a[i];
a[i]=t;
}
for (i=0;i<10;i++) {
cout<<a[i]<< ' t ';
}
cout<<endl;
return 0;
}

Related Keywords:
Related Article

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.