The bubble sort of the iOS algorithm (iv)

Source: Internet
Author: User

The basic idea of bubble sorting: (in ascending order, for example) an array of n elements is in principle ordered n-1. For each lying sort, start with the first number and then compare the size of the previous number to the next. If the previous number is larger than the last one, it is exchanged. After such a round, the largest number will appear as the last array element. In the second round, the last number is removed, and the number of the first n-1 is followed by the above steps to find the maximum number, which will be called the second-lowest array element ... after the n-1 round, the sorting was completed.

/* Bubble Sort Analysis Detailed:

Raw data: 28, 30, 19, 2, 23

First trip:

First time: 28, 30, 19, 2, 23

Second time: 28, 19, 30, 2, 23

Third time: 28, 19, 2, 30, 23

Fourth time: 28, 19, 2, 23, 30

Second trip:

First time: 19, 28, 2, 23, 30

Second time: 19, 2, 28, 23, 30

Third time: 19, 2, 23, 28, 30

Third trip:

First time: 2, 19, 23, 28, 30

Second time: 2, 19, 23, 28, 30

Four trips:

First time: 2, 19, 23, 28, 30

*/

Comparison of n elements n-1 trip

Times per trip = number of elements in array-number of times

Code implementation:

Randomly generate a set of 20 elements array values range from [20, 40]

int a[] = {0};

printf("a randomly generated array containing 20 elements: \ n");

For (int i = 0; i < ; i++) {

A[i] = arc4random ()% (+- 1 + ) + 20; //arc4random generating random numbers

printf ("%d", A[i]);

}

for (int i = 0; i < 1; i++) {//Compare the number of trips

for (int j = 0; J < - 1-i; j + +) {//times per trip

if (A[j] > a[j + 1]) {

int temp = a[j];

A[J] = a[j + 1];

A[j + 1] = temp;

}

}

}

printf("\ n bubble sort, produces a set of arrays from small to large order: \ n");

For (int i = 0; i < ; i++) {

printf ("%d", A[i]);

}

The bubble sort of the iOS algorithm (iv)

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.