About three simple sort operations

Source: Internet
Author: User

First, on the insertion of some ideas and implementation of sorting, the principle of insertion is to add a sentinel before the sequence, through the value of the Sentinel compared with the previous, if the need to change the words directly covered with the Sentinel value position, and finally can fill the Sentinel to the new vacant position, The definition by sort means to fill a 0 position directly before the new list with an index of 0.
nums=[1,9,8,5,6,7,4,3,2]
Nums=[0]+nums
Length=len (Nums)
For I in Range (2,length):
Nums[0]=nums[i]
J=i-1
If NUMS[J]>NUMS[0]:
While Nums[j]>nums[0]:
NUMS[J+1]=NUMS[J]
J-=1
Nums[j+1]=nums[0]
Nums.pop (0)
Print (Nums)

This is the underlying code, but why do I have to add a sentinel with zero index in front of the list, and my idea is that you can use an extra value instead of the one in the table, saving the front padding, and then removing it.
nums=[1,9,8,5,6,7,4,3,2]
Length=len (Nums)

For I in Range (1,length):
Sentry=nums[i]
J=i-1
If Nums[j]>sentry:
While Nums[j]>sentry:
NUMS[J+1]=NUMS[J]
J-=1
Nums[j+1]=sentry
Print (Nums)

This can be achieved, but at the same time there is a small problem is the Sentinel in the time is generally more than the Sentinel, such as XXX, if the maximum value is 1, it is easy to directly reduce the negative index over the bounds, so in the second cycle to add a judgment condition is reduced to negative index can be stopped, This can handle the maximum value in the first case.
nums=[10,9,8,5,6,7,4,3,2]
Length=len (Nums)

For I in Range (1,length):
Sentry=nums[i]
J=i-1
If Nums[j]>sentry:
While Nums[j]>sentry:
NUMS[J+1]=NUMS[J]
J-=1
If J==-1:
Break
Nums[j+1]=sentry
Print (Nums)

If you continue to optimize, it is to enter the orderly area after the binary can be found, a little increase in efficiency.
Second, choose the sort
Select Sort is a slightly more optimized than bubble sort algorithm, essentially in each trip to select a maximum value, and then directly with the head or tail of the queue exchange, the final order of a sorting algorithm, and bubble optimization is a trip down a small number of exchanges. But at the same time it can't end early, so it's almost as efficient.
lst=[1,9,8,5,6,7,4,3,2]
Length=len (LST)
For I in range (length):
Maxer=i
For j in Range (I+1,length):
If LST[MAXER]<LST[J]:
Maxer=j

lst[i],lst[maxer]=lst[maxer],lst[i]

Print (LST)

This is a simple quick sort, in which the disadvantage of this sort is not to end prematurely, and we would like to be able to select the maximum and minimum values in one trip
lst=[1,9,8,5,6,7,4,3,2]
Length=len (LST)
For I in Range (LENGTH//2):
Maxer=i
Miner=-i-1
For j in Range (I+1,length-i):
If LST[MAXER]<LST[J]:
Maxer=j
If Lst[-j-1]<lst[miner]:
Miner=-j-1
If Lst[miner]==lst[maxer]:
Break
If Maxer!=i:
Lst[i],lst[maxer]=lst[maxer],lst[i]
If Miner==i or miner==i-length:
Miner=maxer-length
If miner!=-i-1:
LST[-I-1],LST[MINER]=LST[MINER],LST[-I-1]
Print (LST)

Third, bubble sort
nums=[1,2,3,4,5,6,7,8,9]
#nums =[1,9,8,5,6,7,4,3,2]
Length=len (Nums)
Count=0
Scount=0
For I in range (9):
Count+=1
Flag=false
For j in Range (8-i):
If NUMS[J]>NUMS[J+1]:
TMP=NUMS[J]
NUMS[J]=NUMS[J+1]
Nums[j+1]=tmp
Scount+=1
Flag=true
If not flag:
#if Flag==false:
Break
Print (Nums,count,scount)

About three simple sort operations

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.