-----------Simple Sort-------------

Source: Internet
Author: User

Simple sorting

Internal sort and external sort internal sort is if you have 2GB of memory just have 2GB below the data need to sort so just to store all the data in memory to be sorted this is the internal sort and the opposite of the external sort is that you have 1TB of data to be To sort but you have 2GB of memory when the memory does not hold the data, you need to start the external sort.

Stable and unstable sort the relative position of any two equal data before and after the sort does not change. (This is relatively easy to achieve)

    No sort of ordering is optimal in any case.

So there must be the inevitability of its existence in the textbook of things as long as there should be a good study.

Bubble sort.  This is very simple, and everyone should be. Attach the code directly

Bubbles are rubbish, but there are two of bubbles.

1: You can sort the list (quick sort also seems possible).

2: Stability can be controlled manually (quick sort is also possible).

(Small problem if there are 7 numbers, you need to compare a few times?)  -------------Why? How to calculate?

 ///The core idea is to find the biggest one at a time and then in this exchange know to put the largest to the last side of the second place the second largest in the second place to do n times such operations.
Stability is to see the conditions you need to exchange if it is not greater than or equal to or less than or equal to is stable as long as the equality is also exchanged is an unstable sort.
voidBubble_sort (ElementType a[],intN) { for(p=n-1; p>=0; p--) {flag=0; for(i=0; i<p; i++ ) { /*a trip to bubble*/ if(A[i] > a[i+1]) {Swap (a[i], a[i+1]); Flag=1;/*swap symbol has been swapped*/ } } if(flag==0) Break;/*No exchange for the whole process*/}} //Best case: Order T  = O (n)//Worst case: reverse order T = O (n 2)

Insert Sort: That's the idea of card-playing when we play cards.

1: Short program But what is the most important reason for the insertion sort? Back add

2: Insert sort stable.

The lower bound of time complexity

For subscript i<j, if A[I]>A[J], then the name (I,J) is a pair of reverse pairs (inversion)

calculation of inverse pairs

Problem: Sequence { the,8, -,Wuyi, +, +How many reverse pairs are there in? 9 Pairs ( the,8) ( the, +) ( the, +) ( -,Wuyi) ( -, +) ( -, +) (Wuyi, +) (Wuyi, +) ( +, +)

Swapping 2 adjacent elements just eliminated 1 reverse pairs!

Insert Sort: T (N, I) = O (n+i)
-If the sequence is basically orderly

V

Thinking!!!!!!!!!!

1 4 2) .?      This means: to improve the efficiency of the algorithm, we must? The elimination of more than 1 in reverse order each time!? Each exchange is 2 miles apart

-----------Simple Sort-------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.