{9 for(intI=1; i)Ten { One inttmp=i; A for(intj=i+1; j) - { - if(a[j]a[tmp]) thetmp=J; - } - intt=A[i]; -a[i]=a[tmp]; +a[tmp]=T; - } + } A intMain () at { -Std::ios::sync_with_stdio (false); -Cin>>N; - for(intI=1; i) -Cin>>A[i]; - Select_sort (a); in for(intI=1; i) -cout" "; tocoutEndl; + return 0; -}There are two ways to choose a sort, the first is to constantly go for t
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
" "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
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
: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
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.
him, This will find the smallest number in the array. Then find the second small number in the array, let him swap the value with the second element in the array, and so on.Insert SortStarting with the first element of the array, a[0], insert the next element a[1] into the front or back of a[0], and continue the process. Insert A[i] into the appropriate position in the sorted a[0]~a[i-1] every time1 Public classsort{2 Public Static voidMain (string[] args) {3 intarray[]={12,76,34,
First, bubble sortBubble sort (Bubble sort) is a relatively classic sorting algorithm.This is called "bubble sort" because the larger (descending) or smaller (ascending) adjacent elements in the
Bubble sort, select sort, and insert sort complexity are all points of magnitude, put together to say.This article introduces some of the better materials for learning these three sorting methods. Bubble sort See "learning JavaScr
Objective
The reason to put the bubble sort and select sort together is that the implementation code of the two is very similar, and is the most basic sort, very easy to understand and implement. Of course, if it's just to tell the two ways of sorting, there's no need to write this blog post. As with the previous blog
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.