1. Select Sort
(1) Basic idea: each time (for example, I, i=1,2,..., n-2) from the back n-i the data elements to be sorted out the smallest element of the keyword, as an ordinal element sequence I element.
(2) Example analysis
2. Insert Sort
(1) Basic idea of inserting sort
When inserting an I (i≥1) data element, the previous v[0],v[1],..., v[i-1] has been sequenced, and the keyword of v[i] is compared to the keyword v[i-1],v[i-2],..., v[0] , where the position is found and V [i] Insert , the object in its original position moves backwards .
(2) Example analysis
Implementation of the "programming Experiment" selection sort (sort::select) and insert sort (sort::insert)
Sort.h
#ifndef Sort_h#defineSort_h#include"Object.h"namespacedtlib{classSort: Publicobject{Private: //privatized constructors, assignments, copy constructs, and moreSort (); Sort (Constsort&); Sort&operator=(Constsort&); Template<typename t>Static voidSwap (t& A, t&b) {T temp (a); A=b; b=temp; } Public: //Select Sort (O (n*n), unstable)Template <typename t>Static voidSelect (T array[],intLenBOOLMin2max =true) { for(intI=0; i<len-1; i++){ //The first trip. The ordered area is [0,i-1], the unordered area is [I, len-1]//1. Find the first element of the unordered zone: Array[i]//2. Find the minimum value from the unordered area intmin = i;//assumption: The first element of the smallest element in an unordered region for(intj=i+1; j<len; J + +){ if(Min2max?) (Array[j] < array[min]): (Array[j] >Array[min])) {min=J; } } //3. Swap the position of the first and smallest values in the unordered area, placing the minimum value in the header of the unordered area if(I! =min) {Swap (Array[i], array[min]); } } } //Insert Sort (O (n*n), stable)Template <typename t>Static voidInsert (T array[],intLenBOOLMin2max =true) { //1. The initial state ordered area is [0], the unordered area is [1..n-1] for(intI=1; i<len; i++) {//Note that starting with 1, because you want to insert each element of an unordered area into an ordered area//ordered area [0, I-1], unordered zone [I, n-1]//2. Remove the first element of the unordered zone and compare it from the back of the ordered area to the frontT e = array[i];//unordered area first element intK = i;//K Records where e should eventually be placed. for(intj=i-1; (j>=0) && (Min2max? (E<array[j]): (E>array[j]); j--){ //3. Edge-Compare move element, E final position recorded in Karray[j+1] = Array[j];//move A[j] back oneK =J; } //4. Place e down if(k! =i) {Array[k]=e; } } }};}#endif //Sort_h
Main.cpp
#include <iostream>#include"Sort.h"using namespacestd;using namespaceDtlib;intMain () {intarray[]={ the, -, $, the, the, -, the, -}; intLen =sizeof(array)/sizeof(*array); Sort::insert (Array, Len,false);//Sort Descending for(intI=0; i<len; i++) {cout<< Array[i] <<" "; } cout<<Endl; return 0;}/*test results for all*/
3. Summary
(1) Select the smallest element in the unordered element to select the sort each time.
(2) Insert sort inserts the first element into the previous i-1 element.
(3) Select sort is not stable sorting method, insert sort is stable sorting methods
(4) The time complexity of selecting sort and insert Sort is O (N2)
47th Lesson Select sort and insert sort