Vector sorting

First, the bubble sort:

 Each trip scanning exchange, are recorded the existence of the elements in reverse order.

  

For example as follows:

  

  Once the scan after a trip, did not find any reverse of adjacent elements, which means sorting task has been completed, by returning the flag "sorted", so that even if the algorithm terminates.

  The improved process is as follows:

  

  Skip the red portion, jump directly to the tail.

  


 

Second, improved bubble sort:

  Green for disorderly red for orderly;

 Anti Example: Ordered portion located behind, in front of the disorder. Each exchange is lying scan last element exchange green, making it in place.

 

A method for detecting such a state, whether or not the red recording part is ordered later.

  

  For example as follows:

  

 

Guess you like

Origin www.cnblogs.com/ccpang/p/11415369.html