The bubble sort method in JavaScript

Source: Internet
Author: User

in development, it is often necessary to arrange a set of data in an orderly manner, so master several or even more sorts . algorithm is absolutely necessary.

here to a simpler algorithm in the sorting algorithm is introduced: bubble sort.

Try to compare learning bubbles by trying to sort by the simplest idea.

has an array whose size is 10 elements (int str[10]) and the data within the array is unordered. It is now required that we program this unordered array into an array of small to large orders (starting with subscript 0) .

according to the requirements of the topic, there is no doubt that the correct result should be like this: 1 2 3 4 5 6 7 8 9 To do so, the simplest and most straightforward way to think about is to do a contrast exchange.

  • First, put Ten The smallest number in the number is placed in the subscript 0 the location (str[0])
  • by adding Subscript is 0 the number (str[0]) with the rest of the remaining 9 The number of the smallest one can be obtained by making a comparison exchange (placing fewer people in the position labeled 0 ).
  • Ten when the smallest number is determined, the next thing you need is the rest. 9 The one with the smallest number.
  • because a minimum number has been determined, so don't move . Str[0] , directly from Str[1] start by swapping with the remaining 8 numbers to find out where the smallest of the 9 numbers is placed on the subscript 1 (str[1]) .
  • continue to follow this line of thought to turn these 10 numbers into an orderly one ( from small to large ) array.

Code:

  1. #include <stdio.h>
  2. void swap (int *a, int *b); Exchange two numbers
  3. int Main ()
  4. {
  5. int str[10];
  6. int I, J;
  7. Initialize array to 10 9 8 7 6 5 4 3 2 1
  8. for (i = 0; i < i++)
  9. {
  10. Str[i] = 10-i;
  11. }
  12. Sort, from a[0] start platoon, small to large
  13. for (i = 0; i < i++)
  14. {
  15. for (j = i + 1; j <; J + +)
  16. {
  17. if (Str[i] > Str[j])
  18. {
  19. Swap (&str[i], &str[j]);
  20. }
  21. }
  22. }
  23. 10-digit output
  24. for (i = 0; i < i++)
  25. printf ("%d\n", Str[i]);
  26. return 0;
  27. }
  28. void swap (int *a, int *b)
  29. {
  30. int C;
  31. c = *a;
  32. *a = *b;
  33. *b = C;
  34. }

This method is more easily thought of as an implementation method. But there is a shortage: the smaller number that is originally in front is swapped to the back.

For example:

Start: 9 4 5 6 8 3 2 7 1 (the subscript is 0~9 from left to right respectively) follow the procedure above to exchange

First time: 4 9 5 6 8 3 2 7 ten 1

Second time: 4 9 5 6 8 3 2 7 ten 1

。。。 : (No Exchange)

Fifth time: 3 9 5 6 8 4 2 7 1

Sixth time: 2 9 5 6) 8 3 4 7 1

。。。 : (No Exchange)

Tenth time: 1 9 5 6 8 3 4 7 2  

As can be seen, the original small number is in the front, after a round of exchange and put in the back

So how to solve this deficiency? You can use bubble sorting.

What is bubble sort?

You can understand this: (from small to large sort) There are 10 different sizes of bubbles, from the bottom to the lower bubbles gradually upward, so after traversing once, the smallest bubbles will be raised to the top (subscript 0), and then from the bottom up to this rise, Loop until 10 bubbles are of an orderly size.

in bubble sort in which The most important idea is a 22 comparison, which will raise the two less.

Code:

  1. #include <stdio.h>
  2. void swap (int *a, int *b);
  3. int Main ()
  4. {
  5. int Array[10] = {15, 225, 34, 42, 52, 6, 7856, 865, 954, 10};
  6. int I, J;
  7. for (i = 0; i < i++)
  8. {
  9. Each rising from bottom to top
  10. for (j = 9; j > i; j--)
  11. {
  12. if (Array[j] < array[j-1])
  13. {
  14. Swap (&array[j], &array[j-1]);
  15. }
  16. }
  17. }
  18. for (i = 0; i < i++)
  19. {
  20. printf ("%d\n", Array[i]);
  21. }
  22. return 0;
  23. }
  24. void swap (int *a, int *b)
  25. {
  26. int temp;
  27. temp = *a;
  28. *a = *b;
  29. *b = temp;
  30. }


The bubble sorting algorithm will only reduce the number of progressive push-ups, and will not cause the article to be said earlier.

The bubble sort method in JavaScript

Related Article

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.