" "Bubble Sort It repeatedly visited the sequence to sort, comparing two elements at a time, swapping them out if they were in the wrong order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the larger element will slowly "float" through the switch to the top of th
Here we introduce some of the commonly used sorting methods, sorting is a programmer's basic skills, so-called sorting is a set of data, in a certain order of the process.Charging efficiency lookBubble sorting Internal sorting methodExchange-Type sorting methodBubbling methodBasic idea:Bubble Sort MethodCase:
123456789101112131415161718192021st2223242526
Simple.$arr =array (0,5,-1);Now let's wrap the function as a function and use
First, overview: The previous blog describes common simple algorithms: bubble sort, select sort, and insert sort. This article describes the Advanced sorting algorithm: Quick sort and merge sort. Before we begin to introduce the
Bubble Sort :The data in the array, in turn, compares the size of the adjacent two numbers.If the previous data is larger than the subsequent data, the two numbers are exchanged.Time complexity O (n^2)1 functionBubble (array) {2 vartemp;3 for(vari=0; i){4 for(varj=0; j){5 if(arr[j]>arr[j+1]){6temp = arr[j+1];7ARR[J+1] =Arr[j];8arr[j]=temp;9 }Ten }console.log (arr);
1. Direct Ordering: public static void selectsort (int [] arr) {
for (
int x = 0; x for (int y = x + 1; y if (Arr[x] > Arr[y]) { int temp = Arr[x] ; ARR[X] = Arr[y]; Arr[y] = temp; } } } }
Description: Double for loop, the first number of the array a[0] and all subsequent numbers are compared, get the smallest number, and then the second number a[1] and all the subsequent numbers are compared
The efficiency is low, the time complexity should be O (n*n) bar.
Although bubbling is a relatively simple sort, it is helpful to write it again or to help you to understand and remember deeply.
/*============================================================================= # # FileName:BubbleSort.cpp # Desc: Bubble Sort # # # Author:yulu # email:187373778@qq.co
First, bubble sort:
1, Algorithmic thinking:
For the data to be sorted, compare the two adjacent numbers from top to bottom and adjust them, moving the largest number downward and the smaller number upward. That is: Each trip compares adjacent two data elements in turn, placing the smaller numbers on the left, looping the same operation until all the data elements to be sorted are finished.
2. Example Analy
public class Directrank {public static void Main (string[] args) {Directrank aa=new Directrank ();try {Aa.judge (args);} catch (Testdefinexception E2) {E2.printstacktrace ();}Integer[] A=new integer[args.length];try{for (int i=0;i{System.out.print (args[i]+ "");A[i]=a[i].parseint (Args[i]);//Convert the read-in string type to Intgr}System.out.println ();}catch (NumberFormatException e){System.out.println ("\ n input data is incorrect \ n");}catch (NullPointerException E1){e1.getmessage ();}Aa. R
{
/**
* Sort results with enhanced for loop output
* * Public
static void Main (string[] args) {
int[] a = {2, 4,
a. Arraydemo.bubblesort (a);
for (int b:a) {
System.out.print (b + "");
}
}
*
* bubble sort function, defined as static convenience,
* is also a way to define the tool class in development * * Public
:200px;
Text-align:center;
margin-left:100px;
margin-top:20px;
font-size:20px;
}
Then the main function is realized.
Script.js window.onload = function () {var btn = document.getElementById ("resultbtn"); Result Output button var Inputnum = document.getElementById ("number"); Digital input box var resultlbl =document.getelementsbytagname ("label");
The result shows the label Var I,j,temp; Bubble
Bubble sort (Bubble sort) basic idea: 22 Compare the keywords of adjacent records, if the reverse order is exchanged until there are no reversed records. The time complexity is O (N2).The basic idea of simple Selection sort is to select the smallest record of a keyword from
the address, take this value in turn to compare, when the value is found smaller than it, change the address in the Indexleast, replaced by a small address number, It is then exchanged with the address of the numeric value that is compared, and after the exchange, it continues to be compared with the value in the Indexleast address.template Bubble Sort:To sort a sequence with n elements in ascending order
Today, what we bring to you is the exchange sort.First, let's take a look at what is called swap sequencing. The so-called exchange, which is based on the comparison of the two record key values in the sequence of the position of the two records in the sequence, the characteristics of the Exchange order is: a record of large key values to the end of the sequence of movement, the key value of a small record to the front of the sequence movement. so next, let's take a look.
elements to be sorted are arranged. Selecting sorting is an unstable sorting method.
The sorting is unstable. The algorithm complexity is O (n ^ 2 ).
Program Implementation example
Sort arrays from small to large
Public void choicesort (INT [] array)
{
Int min;
For (INT I = 0; I
{
Min = I;
For (Int J = I + 1; j
{
If (list [J]
Min = J;
}
Int T = list [Min];
List [M
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.