The bubbling sort has three record values, two of which point to two objects that need to be compared, one pointing to the end value of the numeric value that needs to be sorted.In,in+1,out Public classArraybub {Private Longa[]; Private intNelems; PublicArraybub (intmax) {a=New Long[Max]; Nelems=0; } Public voidInsertLongvalue) {A[nelems]=value; Nelems++; } Public voiddisplay () { for(intj=0;j) {System.out.print (A[j]+" "); } System.out.println (); } //How to
Bubble SortBubble sort is to compare the adjacent two numbers each time, (from small to large) if the front number is larger than the number behind, then exchange, otherwise do not exchange;var arr = [11,12,14,9,10,99,22,7]; Array.prototype.bubbleSort = function () {for (var i = 0; i Quick Sort(1) The Selection datum (2) is less than the base number exists on the left, greater than the existence of the right (3) recursive entire functionfunction Qui
Package Day04;public class Shuzu_maopaopaixu_05 {/** bubble sort: The next two elements are compared if the first loop matches the conditional transposition: The most value appears in the last bit */public the static void main ( String[] args) {//TODO auto-generated method Stubint [] Arr={3,2,1,6,9,4};bubblesort (arr);p Rintarray (arr);} public static void Bubblesort (int. [] arr) {for (int. i=0;i 7. Bubble
Simple sorting (after learning to review in time, otherwise like not to learn the same, andSpend more time in the future)Bubble sort: small number upBubble sort (bubblesort) is a repeated visit to the sorted sequence,Compare two elements at a time, if they are in the wrong order, just put them onSwap them over. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.Principle:1. Compare adjace
: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 sort var
For the improvement of bubble sort, thanks to Morewindows (Microsoft's most valuable expert), to provide me with such a good idea, I just wrote a full Java version.
/** * * @author Fly/public class Bubblesort {//No thought write exchange sort//not bubble sort, compare not adjacent element public int[] Bubblesort (
Int[] a) {int size = A.length; for (int i = 0; i
Bubble Sort Optimization
Bubble sort IdeaThe inner loop number 22 is compared and exchanged, and the last number becomes the maximum numberOuter loop control cycle times, not the last one to participate in the exchange;
Bubbling improvementsIf the inner loop has not been exchanged once, the array is ordered and the break is direct;no need to recycle;
#include
first, bubble sort
Code implementation:
public class Test_bubblesort {public
static void Main (string[] args) {
int[] arr = {23,56,15,13,36};
Bubblesort (arr);
Print (arr);
}
Bubble sort public
static void Bubblesort (int[] arr) {for
(int i = 0;i
The time complexity of the algorithm is O (n^2), and the space complexity is O (1).
Second, select the sort
Code implementation:
public class te
001
#include
002
#include
003
int main ()
004
{
005
int l[11]={0,70,38,65,97,76,93,1,15,68,64}; 0 do not sort just as sentinels, or temporarily swap space for use.
006
int i=0;
007
printf ("Unordered pre-sequence: \ n");
008
for (i=1;i
009
{
010
printf ("%d",
first, the basic idea:
Bubble sort is a simple sort of commutative class. The basic idea is to scan the elements that are to be sorted from scratch, to compare the adjacent elements in sequence during the scan, and to move the elements of the key value back. After each trip, the element with the largest key value is moved to the end, and the position of the element is noted, and the next order is only required to compare to this location until all el
This paper implements eight commonly used sorting algorithms: Insert sort, bubble sort, select Sort, hill sort, quick sort, merge sort, heap sort and lst cardinal order.
First, the algorithm implementation file Sort.h, the code is as follows:
* * Implements eight common sorting algorithms: Insert sort, bubble sort, select sort, Hill sort * and quick sort, merge sort, heap sort and lst cardinal sort *
Let's take a look at some of the sorting methods commonly used in web effects, we mainly include the bubble Insert Hill sorting method, mainly for array operations.
----------some sort algorithms
//JS sorts using sort
Systemsort:function (Array) {
Return Array.Sort (function (A, b) {
return a-b;
});
},
Bubble sort
Bubblesort:function (Array) {
var i = 0, Len = array.length,
J,
1, with the third variable value exchange
int a = 1;int b = 2;int C = A;A = b;b = C;2. Exchange with addition and subtraction
A = a + B;b = a-b;A = A-b;OrA = A-b;b = A + B;A = b-a;3, in different or (XOR) method Exchange
A = a ^ b;b = a ^ b;A = a ^ B; codeproject on an article
The code is as follows
Copy Code
public enum Echangetype{Pre-and post-value exchange with third-party variable TempThridvar,Using A=a+b;b=a-b;a=a-b calculation method to Exchange before and
And in some scenes, the queue is indeed like a surprise, can bring good results, such as the use of timer, can simulate the time difference effect
Copy Code code as follows:
function Createdq () {
var dq = [], size = 0;
return {
Setdq:function (queue) {
dq = queue;
size = Queue.length;
},
Queue:function (FN) {
Size + +;
Dq.push (FN);
},
Dqueue:function () {
Size--;
return Dq.shift ();
},
Run:function (FN) {
var me = this, timer;
Timer = setinterv
__construct ($name, $age)
{
$this->age = $age;
$this->name = $name;
}
}
We can use another function Usort to specify the sort by age:
Usort ($arr, function ($a, $b) {
if ($a->age = = $b->age) {return
0;
}
Return $a->age > $b->age? 1:-1;
});
2, bubble sort
N numbers to sort, outer loop n-1, inner loop n-1-i, compare J and j+1, and swap
$arr = [3,1,7,9,5,4];
$n = count ($arr);
For ($i =0 $i
JS Common bubble sort, quick sort, insert sort code sharing
4.2.1 Bubble Sort Algorithm Introduction parsing: Compare adjacent two elements, if the previous one is larger than the latter, swap position.
In the first round, the last element should be the largest one.
Follow the steps one method to compare the adjacent two elements, this time because the last element is already the largest, so the last ele
Brief introductionThe bubble algorithm is a basic sort algorithm that repeats the comparison of the two adjacent elements in the array. If one element is larger (smaller) than the other, then the position of the two elements is exchanged. Repeat this comparison until the last element. This comparison will repeat the n-1, each trip to compare N-j times, J is already sorted the number of elements. Each comparison is able to find the largest or smallest
1, principle: compare Array[index] and array[index+1 from the first position of the array, and swap array[if ARRAY[INDEX] is greater than array[index+1] Index] and array[index+1] position, stop to array end; starts at the first position of the array, repeats the action above, and stops at the end of the array length minus one position; The starts at the first position of the array, repeats the action above, and stops at the end of the array length minus two positions; ..... 2, time complex
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.