JAVA simple data structures and algorithms sort 3-

Two basic operations are ordered comparison and exchange . There are moved in the insertion sort.

Bubble sort: any two neighboring elements, if the number in front of a large number of small, then the switch;

  Each trip traversal of a maximum number to the end of the sequence, a total of N-1 times to traverse.

  If you do not finish the trip after the exchange element, then the sequence has been ordered.

  public void bubbleSort(){

    int out,in;

    for(out=array.length-1;out>0;out--){

      for(in=0;in<out;in++){

        if(arrry[in]>array[in+1]){

          swap(array[in],array[in+1]);

        }

      }

    }

  }

Bubble sort efficiency:

  

 

 

   

 

 Selection sort: a first element selected from the smallest element to be sorted, stored at the start of the sequence;

      Then look from the remaining unsorted element to the smallest element, is then placed at the end of the sorted sequence.

      And so on.

  public void selectSort(){

    int out,in,min;

    for(out=0;out<array.length-1;out++){

      min=out;

      for(in=out+1;in<array.length;in++){

        if(arrry[in]<array[min]){

          min=in;

        }

      }

      swap(array[out],array[min]);

    }

  }

 

 

 Select the sort efficiency:

  Faster than bubble sort selection sort, both the number of comparisons is the same: N * (N-1) / 2, but the choice of the number of exchanges for sorting O (N) is less than the bubble sort.

Insertion sort: each element to be sorted into the correct position on the sorted sequence.

    Local order: local order between the elements are in sequential order, but the final position of these elements has not been determined.

    Because when unsorted element when inserted therein, the position variation thereof occurs.

  

  public void insertSort(){

    int out,in;

    for(out=1;out<array.length;out++){

      int temp = array [out]; // tag of the element out

      in = out;

      while (in> 0 && array [in-1]> = temp) {// forward from a starting position mark, the mark element larger than the shift element

        array[in]=array[in-1];

        in--;

      }

      array [in] = temp; // put out into the marker elements it is smaller than the back of the element - the vacancy

    }

  }

  Insertion sort Efficiency: twice as fast as bubbling, slightly faster than selection.

  

 

 summary: 

    The simplest bubble sort, but the worst efficiency.

    

    Insertion sort of the three most widely used sorting algorithm. In most cases, when a small amount of data or substantially when ordered, insertion sort best.

       Bubble sort is stable, unstable selection sort, insertion sort is a stable sort.

       The time complexity of the above three algorithms is O (N ^ 2).

    More than three algorithms are "in place" to complete the order, in addition to the initial array requires little additional memory space, only need an extra variable to temporarily store data items of exchange.

Guess you like

Origin www.cnblogs.com/lsh0908/p/11755484.html