Bubble sort
Bubble sort (Bubble sort) is the simplest of the sorting algorithms
It's usually when we get to know a new language. Practiced hand use
Today is no exception, although written in C # countless times the
The bubble sort should be the first lesson for children who are just touching the sort:(Although this sort is time consuming)The principle of bubble sort:We assume this is from small to large (from big to small)The process of bubbling the
1. Bubble sortBasic idea: For each order of the trip, 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 that, the largest number will appear at the bottom of the last position.In the second round, the last number is removed, the number of the first n-1 is followed by the above steps to find the maximum number, which will appear in the pen
second comparison, and the third comparison is You only need to compare the number other than the last two numbers, and so on ... That is, not a trip to compare, each trip less than once, to a certain extent, reduce the amount of the algorithm.Code implementation:public class Bubble { public static void Main (String [] args) { int [] arr={1,7,9,11,158,97,666}; System.out.print ("Array before sorting:"); for (int num:arr) {
be compared, and a maximum number is also found after the number of the second comparison, and the third comparison is You only need to compare the number other than the last two numbers, and so on ... That is, not a trip to compare, each trip less than once, to a certain extent, reduce the amount of the algorithm.In terms of time complexity:1. If our data is in the right order, we need to take a trip to complete the sequencing. The required number of comparisons and the number of records moved
The common sorting algorithms are bubble sort, Merge sort, Quick sort, and so on, all of the Basic Law ideas of sorting are to reduce the size of an infinite scale of data through the algorithm, to guide the final completion of the sorting.Here to share the buuble sort algor
These few very basic sorts are very useful, and I've re-organized the code1#include 2#include 3 4 using namespacestd;5 6 voidBouble_sort (int* Arry,intLenth)//Bubble Sort7 {8 inti,k;9 Ten intFlag =0; One A for(i = lenth-1; I >=0; i--) - { - for(k=0; k) the { - if(Arry[k] > arry[k+1]) - { -Swap (arry[k],arry[k+1]);//If there is a larger case than the previous one, exchange two digits +
The examples in this article describe the way the go language implements bubble sort, select sort, fast sort, and insert ordering. Share to everyone for your reference. The specific analysis is as follows:
The algorithm is the soul of the program, and the sorting algorithm is the most basic algorithm. There are a numb
The algorithm is the soul of the program, and the sorting algorithm is the most basic algorithm. There are many kinds of sorting algorithms, this article introduces 4 sorting algorithms: Bubble sort, select sort, quick sort and insert sort, take small to large as an example.
Basic concepts:
The basic concept of bubble sort (bubblesort) is to compare the number of adjacent two digits, place the decimal number in the front, and put the large numbers behind. On the first trip: first compare the 1th and 2nd numbers, put the decimal number before the big number put. Then compare the 2nd and 3rd numbers, put the decimal places before the large number, so continue until the last two,
Bubble Sort Idea:
The small always take the big forward and always take the back. Steps:
1. Compare adjacent elements, if the first one is larger than the second, just swap him for two. 2. In this way to the No. 0 data to the first N-1 data traversal, the largest data sank to the N-1 position 3.n=n-1, if N is not 0 to repeat the previous two steps, or sorting completed. Let's take a look at the results of t
A simple example of a bubble sort function written in PHP, and a php bubble sort function
A question about algorithms that I encountered a few days ago asked me to sort an array using the PHP language. I wrote a function using the bubbl
Cocktail sort (double bubble sort, stir sort or ripple sort)
Using System; Using System.Collections.Generic; Using System.Linq; Using System.text;namespace Sort {class Cocktailsorter {private static int[] MyArray;
This is a creation in
Article, where the information may have evolved or changed.
The algorithm is the soul of the program, and the sorting algorithm is the most basic algorithm. There are many kinds of sorting algorithms, this article introduces 4 sorting algorithms: Bubble sort, select sort, quick sort and insert
In my previous blog, "single linked list (not the lead node) to achieve bubble sort" to achieve the not the lead node of the single list of bubble sort, breaking the common use of the array to bubble the convention. Then the leading node of the linked list is more commonly u
Bubble sort: This is a good understanding of two two, and the code is better written.Its principle is the adjacent two two comparison, if the previous number is larger than the back, then the exchange, it can be compared to a time when the maximum number of a single, and then loop, each outer loop within the number of cycles less than the original. #region Bubble
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.