Bubble sort with quick sort array out of bounds?

Source: Internet
Author: User
Tags access properties

Bubble Sort principle:

1. Comparison of the first and second items;
2. If the first item should be followed by the second item, then the exchange order of the two;
3. Comparison of the second and third items;
4. If the second item should be followed by the third item, then the exchange order of the two;
5. And so on until the sorting is completed;

Example Description:

Sorts the array [3, 2, 4, 5, 1] in order from small to large:

1.3 should after 2, so exchange, get [2, 3, 4, 5, 1];

2.3, 4 order unchanged, 4, 5 also unchanged, Exchange 5, 1 get [2, 3, 4, 1, 5];

3. The end of the first traversal, the last item in the array will not change in the correct position, so the next traversal can exclude the last item;

4. Start the second traversal, and the final result is [2, 3, 1, 4, 5], excluding the last two to perform the next traversal;

5. The third traversal results are [2, 1, 3, 4, 5]; 6. Finally get [1, 2, 3, 4, 5], sort end;

 function   Sort1 (arr) { for  (var  i = 0; i < arr.length; I ++//  Arr.length-i meaning: Each traversal removes the good elements that are sorted later  for  ( j = 0; J < Arr.length-i; J ++ if  (Arr[j] > Arr[j+1 var  temp = Arr[j];          ARR[J]  = Arr[j+1];        Arr[j  +1] = temp;  }}}  return   arr; }

The above code, when I first saw it, had a little doubt, notice if () the arr[j+1 inside of it, such as the array arr = [2, 1, 3, 5] if (Arr[j] > Arr[j+1])

So when J=3 j+1 is arr[4] then the array will not be out of bounds?

In fact, in JS we know that the array is actually an object created by the new Array () function, and when we access properties that do not exist in the object

such as OBJ.A or arr[6] (this index is the property of the array)

The value is undefined

Then, in the IF () statement, the size of the number type, the undefined hermit converts to the # type is Nan, and the definition of Nan in JS is not equal to any numeric value including itself,

So if () evaluates to false the following assignment statement will not execute without error.

Because of the specificity of the JS language this cross-border is no problem, but colleagues tried to use C # is unable to execute.

Bubble sort with quick sort array out of bounds?

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.