Simple Algorithms and sorting

Source: Internet
Author: User

Today, we summarize the algorithm of simple sorting

"Custom Sort"
Look for a minimum number, then the number and then the other numbers in the array, if you find that the number is smaller than the number of the two number to swap positions, and then continue to find the next smallest number for the next round of comparison

var arr = [31,6,19,8,2,3];
function Findmin (Start,arr) {
var imin = Arr[start];
var iminindex = start;
for (var i = start+1;i<arr.length;i++) {
if (arr[i]<imin) {
Imin = Arr[i];
Iminindex = i;
}
}
return iminindex;
}
function Sort1 (arr) {

for (var i = 0;i<arr.length;i++) {
var iminindex = findmin (I,arr);
var car;
car = Arr[i];
Arr[i] = Arr[iminindex];
Arr[iminindex] = car;
}
return arr;
}
document.write (Sort1 (arr));

"Linear lookup": one-by-one to find

Do not repeat order
var arr = [0];
for (var i = 1;i<100000;i++) {
Arr[i] = Arr[i-1]+math.floor (Math.random () *4+1);
}

function Find1 (N,arr) {
for (var i = 0;i<arr.length;i++) {
if (arr[i]==n) {
return true;
}
}
return false;
}
Test performance
var T1 = new Date (). GetTime ();
for (var i = 0;i<10000;i++) {
var n = math.random () *10000;
Find2 (n,0,arr.length-1)
}
Alert (new Date (). GetTime ()-t1);

"Two-point search": keep dividing into two parts and finding them in parts
is a universal method, not necessarily the best, but a guaranteed method. (Divide and conquer law)

The middle value is added by two, the uniform left, and the rounding down

Do not repeat order
var arr = [12,17,23,34,45,76,89];
function Find2 (n,s,e) {
Boundary processing
if (s>e) {
return false;
}else if (s==e) {
if (arr[s]==n) {
return true;
}else{
return false;
}
}

var C = Math.floor ((s+e)/2);
if (arr[c]==n) {
return true;
}else{
if (N<arr[c]) {
Return Find2 (N,S,C);
}else{
Return Find2 (n,c+1,e);
}

}
}

Alert (Find2 (34,0,arr.length-1));//true false

"Boundary Processing"-----recursion, one level down.

Requires that the array is not repeated in order \
var arr=[12,23,34,45,56,67,78]
function Find2 (n,s,e) {
if (s>e) {
return fasle;
}else if (s==e) {
if (arr[s]==e) {
return true;
}else{
return false;
}
}
var C=math.floor ((s+e)/2);
if (arr[c]==n) {
return true;
}else{
if (N<arr[c]) {
Return Find2 (N,S,C);
}else{
Return Find2 (n,c+1,e);
}
}
}

Alert (Find2 (12,arr.length+1,78));

Application

"Find minimum Value"

var arr = [12,54,32,9,5,3,1,101,-100,-1000];
function Findmin (s,e) {
if (s>e) {
return [];
}else if (s==e) {
return Arr[s];
}else if (s = = e-1) {
if (Arr[s]<arr[e]) {
return Arr[s];
}else{
return arr[e];
}
}
var C = Math.floor ((s+e)/2);
var L = findmin (s,c);
var r = findmin (c+1,e);
if (l<r) {
return l;
}else{
return R;
}
}
Alert (Findmin (0,arr.length-1));

"Array de-weight"

var arr = [1,2,3,4,5,4,3,4,5,2,1,4,2,1,5,7];
function Findinarr (N,arr) {
for (var i = 0;i<arr.length;i++) {
if (arr[i]==n) {
return true;
}
}
return false;
}
function Removecopy (s,e) {
if (s>e) {
return [];
}else if (s==e) {
return [Arr[s]];
}else if (s = = e-1) {
if (Arr[s]==arr[e]) {
return [Arr[s]];
}else{
return [Arr[s],arr[e]]
}
}
var C = Math.floor ((s+e)/2);
var L = removecopy (s,c);
var r = removecopy (c+1,e);

for (var i = 0;i<r.length;i++) {
if (!findinarr (r[i],l)) {
L.push (R[i]);
}
}
return l;
}
document.write (Removecopy (0,arr.length-1));

"Array Sort"

var arr = [34,32,1,76,55,-100,99,101];
function Mysort (s,e) {
Boundary processing
if (s>e) {
return [];
}else if (s==e) {
return [Arr[s]]
}else if (s = = e-1) {
if (Arr[s]<arr[e]) {
return [Arr[s],arr[e]];
}else{
return [Arr[e],arr[s]];
}

}
1. In-between values
var C = Math.floor ((s+e)/2);
2. Sub-processing
var L = mysort (s,c);
var r = mysort (c+1,e);
var res = [];
while (l.length>0| | r.length>0) {
if (L[0]<r[0]) {
Res.push (L.shift ());
}else{
Res.push (R.shift ());
}
}
if (L.length ==0) {
res = Res.concat (r);
}else if (r.length ==0) {
res = Res.concat (l);
}
return res;
}
Call
document.write (Mysort (0,arr.length-1));


Bubble sort Bubblesort

Loop, take out two values at a time, 22 compare, if the next value is smaller than the current one, then swap position

The outer loop is the number of loops, and the inner loop is a 22 exchange comparison

var arr=[-122,-2,5,6,73,34,5,2];

function Bubblesort (arr) {
for (Var i=0;i<arr.length;i++) {
for (Var j=0;j<arr.length-1;j++) {
if (Arr[j]>arr[j+1]) {
var tmp=arr[j];
ARR[J]=ARR[J+1];
Arr[j+1]=tmp
}
}
}
return arr;
}

document.write (Bubblesort (arr));


"Quick Sort"-------quickSort

Take the number of the middle of the array, the middle number of the middle number is smaller than the median to the left, and the middle number is larger than the right, and then two times link up

function QuickSort (arr,s,e) {
Boundary Processing Engagement Process
if (arr.length==0) {
return [];
}

var C=math.floor ((s+e)/2);
var arrc=arr.splice (c,1);
var l=[];
var r=[];

for (var i=0; i<arr.length;i++) {
if (ARR[I]<ARRC) {
L.push (Arr[i]);
}else{
R.push (Arr[i]);
}
}
Return QuickSort (L). Concat (Arrc,quicksort (R));
}

var arr=[5,5,12,56,1,67,-1,-23-1];

document.write (QuickSort (arr,0,arr.length-1));

"Hash" hash array------JS commonly used structure
Add to

var arr=[];
arr.length=0;
var cont=0;

function Hash_add (n) {

var pos=n%arr.length;
When the space is low

if (Arr[pos]) {
while (Arr[pos]) {
cont++;
if (arr[pos]==n) {
Return
}else{
pos++;
if (pos==arr.length) {
pos=0;
}
}
}
Arr[pos]=n;
}else{
Arr[pos]=n;
}
Expansion at a time of insufficient space
if (cont==arr.length) {
D, wait, expand.
var Oldarr=arr;
arr.length=oldarr.length*2;
Arr=[];
for (Var i=0;i<oldarr.length;i++) {
Arr.push (Oldarr[i]);
count=0;
}
}
}

Hash_add ();

Simple Algorithms and sorting

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.