D5- sorting algorithm (a) [the Java data structures and algorithms]

1. Sort sorting algorithm, also known as (sort Algorithm), the sorting process is a set of data sorted by the order specified

  1.1 sort of classification

  (1) Internal Sort: refers to all the data to be processed are added to the internal memory in order.

  (2) external sorting: data is too large to load into memory of all, it needs an external storage sort

  1.2 common sorting

  (1) internal sorting: Sort insert (direct insertion sort, Shell sort), selection sort (simple selection sort, heap sort), exchange sorting (bubble sort, quicksort), merge sort, radix sort

  (2) external sort

  1.3 algorithm complexity time - two ways to measure the execution time of an algorithm

  (1) statistical methods ex post - run first look at run time

    - To run on the performance of the algorithm design for evaluation, we need to actually run the program

    - statistics resulting time depends on the computer's hardware, software and other environmental factors

  (2) Method of estimating beforehand

    - be judged by analyzing the time complexity of an algorithm which algorithm better

  (3) Time Frequency

    - Time spent with the algorithm in a statement algorithm execution times proportional to, statements which algorithm to perform more often, it takes time and more. A number of times the algorithm execution statement statements is called frequency or time frequency, referred to as T (n).

  (4) time complexity

    - In general, the number of repetitions of the basic operation of the algorithm execution statement is a function of the problem size n, is represented by F (n), if an auxiliary function F (n), such that when n approaches infinity , T (n) / f (n) is not equal to the constant limit value is 0, then as f (n) is T (n) is a function of the same order. Denoted by T (n) = O (f (n), is a progressive time complexity of the algorithm, the time complexity for short)

    -T (n) different, but may be the same time complexity.

    - The method of computation time complexity:

      - 1 by a constant to replace all of the additive constant runtime

      - the number of runs a function of the revised, retaining only the highest order term

      - Removal of the highest order term coefficients

     - common time complexity

      - constant of the order O (1), of the order O (log 2 n-), linear order O (n), the linear order of O (nlog 2 n-), the square of the order O (n ^ 2), the cubic order O ( n ^ 3), k th order O (n ^ k), exponential order O (2 ^ n)

      - common algorithm time complexity ascending order above, the time complexity is increasing, the lower the efficiency of the algorithm execution

    - The average time complexity: refers to the case of all possible inputs appear with equal probability instance, the running time of the algorithm

    - worst case time complexity: time complexity are discussed generally time complexity in the worst case, the worst case time complexity is scared limit algorithm running on any of the input time instance

    - The average time complexity and the worst time complexity of the algorithm related to whether the same

Sorting Average time Worst case stability Additional space Remark
bubble O (N 2 ) O (N 2 ) stable O (1)

n hours better

exchange O (N 2 ) O (N 2 ) Unstable O (1) n hours better
select O (N 2 ) O (N 2 ) Unstable O (1) n hours better
insert O (N 2 ) O (N 2 ) stable O (1) Most good when ordered
Cardinal number O (log R B) O (log R B) stable O (n) B is true number (0-9), R is the radix (a ten hundred)
Shell O (nlogn) O(ns)1<s<2 Unstable O (1) s is selected packet
fast O (nlogn) O (N 2 ) Unstable O (nlogn) n large better
Merger O (nlogn) O (nlogn) stable O (1) n large better
stack O (nlogn) O (nlogn) Unstable O (1) n large better

  (5) space complexity of the algorithm

    - spatial complexity of the algorithm (Space Complexity) is defined as the storage space consumed by the algorithm, is a measure of the algorithm during operation of the temporary storage space occupied. Temporary work needs to occupy some algorithm to solve the problem of the number of units and the size of n relevant, it's as n increases, when n is large, it will take up more memory cells.

    - algorithm analysis focuses on is the time complexity, focus more on program execution speed on the user experience. Some caching products (Redis, memcache) and algorithms (radix sort) is essentially a space for time.

 

