The first day learning algorithm ------- primary sorting algorithm

The so-called sequencing, is to make a bunch of records, which follow a certain size or certain keywords, increasing or decreasing line up operations. Sorting algorithm, it is how to make the recording method according to claim arrangement. We received considerable attention to sorting algorithms in many areas, especially in processing large amounts of data. A good algorithm can save a lot of resources. In various fields taking into account the constraints and specifications data, to get a good algorithm realistic, get through a lot of reasoning and analysis.

 

Sorting algorithm class template

——————————————————————————————————————————

public class Example{
    public static void sort(Comparable[] a){
    }
    private static boolean (lessComparable v,Comparable w){
        return v compareTo(w) < 0;
    }
    private static void exch(Comparable[] a, int i, int j){
        Comparable t = a[i]; 
        a[i] = a[j];
        a[] = t;
    }
    private static void show (Comparable[] a){
        //Printing in a single line array 
        for ( int I = 0; I <a.length; I ++ ) 
            StdOut.print (A [I] + "" ); 
        StdOut.printIn (); 
    } 
    public  static  Boolean IsSorted (the Comparable [] A) {
         // test whether the ordered array elements 
        for ( int I =. 1; I <a.length; I ++ _)
         iF (less (A [I], A [I -. 1])) return  to false ;
         return  to true ; 
    } 
    public  static  void main (string [] args) {
         // read from the standard input string, sort them output
        String[] a = In.readStrings();
        sort(a);
        assert isSorted(a);
        show(a);
    }
}

——————————————————————————————————————————

Selection Sort

  One of the simplest sorting algorithm is this: First, find the elements of an array of small, and secondly, it will be the first element of the array and swap positions (if the first element is the smallest element and then it own exchange). Again, find the smallest element in the rest of the elements, it will be the second element of the array exchange position. And so forth, until the entire array is sorted. This method is called selection sort, since it is constantly selecting the smallest among the remaining elements.

  Overall, the selection sort is a very easy to understand and implement simple sorting algorithm, it has two very distinct characteristics.

  Running time and independent of the input . To find the smallest element array and scan it again does not provide any information for the next pass scans in some cases of this nature are shortcomings in the implementation of the computer, because the use of selection sort of people may be surprised to find that there has been a order or move an array. This sorting algorithm select one primary sort key all equal time array and a random array of elements of the array actually used as long! As we will see, other algorithms will be more adept at using the initial state of the input.

  Data movement is the least . Each exchange will change the value of the two array elements, thus selected for sorting and cross-exchanging the N-th selection ordering does not change the number and size of the array is a linear relationship. We will study any other algorithms do not have this feature (most of the growth in the number and the order in which the elements are already logarithmic or linear square level).

Selection Sort Code

——————————————————————————————————————————

. 1  public  class Selection {
 2    pubic static  void Sort (the Comparable []) {
 . 3          // a a [] in ascending order 
. 4          int N = a.length;        // length of the array 
. 5          for ( int I = 0; I <N; ++ I ) {
 . 6              // the a [i] to a [i + 1..N] smallest switching element 
. 7              int min = I;         // index of the smallest element 
. 8              for ( int J = I +. 1; J < N; J ++ )
 . 9                  IF (less (A [J], A [min])) min = J;
 10              EXCH (A, I, min);
. 11          }
 12 is      }
 13 is  }
 14  // less (), EXCH (), IsSorted () and main () and other methods, see "class templates sorting algorithm"

——————————————————————————————————————————

Insertion Sort

  Usually people finishing method is a bridge to a, each card will be inserted into the appropriate position other cards have been ordered in. In the implementation of the computer in order to make room for the element to insert all the elements we need to rest before insertion are moved to the right one. This algorithm is called insertion sort.

  As with selection sort, all the elements of the current index on the left are ordered, but their final position is uncertain, in order to make room for smaller elements, they may be moved. However, when the index reaches the right end of the array, sort the array is complete.

  And selecting a different sort, the time required for insertion sort depends on the initial input sequence of elements. For example, the elements of which have a large and ordered (or nearly ordered) array will be sorted to the sorting is much faster than an array in random order or in reverse order array.

