The basic idea of choosing a sort is: Each trip selects the record with the lowest keyword in n-i+1 (i=1,2,...n-1) as the first record in the ordered sequence. Based on this idea, the algorithm mainly has simple selection sorting, tree selection sorting and heap sorting. [1]
Simple selection of the basic idea of sorting: The 1th trip, in order to sort records R[1]~r[n] to select the smallest record, it and R[1] Exchange, 2nd trip, in order to sort records R[2]~r[n] to select the smallest record, and r[2] exchange; And so on, the first trip to sort records r[i]~r[n ], the smallest record is selected, and it is exchanged with r[i], so that the ordered sequence continues to grow until all sorts are completed.
The following is a simple selection of the stored state of the sort, in which the curly braces are unordered and outside the curly braces are ordered sequences:
Initial sequence: {49 27 65 97 76 12 38}
1th Trip: 12 and 49 Exchange: 12{27 65 97 76 49 38}
2nd trip: 27 not moving: 12 27{65 97 76 49 38}
3rd trip: 65 and 38 Swap: 12 27 38{97 76 49 65}
4th trip: 97 and 49 Swap: 12 27 38 49{76 97 65}
5th trip: 76 and 65 swap: 12 27 38 49 65{97 76}
6th trip: 97 and 76 Exchange: 12 27 38 49 65 76 97 Complete
Write a program that arranges any array from small to large:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #author:show5304 5 defFind_minmunber (list):6Smallest_munber=List[0]7smallest_index=08 forIinchRange (1, Len (list)):9 iflist[i]<Smallest_munber:TenSmallest_munber=List[i] Onesmallest_index=I A returnSmallest_index - - defFrom_small_arr (arr): theNew_arr=[] - forIinchRange (len (arr)): -Min_munber=Find_minmunber (arr) - new_arr.append (Arr.pop (min_munber)) + returnNew_arr - + A atmy_list=[9,24,56,97,82,1,3,6,997,642,352] - Print("the list is arranged from small to large:") - Print(From_small_arr (My_list))
C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe c:/users/administrator/pycharmprojects/ untitled/day1/sx.py list from small to large arranged as: [1, 3, 6, 9, five, up, five, $, 352, 642, 997]process finished with exit code 0
View Code
Change from large to small arrangement:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #author:show5304 5 6 defFind_max_munber (list):7Max_munber=List[0]8max_munber_index=09 forIinchRange (1, Len (list)):Ten ifList[i]>Max_munber: OneMax_munber=List[i] Amax_munber_index=I - returnMax_munber_index - the defForm_large_small (arr): -New_arr=[] - forIinchRange (len (arr)): -Largest_number=Find_max_munber (arr) + new_arr.append (Arr.pop (largest_number)) - returnNew_arr + Amy_list=[7,56,89,41,8,2,46,92,478,1,896,74,66] at Print("arrays are sorted from large to small in order:") - Print(Form_large_small (My_list))
C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe c:/users/administrator/pycharmprojects/ untitled/day1/sx2.py Arrays are arranged from large to small in order: [896, 478,, 2, 7, 8, 1]process finished with exit Code 0
View Code
Python Learning (6) Choosing a sorting algorithm simple code