Sorting data structure

Algorithm = {} var; 

// find the remaining bubble sort per maximum number of subsidence, sink is formed reached the maximum number of sequentially ordered 
@ Bubble sort is a simple sorting algorithm. It repeatedly visited the number of columns to be sorted, a comparison of two elements, if they put them in the wrong order switching over. The number of visits to the column work is repeated until there is no longer need to swap, that is to say the number of columns already sorted completed. The origin of the name of the algorithm is because the smaller elements will slowly through the exchange of "float" to the top of the columns. 

// 1.1 Algorithm Description 
// comparing adjacent elements. If the first is greater than the second, both of them exchanged; 
// do the same work for each pair of adjacent elements, from the beginning to the end of the first to the last pair, so in the final element should be the maximum number; 
// for repeating the above step for all elements, except the last one; 
// repeat steps 1 to 3 until the sorting is completed. 
= Function Algorithm.BubbleSort (ARR) 
{ 
   var len = arr.length; 

   for (var I = 0; I <-len. 1; I ++) 
   { 
       for (var J = 0; J <-len. 1-I; J ++) 
       { 
           IF (ARR [J]> ARR [J +. 1]) 
           { 
               var ARR TEMP = [J +. 1]; 
               ARR [J +. 1] = ARR [J];
               ARR [J] = TEMP; 
           }
       } 
   } 

   Return ARR; 
} 

// Sort selection (Selection - sort) is a straightforward sorting algorithm. It works: First, find the minimum unsorted sequence (large) element, sorted sequence stored in the starting position, and then continue to look for the smallest (large) elements from the remaining unsorted element and put it in the sorted sequence the end. And so on, until all the elements are sorted. 
2.1 Algorithm description // 
// n records may be sorted via direct selection n - 1 times obtained directly select the sort order result. The algorithm is described as follows: 
@ initial state: disordered region is R [1..n], ordered regions null; 
// Run i-th ordered (i = 1, 2, 3 ... n - 1) at the start, current ordered regions and disordered regions were R [1..i - 1] and R (i..n). The trip from the current sorting disordered zone - Minimum recording key selected R [K], it will exchange with the first record disordered region R, so that R [1..i] and R [i + 1 ..n) were changed to increase the number of records and the number of records a new order to reduce a new area of disorder; 
// n--. 1 end times, ordering the array. 

= Function Algorithm.SelectionSort (ARR) 
{ 
    var len = arr.length; 

    var minIndex, TEMP; 
    // find the minimum number into a first position, and then find the second small numbers put into the second position. 

    for (var I = 0; I <-len. 1; I ++) 
    { 
        minIndex = I;
 
        for (var J = I +. 1; J <len; J ++) 
        { 
            IF (ARR [J] <ARR [minIndex]) 
            { 
                minIndex = J; 
            } 
        } 

        TEMP = ARR [I]; 
        ARR [I] = ARR [minIndex]; 
        ARR [minIndex] = TEMP; 
    } 

    return TEMP; 
} 

algorithmic descriptions // insertion sort (insertion-Sort) is a straightforward sorting algorithm . It works by constructing an ordered sequence, for unsorted data, scanning in the sorted sequence from back to front, and find the corresponding insertion positions. 
3.1 Algorithm description // 
// Generally, insertion sort are implemented using in-place on the array. The algorithm is described as follows: 
// starting from the first element, which can be considered to have been sorted; 
// fetches the next element from the forward scan has been sorted in the sequence of elements; 
// if the element (sorted ) is greater than the new element, the element to the next position; 
// repeat step 3 until you find the sorted elements less than or equal to the new position of the element;
// the new element is inserted into the rear position; 
// repeat steps 2-5. 
= Function Algorithm.InsertionSort (ARR) 
{
    var len = arr.length; 

    var preIndex, Current; 

    for (var I =. 1; I <len; I ++) 
    { 
        preIndex = I-. 1; 

        Current = ARR [I]; 
        
        the while (preIndex> 0 && ARR [preIndex]> Current ) 
        { 
            ARR [preIndex +. 1] = ARR [preIndex]; 
            
            preIndex -; 
        } 

        ARR [preIndex +. 1] = Current; 
    } 

    return ARR; 
}

  

Guess you like

Origin www.cnblogs.com/ms_senda/p/11401845.html