2. Bubble Sort (Bubble Sorting)

  2.1 Thought: by treating front to back ordering sequence , sequentially comparing the value of adjacent elements , then the switch if found to reverse the larger the value of the primary key of the element toward the rear from the front. Because the sorting process, the elements keep close to their location, when comparing a trip down not been exchanged, a sequence of ordered instructions, thus setting the flag FLAG is determined whether a by exchanging elements in the sorting process, thereby reducing unnecessary Compare (optimization can be written in the bubbling finished).

  Rule 2.2

  (1) a total size of the array for a cycle times -1

  (2) the number of times each trip ordered in decreasing

  (3) If it is found in a sort trip, the exchange did not happen once, can be ended prematurely bubble sort (optimization)

  2.3 Source Code

public  static  void bubbleSort ( int [] ARR) {
         // the TODO Auto-Generated Method Stub
         // bubble sort evolution, time complexity is O (n-2 ^) 
        Boolean In Flag = to false ; // define identification variable, indicating whether been exchanged, no switching is false, the exchange is to true 
        int TEMP = 0; // temporary variables 
        for ( int I = 0; I <-arr.length. 1; I ++ ) {
             // each pass are not assumed to be before the start of exchange 
            in Flag = to false ;
             for ( int J = 0; J <-arr.length. 1-I; J ++ ) {
                 // if the number is larger than the latter in front of the number, then the switch 
                if(ARR [J]> ARR [J +. 1 ]) { 
                    In Flag = to true ; // been exchanged 
                    TEMP = ARR [J]; 
                    ARR [J] = ARR [J +. 1 ]; 
                    ARR [J + 1'd] = TEMP ; 
                } 
            } 
            System.out.printf ( "% d of Run sort:", I +. 1 ); 
            System.out.println (of Arrays.toString (ARR)); 
            // trip is determined whether the end of the exchange, if a times are not exchanged, bubble sort is ended 
            IF (! In Flag) {
                 BREAK ; 
            } 
        } 
    }

 

 

3. Selection Sort (select sorting)

   3.1 is an internal sort, from the need to sort the data, an element is selected in accordance with specified rules, in accordance with the purpose of re-ordered after the exchange position.

   3.2 Thought: first from arr [0] -arr [n-1] selected minimum value, and arr [0] exchange, the second time arr [1] -arr [n-1] selected minimum value, and arr [. 1] .... exchange the n-1 times [n-2] -arr [n-1] selected from arr minimum, with arr [n-2] switching, by a total of n-1 times, to obtain a code ordered from small to large ordered sequence arrangement.

  3.3 Description

  (1) a total array size selection sort sorting wheel -1

  (2) per one sort, it is a cycle, assuming the current to this number is the minimum number, and then compares each of the number of the latter, if there are less than the current number of number, it is re-determined minimum number, and get subscript

  (3) When the last traversed the array to obtain the minimum number and the subscript round

  (4) exchange

  3.4 source code

 

package cn.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class SelectSort {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //int[] arr= {101,34,119,1};
        int[] arr=new int[80000];
        for(int i=0;i<80000;i++) {
            arr[i]=(int)Math.random()*8000000;
        }
        
        Date date1=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1Str=simpleDateFormat.format(date1);
        System.out.println("排序前时间"+date1Str);
        
        
        selectSort(arr);
        Date date2=new Date();
        String date2Str=simpleDateFormat.format(date2);
        System.out.println("排序后时间"+date2Str);
    }
    
    public static void selectSort(int[] arr) {
        
        int min = 0 ;
         int minIndex = 0; // minimum subscript 
        Boolean In Flag = to false ;
         for ( int I = 0; I <arr.length; I ++ ) { 
            min = ARR [I]; // assumed per se min 
            In Flag = to false ;
             for ( int J = I +. 1; J <arr.length; J ++ ) {
                 IF (min> ARR [J]) { 
                    min = ARR [J]; // find a [i + 1 ] minimum between -a [n-1], the minimum value of the subscript remember 
                    minIndex = J; 
                    in Flag= To true ; 
                } 
            } 
            IF (In Flag) {
                 // exchange 
                ARR [minIndex] = ARR [I]; 
                ARR [I] = min;                 
            } 
        } 
    } 

}

 

  

 

Guess you like

Origin www.cnblogs.com/ERFishing/p/11284299.html