Insertion sort codes

——————————————————————————————————————————

public  class Insertion { 
    pubic static  void Sort (the Comparable []) {
         // the a [] in ascending order 
        int N = a.length;        // length of the array 
        for ( int I =. 1; I <N; I ++ ) {
             // the a [i] is inserted into a [i-1] .. among the 
            for ( int J = I; J> 0 && less (A [J], A [-J. 1]); J, ) 
                EXCH (A , J, J -1 ); 
        } 
    } 
} 
// less (), EXCH (), IsSorted () and main () and other methods, see "class templates sorting algorithm"

——————————————————————————————————————————

Shell sort

  In order to demonstrate the value of nature's primary sorting algorithm, then we will learn a quick sort insertion sort insertion sort algorithm is very slow for large-scale arrays out of order, because it will only swap adjacent elements, so elements can only be a little by little from one end to the other end of the array. For example, if the smallest element in the right end of the primary key of the array, to be moved to its correct position requires N-1 or moved. Shell sort order to speed simply improved insertion sort, not adjacent switching elements to sort the local array, and finally with the partial insertion sort ordered array is sorted.

  Hill sorting idea is that the elements of the array at any interval h are ordered such an array is an ordered array of words referred to as h, a h h ordered array is an ordered array of mutually independent braided when an array composed together (see Figure 2.12) during the ordering, if h is large, we can move elements to faraway places, to achieve a smaller h ordered convenient way to create this for any order h 1 at the end of the sequence, we are able to sort the array. This is the sort Hill. 23 algorithm uses the sequence 1/2 (32-1), starts to decrease from 1 N3. We call this sequence is called increasing sequence. Real-time calculation algorithm 23 increments its sequence, another way is to increment the sequence is stored in an array.

 

 h=4

  L  E  E  A  M  H  L  E  P  S  O  L  T  S  X  R

  L  ——————  M  ——————  P  ——————  T

     E  ———————  H  ——————  S  ——————  S

       And L ------- ------ ------ O X

           A ------- E ------- L ----- R

           a sorted array h h i.e. an array of ordered sub-array of
  a method implemented for sorting Hill h each, independently of h sub-array sorted by insertion sort. But because the sub-arrays are independent of each other, a more simple method is to (greater than its elements to the right one space) before each element of the array will be switched to the sub-h- is larger than its elements. The distance in the code motion elements only need to 1 by insertion sort to h. Such implementations Hill sorting is converted to a similar but different insertion sort incremental process.
Hill sorting more efficient because it weighed the scale and order sub-array. Sort the beginning of each sub-array is very short, sub-arrays are partially ordered after ordering, both of which are very suitable for insertion sort. Degree orderly array of sub-section depends on the choice of increasing sequence. Hill ordered a thorough understanding of the performance is still a challenge. In fact, the algorithm 2.3 is the only thing we can not accurately describe their method for sorting an array of performance characteristics of the disorder.

Hill sorting code

——————————————————————————————————————————

public  class Shell {
     public  static  void Sort (the Comparable [] A) {
         // the a [] in ascending order 
        int N = a.length;
         int H =. 1 ;
         the while (H <n-/. 3). 1 H + = 3H;     // 1,4,13,40,121,364,1093 ... 
        the while (h> =. 1 ) {
             // array becomes h ordered 
            for ( int I = h; I <N; I ++ ) {
             / / the a [i] is inserted into a [i - h], a [i - 2h], a [i - 3 * h] ... in 
                for ( int J = I; J> = H && less (A [J], A [J - H]); J - =  H)
                    EXCH (A, J, J- H); 
            } 
                H = H /. 3 ; 
        } 
    } 
} 
// less (), exeh (), IsSorted () and main () and other methods, see "mode sorting algorithm class"

——————————————————————————————————————————

 

Guess you like

Origin www.cnblogs.com/algorithms-learn-note/p/10974601.html